简体   繁体   English

awk 删除以字符结尾的行

[英]awk to remove lines that finish with a character

I have a string:我有一个字符串:

COL1列1 COL2列2
PRE test1/测试1/
PRE test1/测试1/
PRE test1/测试1/
2023-01-27 2023-01-27 12:37:16 12:37:16
2023-01-27 2023-01-27 12:37:16 12:37:16
2023-01-27 2023-01-27 12:37:16 12:37:16
2023-01-27 2023-01-27 12:37:16 12:37:16
2023-01-27 2023-01-27 12:37:16 12:37:16

Want left a black space with awk the complete lines that finish with the character "/" but i cannot guess it.想要用 awk 留下一个黑色空间,以字符“/”结尾的完整行,但我猜不出来。

I test it for example but doesn't work:例如,我对其进行了测试但不起作用:

awk '{gsub("*/",""); print $1 $2}'

Thanks!谢谢!

Use the pattern expression to run different code depending on whether the line ends with /$ or not.根据行是否以/$结尾,使用模式表达式运行不同的代码。

awk '!/\/$/ {print $1, $2}
     /\/$/ {print ""}' filename

The issue with the command you tried is that it only replaces the string "*/" with an empty string, but it doesn't check for lines that end with the character "/".您尝试的命令的问题是它仅将字符串“*/”替换为空字符串,但不检查以字符“/”结尾的行。 Here's a corrected version of the command:这是该命令的更正版本:

awk '$2 ~ /\/$/ {gsub("\/$",""); print $1, $2}'

This command uses the ~ operator to check if the second field ( $2 ) ends with the character / , and if so, removes the / from the end of the line before printing both fields.此命令使用~运算符检查第二个字段 ( $2 ) 是否以字符/结尾,如果是,则在打印两个字段之前从行尾删除/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM