awk grep 或 sed:如何匹配两个文件

awk grep or sed : how to match two files

(你真棒)

如何将参数从上一个命令传递给 awk 或 grep?

我想匹配来自两个文件的一个字段(用户名):

预期结果:

firstname   Lastname  Username IPaddress
Mozes       Bowman    user1    134.244.47.32
Jazzmyn     Parrish   user2    3.249.198.34
Chet        Woods     user3    52.215.73.213

来自这 2 个:

**file1**
IPaddress       Username
34.244.47.32    user1
3.249.198.34    user2
52.215.73.213   user3

**file2**
firstname Lastname   Username
Mozes     Bowman     user1
Jazzmyn   Parrish    user2
Chet      Woods      user3

这是我所能做到的:

awk '{print ,}' file1 | while read Username IP ; do grep $username file2 && echo $IP; done

导致IP每次换行显示:

firstname   Lastname    Username
IPaddress
Mozes   Bowman  user1
34.244.47.32
Jazzmyn Parrish user2
3.249.198.34
Chet    Woods   user3
52.215.73.213

你可以试试这个awk

awk 'NR==FNR {r[]=[=10=] ; next } { print r[]"\t"  }' $file2 $file1

输出

firstname Lastname   Username   IPaddress
Mozes     Bowman     user1      34.244.47.32
Jazzmyn   Parrish    user2      3.249.198.34
Chet      Woods      user3      52.215.73.213

请尝试以下 awk 代码。这将处理我们在用户名中也有空格的情况。按照 OP 显示的示例使用相同的格式编写和测试。

awk '
BEGIN{
  OFS="\t"
  print "firstname   Lastname  Username IPaddress"
}
NR==1{ next }
FNR==NR{
  match([=10=],/^([0-9]+\.)+[0-9]+[[:space:]]+/)
  value=substr([=10=],RSTART,RLENGTH)
  sub(/[[:space:]]+$/,"",value)
  arr[substr([=10=],RSTART+RLENGTH)]=value
  next
}
{
  first=
  second=
  ==""
  sub(/^[[:space:]]+/,"")
}
([=10=] in arr){
  print first,second,[=10=],arr[[=10=]]
}
' file1 file2

说明:为上述解决方案添加详细说明。

awk '                                                ##Starting awk program from here.
BEGIN{                                               ##Starting BEGIN section of this program from here.
  OFS="\t"                                           ##Setting OFS as tab here.
  print "firstname   Lastname  Username IPaddress"   ##printing header here.
}
NR==1{ next }                                        ##Checking condition if this is very first line of file1 then simply ignore it.
FNR==NR{                                             ##This condition will be TRUE when file1 is being read.
  match([=11=],/^([0-9]+\.)+[0-9]+[[:space:]]+/)         ##Using match function to match IP address in file1 here followed by spaces.
  value=substr([=11=],RSTART,RLENGTH)                    ##Creating value which is substring of matched regex.
  sub(/[[:space:]]+$/,"",value)                      ##Substituting spaces till last in value.
  arr[substr([=11=],RSTART+RLENGTH)]=value               ##Creating array named arr with index of rest of value(apart from ip values followed by spaces in file1) and its value is variable value.
  next                                               ##next will skip all further statements from here.
}
{
  first=                                           ##Creating first which has  of current line.
  second=                                          ##Creating second which has  of current line.
  ==""                                           ##Nullifying  and  here.
  sub(/^[[:space:]]+/,"")                            ##Substituting spaces from starting with null.
}
([=11=] in arr){                                         ##If current line is present in arr then do following.
  print first,second,[=11=],arr[[=11=]]                      ##Printing variables then current line followed by arr value.
}
' file1 file2                                        ##Mentioning Input_file names here.

您可以使用这个 awk:

awk 'NR==FNR {a[]=; next} {print [=10=], a[]}' f1 f2 | column -t
firstname  Lastname  Username  IPaddress
Mozes      Bowman    user1     34.244.47.32
Jazzmyn    Parrish   user2     3.249.198.34
Chet       Woods     user3     52.215.73.213

使用 column -t 进行表格输出。

对于您问题中的示例,您只需要:

$ paste file2 file1 | sed 's/ *[^ ]*$//'
firstname Lastname   Username   IPaddress
Mozes     Bowman     user1      34.244.47.32
Jazzmyn   Parrish    user2      3.249.198.34
Chet      Woods      user3      52.215.73.213