我有两个文件
file1.txt(由用户注册生成)
1 Name1 Name2 CellPhone Email ANDANOTHER20FIELDS
2 Name1 Name2 CellPhone Email ANDANOTHER20FIELDS
3 Name1 Name2 CellPhone Email ANDANOTHER20FIELDS
4 Name1 Name2 CellPhone Email ANDANOTHER20FIELDS
file2.txt(由用户作业提交生成,只有一行)
CellPhone MLSNUMBER PRICE ANDANOTHER20FIELDS
两个文件中的CellPhone编号始终采用相同的格式3215551234
我想在file1.txt中搜索CellPhone编号,并将完整的特定行添加到file2.txt的开头
我在最后一天左右用awk和sed得到了这么远,但这让我很难过。我相信awk和join就是这样,但这超出了我的技能水平。
答案 0 :(得分:0)
join
会更改您的字段并需要排序的文件。听起来你正在寻找更像这样的东西:
grep " $(awk '{print $1}' file2.txt) " file1.txt|cat - file2.txt
假设file1.txt中的CellPhone号码周围有空格。
答案 1 :(得分:0)
如果您想在file1.txt
中搜索file2.txt
手机号码:
$ awk '
# Only runs on the first file (file2.txt)
NR==FNR {
# Register each phone number in an array
cp[$1]=1
# Skip the next part of the execution
next
}
# Only running for file1.txt
# Check to see if the phone number in file1.txt is in the array
$4 in cp # This is a short way of writing: $4 in cp {print $0};
' file2.txt file1.txt
1 Name1 Name2 CellPhone Email ANDANOTHER20FIELDS
2 Name1 Name2 CellPhone Email ANDANOTHER20FIELDS
3 Name1 Name2 CellPhone Email ANDANOTHER20FIELDS
4 Name1 Name2 CellPhone Email ANDANOTHER20FIELDS
如果你想在最后连接file2.txt
,你可以简单地写| cat - file2.txt
:
awk 'NR==FNR{cp[$1]=1;next}$4 in cp' file2.txt file1.txt | cat - file2.txt
如果您在file2.txt
中有多个电话号码,并希望输出为:
1 Name1 Name2 Phone1 Email ...
2 Name1 Name2 Phone1 Email ...
Phone1 MLSNUMBER PRICE ANDANOTHER20FIELDS
3 Name1 Name2 Phone2 Email ...
4 Name1 Name2 Phone2 Email ...
Phone2 MLSNUMBER PRICE ANDANOTHER20FIELDS
如果 - 假设file1.txt
预先按电话号码排序:
$ awk '
# Only runs on the first file (file2.txt)
NR==FNR {
# Register each phone number in an array
cp[$1]=$0
# Skip the next part of the execution
next
}
# Print current line same as 1==1{print $0}
1;
FNR>1 && prev!=$4 {
print prevLine
}
{
prev=$4
prevLine=cp[$4]
}
END {
print prevLine
}
' file2.txt file1.txt
1 Name1 Name2 Phone1 Email ...
2 Name1 Name2 Phone1 Email ...
1 Name1 Name2 CellPhone Email ANDANOTHER20FIELDS
Phone1 MLSNUMBER PRICE ANDANOTHER20FIELDS
2 Name1 Name2 CellPhone Email ANDANOTHER20FIELDS
3 Name1 Name2 CellPhone Email ANDANOTHER20FIELDS
4 Name1 Name2 CellPhone Email ANDANOTHER20FIELDS
CellPhone MLSNUMBER PRICE ANDANOTHER20FIELDS