0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vlookup的なことをbash上でノンプラグラミングで行いたい

Last updated at Posted at 2019-12-13

目的

vlookup的なことをbash上でノンプラグラミングで行いたい。

比較するファイル

次の2つのファイルを比較したい
id_list.txt
id_list_new.txt

id_list.txt
ID,NAME,STATUS,WEIGHT
1001,taro,enable,81
1002,jiro,disable,65
1004,hanako,enable,46
1005,makoto,enable,50
1006,ichiro,enable,72
1007,satoru,disable,78
id_list_new.txt
ID,NAME,STATUS,WEIGHT
1001,taro,enable,81
1002,jiro,disable,65
1004,hanako,disble,46
1005,makoto,enable,49
1006,ichiro,enable,72
1007,satoru,disable,78
1008,fujiko,enable,46

使用するツール

1)catコマンド
 文字列を連結する
2)echoコマンド
 文字列を出力する
3)joinコマンド
 文字列を連結する
4)commコマンド
 文字列を比較し、差分を出力する
5)sortコマンド
 文字列を並び替える
と 
リダイレクト |
名前付きパイプ <()

VLOOKUP的なコマンド

できる限りワンライナーで。

vlookup.sh
# !/bin/bash

a="id_list.txt"
b="id_list_new_txt"

cat <(echo "ID,NAME,STATUS,WEIGHT,,,")\  
 <(join -t, -j 1 <(comm -13 <(sort ${a}) <(sort ${b})) <(sort ${a}))\
 <(join -t, -j 1 <(comm -13 <(sort ${a}) <( sort ${b})) <(sort ${a}) -v 1)

出力結果

IDをキーとし、最新のデータを左に、変更前のデータを右に並べる。

ID,NAME,STATUS,WEIGHT,,,
1004,hanako,disble,46,hanako,enable,46
1005,makoto,enable,49,makoto,enable,50
1008,fujiko,enable,46
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?