如何使用 shell 脚本删除文件中不需要的字符



>我有一个看起来像这样的文件(文件.txt)

AYOsVS7Wknsgv2StRsEK JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
AYOsVS7yknsgv2StRsEL JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
AYOsVS8aknsgv2StRsEM JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
AYOsVS8hknsgv2StRsEN JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
AYOsVS8mknsgv2StRsEO JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
RondomText JBC-ChangeService-Test N/A Coverage(89.1%)     JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]

我必须删除最后一列中不需要的字符[JamesPeter]JamesPeter

然后是预期的输出

AYOsVS7Wknsgv2StRsEK JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
AYOsVS7yknsgv2StRsEL JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
AYOsVS8aknsgv2StRsEM JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
AYOsVS8hknsgv2StRsEN JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
AYOsVS8mknsgv2StRsEO JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
RondomText JBC-ChangeService-Test N/A Coverage(89.1%)     JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter

这就是我尝试过的

sed 's/[//; s/]//' file.txt

然后输出我得到

AYOsVS7Wknsgv2StRsEK JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
AYOsVS7yknsgv2StRsEL JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
AYOsVS8aknsgv2StRsEM JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
AYOsVS8hknsgv2StRsEN JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
AYOsVS8mknsgv2StRsEO JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
RondomTextJBC-ChangeService-Test N/A Coverage(89.1%)     JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]

有人可以帮我解决这个问题吗?提前感谢!

注意:我不允许使用jq或通用脚本语言(JavaScript,Python等)。

如果[]确实只出现在最后一列中,那么您可以使用tr删除所有[],据我所知,这不算作通用脚本语言,让file.txt内容

AYOsVS7Wknsgv2StRsEK JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
AYOsVS7yknsgv2StRsEL JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
AYOsVS8aknsgv2StRsEM JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
AYOsVS8hknsgv2StRsEN JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
AYOsVS8mknsgv2StRsEO JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]
RondomText JBC-ChangeService-Test N/A Coverage(89.1%)     JBC 97 2022-10-11-10:23 alan.walker@qt.com development [JamesPeter]

然后

tr -d '[]' < file.txt

给出输出

AYOsVS7Wknsgv2StRsEK JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
AYOsVS7yknsgv2StRsEL JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
AYOsVS8aknsgv2StRsEM JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
AYOsVS8hknsgv2StRsEN JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
AYOsVS8mknsgv2StRsEO JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
RondomText JBC-ChangeService-Test N/A Coverage(89.1%)     JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter

说明:我file.txt输入tr的 stdin,我指示删除以下字符:[]

但是,如果[]可能出现在最后一个以外的列中,那么您可以使用GNUAWK以下方式,让file.txt内容

[HelloWorld] 1 [Name]
Hello        2 [Name]
World        3 [Name]

然后

awk 'BEGIN{FS="[[:space:]]"}{gsub(/[|]/,"",$NF);print}' file.txt

给出输出

[HelloWorld] 1 Name
Hello        2 Name
World        3 Name

说明:我通知GNUAWK字段分隔符(FS)是单个空格字符,然后对于每一行,我全局(因此gsub)使用空字符串替换文字[或(|)文字],即在最后一个字段($NF) 中删除它

(在Gawk 4.2.1中测试)

这个sed应该可以替换输入中任何位置的[]

sed -E 's/[][]//g' file
AYOsVS7Wknsgv2StRsEK JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
AYOsVS7yknsgv2StRsEL JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
AYOsVS8aknsgv2StRsEM JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
AYOsVS8hknsgv2StRsEN JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
AYOsVS8mknsgv2StRsEO JBC-ChangeService-Test    CODE_SMELL JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter
RondomText JBC-ChangeService-Test N/A Coverage(89.1%)     JBC 97 2022-10-11-10:23 alan.walker@qt.com development JamesPeter

但是,如果您只想从最后一个字段中删除[],请使用:

sed -E 's/[([^]]*)]$/1/' file

为什么不使用tr

Prompt> cat file.txt | tr -d "[" | tr -d "]" >result.txt

-d开关删除提到的字符。

相关内容

  • 没有找到相关文章

最新更新