0

remove digits from end of string, example input:

example123
example321
example0123

expected output:

example
example
example

I tried the below which didn't work:

sed 's/[0-9]\+$//' <in.txt >out.txt 

What should I do?

  • 1
    What didn't work about it? Works for me with GNU sed 4.2.2 – Jesse_b Mar 20 '18 at 14:30
1

The result of:

$ sed 's/[0-9]\+$//' <in.txt

Is:

example
example
example

The command you executed (sed 's/[0-9]\+$//' <in.txt >out.txt) will direct the output to the out.txt file

Running cat out.txt will give you the results you are looking for.

1

\+ will work on GNU sed, not sure about other implementations. Try this

$ sed 's/[0-9]*$//' ip.txt 
example
example
example

$ # or if ERE option is available
$ sed -E 's/[0-9]+$//' ip.txt 
example
example
example
0

Possibly you have whitespace after some of your text. You don't need the $ and removing it will solve that problem:

sed 's/[0-9]\+//' <in.txt >out.txt
0

If you want to apply the changes directly in the input file, so that you don't have to write another file, then use the '-i' flag:

sed -i -r 's/[0-9]+$//' in.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.