如果第 5 列有连字符,则从文件中删除该行
Delete the line from a file if 5th column had hypen
示例文件test.txt
12-3 34r5 2324 - 23ed423 2322
3-4 2-32 45 34ed - 232
e-r 34-2 234 232de - 232
23-4 23-2 232 - - 232de
- - - - - -
预期的输出是
12-3 34r5 2324 - 23ed423 2322
条件是,应从文件中删除第 5 列中包含连字符“-”的行。
是否可以使用 sed 或 awk
我尝试了类似 awk '{print }' test.txt|grep '-'|xargs sed -i -e /-/d
但没有成功
能否请您尝试以下。
awk '=="-"{next} 1' Input_file > temp && mv temp Input_file
或
awk '!="-"' Input_file > temp && mv temp Input_file
解释: 当在第 5 个字段中找到 -
或在第 2 个解决方案中简单地打印时,它只是将光标移动到下一个位置那些在第 5 个字段中没有 -
的行。
示例文件test.txt
12-3 34r5 2324 - 23ed423 2322
3-4 2-32 45 34ed - 232
e-r 34-2 234 232de - 232
23-4 23-2 232 - - 232de
- - - - - -
预期的输出是
12-3 34r5 2324 - 23ed423 2322
条件是,应从文件中删除第 5 列中包含连字符“-”的行。 是否可以使用 sed 或 awk
我尝试了类似 awk '{print }' test.txt|grep '-'|xargs sed -i -e /-/d
但没有成功
能否请您尝试以下。
awk '=="-"{next} 1' Input_file > temp && mv temp Input_file
或
awk '!="-"' Input_file > temp && mv temp Input_file
解释: 当在第 5 个字段中找到 -
或在第 2 个解决方案中简单地打印时,它只是将光标移动到下一个位置那些在第 5 个字段中没有 -
的行。