2つのファイルを比較してテーブル形式で表示する方法

2つのファイルを比較してテーブル形式で表示する方法

両方のファイルを比較し、シェルスクリプトを使用してテーブル形式で違いを表示する必要があります。例えば。

ファイル1.txt

ap-2.21.3.rpm
bp-3.42.4.rpm
cp-devel-3.23.2.rpm
ep-devel- 2.23.2-23.rpm

ファイル2.txt

ap-2.21.3.rpm
bp-3.43.4.rpm
cp-devel-4.33.2.rpm
dp-4.52.4.rpm

出力は次のとおりです。

Name                           |        file1              | file2
-------------------------------+---------------------------+---------------------------
bp                             |         3.42.4            | 3.43.4
cp-devel                       |         3.23.2            | 4.33.2
dp                             |                           | 4.52.4
ep-devel                       |         2.23.2-23         | 

ベストアンサー1

  1. cat file1| awk -F "-" '{print $2}'| sed "s/\.[a-z].*//g">file_1_final.txt
  2. cat file2| awk -F "-" '{print $2}'| sed "s/\.[a-z].*//g">file_2_final.txt
  3. paste file_1_final.txt file_2_final.txt | sed '1i file1 file2' >combined_file1_file2
  4. awk -F "-" 'NR==FNR{a[$1];next}($1 in a){print $1}' file1 file2>>common_difference_file_1_2
  5. awk -F "-" 'NR==FNR{a[$1];next}!($1 in a){print $1}' file1 file2>>common_difference_file_1_2
  6. sed -i '1i name' common_difference_file_1_2
  7. paste common_difference_file_1_2 combined_file1_file2

出力:

name    file1     file2
ap  2.21.3  2.35.3
bp  3.42.4  3.43.4
cp  3.23.2  4.33.2
dp      4.52.4

おすすめ記事