gpt4 book ai didi

linux - AWK 在同一文件中搜索事实本身

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:50:16 26 4
gpt4 key购买 nike

我有一个文件在哪里

数据是

90|123456|.. some more fields
90|654321|... some more fields
.... some more lines starting with 90
91|123456|.. some more fields
91|654321|... some more fields
.... some more lines starting with 91
92|123456|.. some more fields
92|654321|... some more fields
.... some more lines starting with 92

第二个字段对我来说是关键值&它将在开始字段中具有 90,91 和 92 值

90|keyvalue will always be there
91|keyvalue .. not mendatory
92|keyvalue .. not mendatory

预期输出是

90|keyvalue [Mendatory]
91|keyvalue --> print if exist in file
92|keyvalue --> print if exist in file

对于所有键值

我做的是

grep "^90" origfilename |awk -F '|' '{print $2}'> temp90.txt #this gives me all keyvalues

awk '{print "90|"$0"|"}' temp90.txt >> temp90-1.txt
awk '{print "91|"$0"|"}' temp90.txt >> temp90-1.txt
awk '{print "92|"$0"|"}' temp90.txt >> temp90-1.txt

grep -f temp90-1.txt origfilename

这让我得到了输出,但我认为这不是正确有效的方法

如何在单个 awk 或其他方式上做到这一点

最佳答案

awk 助您一臂之力!

$ awk -F'|' 'NR==FNR && /^90/  {k[$2]} 
NR!=FNR && $2 in k{print}' file{,}

90|123456|.. some more fields
90|654321|... some more fields
91|123456|.. some more fields
91|654321|... some more fields
92|123456|.. some more fields
92|654321|... some more fields

说明 在第一次扫描中获取键,在第二次扫描中打印具有匹配键的行。请注意,file{,}file file 相同,awk 会双重扫描输入文件。

关于linux - AWK 在同一文件中搜索事实本身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33617296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com