2

我有一个包含单词列表的文件,例如:

FIRST_WORD abc
FIRST_WORD(1) bcd
FIRST_WORD(2) def
SECOND_WORD gh
THIRD_WORD jiu
THIRD_WORD(1) lom
...

我想删除它(i),当它存在时,以获得:

FIRST_WORD abc
FIRST_WORD bcd
FIRST_WORD def
SECOND_WORD gh
THIRD_WORD jiu
THIRD_WORD lom
...
4

5 回答 5

15

你可以的:

test="123456"
echo ${test:3}

输出:

456
于 2013-01-07T10:43:47.927 回答
3

使用 : 全局替换括号内的所有数字字符串sed

$ sed 's/([0-9]\+)//g' file
FIRST_WORD abc
FIRST_WORD bcd
FIRST_WORD def
SECOND_WORD gh
THIRD_WORD jiu
THIRD_WORD lom

# Save the changes back to the file  
$ sed -i 's/([0-9]\+)//g' file

使用删除前 3 个字符sed

$ sed 's/^...//' file
ST_WORD abc
ST_WORD(1) bcd
ST_WORD(2) def
OND_WORD gh
RD_WORD jiu
RD_WORD(1) lom
于 2013-01-07T10:46:45.037 回答
3

尝试这个。

sed 's/([0-9])//g' file
于 2013-01-07T10:47:01.607 回答
2

这可能对您有用(GNU sed):

sed -r '$!N;s/^((\S+).*\n\2)\([^)]*\)/\1/;P;D' file

但是,如果:

sed 's/([0-9]\+)//' file

就足够了。

于 2013-01-07T12:31:54.407 回答
1

这是一个纯 bash 实现:

while read -r a b; do
    echo "${a%(*)}" "$b"
done < input.txt
于 2013-01-09T14:45:48.117 回答