2

I have a file with a list of words like:

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

and I want to remove the (i), when it is present, to obtain:

FIRST_WORD abc
FIRST_WORD bcd
FIRST_WORD def
SECOND_WORD gh
THIRD_WORD jiu
THIRD_WORD lom
...
Melebius
  • 6,183
  • 4
  • 39
  • 52
user1835630
  • 251
  • 2
  • 6
  • 17
  • 4
    The question title doesn't match the example!? – Chris Seymour Jan 07 '13 at 10:50
  • 1
    So if `(i)` appears elsewhere it should not be replaced, or will it never appear other than as per examples? – Bohemian Jan 07 '13 at 10:56
  • @ChrisSeymour Since OP hasn’t responded for a long time, I updated the question title. Please consider removing your comment and updating [your answer](https://stackoverflow.com/a/14194320/711006) (the best one IMO) to mention that. – Melebius Dec 12 '17 at 12:11

5 Answers5

16

You can get it:

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

Output:

456
arutaku
  • 5,937
  • 1
  • 24
  • 38
3

Global replacement of all digits strings found inside parenthesis using 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

Removing the first 3 characters using 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
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
3

Try this.

sed 's/([0-9])//g' file
jgr
  • 3,914
  • 2
  • 24
  • 28
2

This might work for you (GNU sed):

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

However it might be overkill, if:

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

is suffice.

potong
  • 55,640
  • 6
  • 51
  • 83
1

Here is a pure bash implementation:

while read -r a b; do
    echo "${a%(*)}" "$b"
done < input.txt
Josh Cartwright
  • 771
  • 4
  • 7