Home > database >  Sed command to delete characters on specific location?
Sed command to delete characters on specific location?

Time:12-01

I have this sed command which add's 3 zero's to an id (this occurs only if the id is 13 characters long):

sed 's/^\(.\{14\}\)\([0-9]\{13\}[^0-9]\)/\1000\2/' file

My input looks like this:

A:AAAA:AA: :A:**0123456789ABC   **:AAA:AAA       :          :AA:          :     :

And my output is this one:

A:AAAA:AA: :A:**0000123456789ABC   **:AAA:AAA       :          :AA:          :     :

I want to get rid off the 3 whitespaces after the id number. I can't delete the entire column because I have different data on other records so I want to delete the spaces just in the records/lines I expanded previously. So maybe I just need to add something to the existing command. As you can see there are other whitespaces on the record, but I just want to delete the ones next to de ID(bold one).

I only found ways to delete entire columns, but I haven't been able to find a way to delete specific characters.

CodePudding user response:

Just add three spaces after the closing \):

sed 's/^\(.\{14\}\)\([0-9]\{13\}[^0-9]\)   /\1000\2/'

To make it work for your example, you also need to extend [0-9] to [0-9A-C].

CodePudding user response:

You can use

sed 's/^\(.\{14\}\)\([[:alnum:]]\{13\}\)[[:space:]]*:/\1000\2:/' file

See the online demo:

#!/bin/bash
s='A:AAAA:AA: :A:0123456789ABC   :AAA:AAA       :          :AA:          :     :'
sed 's/^\(.\{14\}\)\([[:alnum:]]\{13\}\)[[:space:]]*:/\1000\2:/' <<< "$s"

Output:

A:AAAA:AA: :A:0000123456789ABC:AAA:AAA       :          :AA:          :     :

Notes:

  • [[:alnum:]]\{13\} - matches 13 alphanumeric chars, not just digits
  • [[:space:]]*: matches zero or more whitespaces and a : (hence, the : must be added into the replacement pattern).
  • Related