How to Use Sed to Delete Leading Digits

SED Command to remove first digits and spaces of each line

Your sed command would be,

sed 's/.* //g' file

This would remove the first numbers along with the space followed.

How do I remove all letters until the first digit with sed?

Could you please try following. We need to substitute everything till first occurrence of digits from starting so we need to use [^0-9] regex here, which means from starting substitute everything till first occurrence of digits with NULL here.

echo "ABC12345" | sed 's/^[^0-9]*//'

Delete the first five characters on any line of a text file in Linux with sed

sed 's/^.....//'

means

replace ("s", substitute) beginning-of-line then 5 characters (".") with nothing.

There are more compact or flexible ways to write this using sed or cut.

Remove leading and trailing numbers from string, while leaving 2 numbers, using sed or awk

You may try this sed:

sed -E 's/^[0-9]+([0-9]{2})|([0-9]{2})[0-9]+$/\1\2/g' file

51word24
anotherword
12yetanother1
62andherese123anotherline43
23andherese123anotherline45
53andherese123anotherline41

Command Details:

  • ^[0-9]+([0-9]{2}): Match 1+ digits at start if that is followed by 2 digits (captured in a group) and replace with 2 digits in group #1.
  • ([0-9]{2})[0-9]+$: Match 1+ digits at the end if that is preceded by 2 digits (captured in a group) and replace with 2 digits in group #2.

sed delete trailing pattern of digits

Simple sed solution

sed 's/[- 0-9]*$//'  

This will delete trailing dashes, blanks and numbers!

sed remove digits at end of the line

Have you tried this:

     sed 's/[0-9]+$//'

Your command would only match and delete exactly 10 digits at the end of line and only, if you enabled extended regular expressions (-E or -r, depending on your version of sed).

You should try

     sed -r 's/[0-9]{1,10}$//'

How can I remove numbers after a specific character with sed?

You can use

sed -i 's/;[^;]*$//' file.fasta

See the online demo:

s='>Contig_1;2
AGATC...
>Contig_2;345
AaGGC...
>Contig_3;22
GGAGA...'
sed 's/;[^;]*$//' <<< "$s"

Output:

>Contig_1
AGATC...
>Contig_2
AaGGC...
>Contig_3
GGAGA...

Note that sed does not place the newline into the pattern space (since you are using a GNU sed, you could force it to do so with -z, but it is not necessary here), and you can't match a newline with \n in your sed command.

The ;[^;]*$ pattern matches

  • ; - a semi-colon
  • [^;]* - any zero or more chars other than ; (if you need to make sure you match digits, replace with [0-9]* or [[:digit:]]*)
  • $ - end of string.

Note you need no g flag here since this command needs to perform a single replacement per line.

sed: remove numbers from file

sed oneliner

sed -r 's/[^[:space:]]*[0-9][^[:space:]]* ?//g'


Related Topics



Leave a reply



Submit