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

2j4z5cfb  于 2022-11-16  发布在  Shell
关注(0)|答案(3)|浏览(278)

我有一个文件,它看起来像这样(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]

我必须删除最后一列[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等)。

omqzjyyz

omqzjyyz1#

如果[]确实只出现在最后一列中,那么您可以使用tr删除所有[],据我所知,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,并指示删除以下字符:一个月八个月一个月,一个月九个月。
然而,如果[]可能出现在列中而不是最后一列,那么您可以按照以下方式使用GNU AWK,让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

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

  • (在gawk 4.2.1中测试)*
ygya80vv

ygya80vv2#

这个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
col17t5w

col17t5w3#

为什么不使用tr

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

-d开关用于删除上述字符。

相关问题