即使与awk不匹配,也要追加该行

时间:2018-01-17 14:07:43

标签: linux awk

我正在尝试比较两个文件,如果满足某些条件,则附加另一列。

file1.txt

1 101 111 . BCX 123
1 298 306 . CCC 234
1 299 305 . DDD 345



file2.txt

1 101 111 BCX P1@QQQ
1 299 305 DDD P2@WWW

输出应为:

1 101 111 . BCX 123;P1@QQQ
1 298 306 . CCC 234
1 299 305 . DDD 345;P2@WWW

我能做的是,只针对匹配的行进行此操作:

awk 'NR==FNR{ a[$1,$2,$3,$4]=$5; next }{ s=SUBSEP; k=$1 s $2 s $3 s $5  }k in a{ print $0,a[k] }' file2.txt file1.txt 
1 101 111 . BCX 123 P1@QQQ
1 299 305 . DDD 345 P2@WWW

但是,我错过了file1中的第二行。

即使与file2区域不匹配,我怎么还能保留它?

3 个答案:

答案 0 :(得分:3)

如果您想要打印每一行,您需要打印命令不受您的条件限制。

awk '
NR==FNR {
  a[$1,$2,$3,$4]=$5; next
}
{
  s=SUBSEP; k=$1 s $2 s $3 s $5
}
k in a {
  $6=$6 ";" a[k]
}
1' file2.txt file1.txt 

1是速记,表示"打印每一行"。这是一个总是评估" true"的条件(没有命令语句)。

k in a条件只是将现有的第6个字段替换为连接的字段。如果不满足条件,则替换不会发生,但我们仍然会因1而打印。

答案 1 :(得分:1)

关注Private Sub StartBtn_Click(sender As Object, e As EventArgs) Handles StartBtn.Click Dim Numbers As New List(Of Integer) For X As Integer = 1 To 15 Dim Num As Integer = RandomNum() Numbers.Add(Num) Next Dim temp_QuestionSet As New List(Of Question) provider = "Provider=Microsoft.ACE.OLEDB.12.0;" dataFile = "Data Source=C:\course work\dttmq.accdb" connString = provider & dataFile Try Connection.ConnectionString = connString Connection.Open() Catch ex As Exception System.Console.Beep() MsgBox("Error") End Try For Each Number In Numbers Dim _question As New Question _question.ID = Number Using Connection Dim command As New OleDbCommand("SELECT Question FROM final WHERE QuestionID = " & Number.ToString, Connection) Try Dim reader As OleDbDataReader = command.ExecuteReader() While reader.Read() _question.Q = reader(0).ToString() End While reader.Close() Catch ex As Exception End Try End Using Using Connection Dim command As New OleDbCommand("SELECT (Answer1) FROM final WHERE QuestionID = " & Number.ToString, Connection) Try Dim reader As OleDbDataReader = command.ExecuteReader() While reader.Read() _question.A = reader(0).ToString() End While reader.Close() Catch ex As Exception End Try End Using Using Connection Dim command As New OleDbCommand("SELECT Answer2 FROM final WHERE QuestionID = " & Number.ToString, Connection) Try Dim reader As OleDbDataReader = command.ExecuteReader() While reader.Read() _question.B = reader(0).ToString() End While reader.Close() Catch ex As Exception End Try End Using Using Connection Dim command As New OleDbCommand("SELECT Answer3 FROM final WHERE QuestionID = " & Number.ToString, Connection) Try Dim reader As OleDbDataReader = command.ExecuteReader() While reader.Read() _question.C = reader(0).ToString() End While reader.Close() Catch ex As Exception End Try End Using Using Connection Dim command As New OleDbCommand("SELECT Answer4 FROM final WHERE QuestionID = " & Number.ToString, Connection) Try Dim reader As OleDbDataReader = command.ExecuteReader() While reader.Read() _question.D = reader(0).ToString() End While reader.Close() Catch ex As Exception End Try End Using Using Connection Dim command As New OleDbCommand("SELECT CorrectAnswer FROM final WHERE QuestionID = " & Number.ToString, Connection) Try Dim reader As OleDbDataReader = command.ExecuteReader() While reader.Read() _question.Z = reader(0).ToString() End While reader.Close() Catch ex As Exception End Try End Using temp_QuestionSet.Add(_question) Next End Sub 可能对您有帮助。

awk

输出如下。

awk 'FNR==NR{a[$1,$2,$3,$4]=$NF;next} (($1,$2,$3,$5) in a){print $0";"a[$1,$2,$3,$5];next} 1' file2.txt file1.txt

答案 2 :(得分:0)

另一个awk

$ awk '     {t=5-(NR==FNR); k=$1 FS $2 FS $3 FS $t} 
    NR==FNR {a[k]=$NF; next} 
    k in a  {$0=$0 ";" a[k]}1' file2 file1

1 101 111 . BCX 123;P1@QQQ
1 298 306 . CCC 234
1 299 305 . DDD 345;P2@WWW

键的最后一个组件是基于第一个或第二个文件输入的第4或第5个字段;相应地设置它并在脚本中使用单个k变量。注意

t=5-(NR==FNR)

可以按常规编写,

t=NR==FNR?4:5