我有两个文件在第一列中包含大城市列表,在其余列中包含不同的数据。我要做的是创建两个新文件,以便两个文件中的列1相同。意思是我想要删除其他文件中不存在的两个文件中的任何行。这两个文件都是CSV。
示例文件1
"Austin, TX",123,1234,12345
"Beaumont, TX",123,1234,12345
"Charlotte, NC",123,1234,12345
"Detroit, MI",123,1234,12345
示例文件2
"Austin, TX",abc,dbas,woeij
"Baytown, TX",abc,dbas,woeij
"Charlotte, NC",abc,dbas,woeij
"Denver, CO",abc,dbas,woeij
输出文件1
"Austin, TX",123,1234,12345
"Charlotte, NC",123,1234,12345
输出文件2
"Austin, TX",abc,dbas,woeij
"Charlotte, NC",abc,dbas,woeij
答案 0 :(得分:1)
使用GNU grep
:
grep -f <(grep -oP '^".*?"' file2.txt) file1.txt
输出:
"Austin, TX",123,1234,12345 "Charlotte, NC",123,1234,12345
grep -f <(grep -oP '^".*?"' file1.txt) file2.txt
输出:
"Austin, TX",abc,dbas,woeij "Charlotte, NC",abc,dbas,woeij