Dom
Dom

Reputation: 3444

How to replace block text with Sed in Linux?

I have a file I would like to

Easier to understand with a diagram:

enter image description here

I tried that without success:

sed -e "s/\(.*\)161$/ /" $FICSUP$EXTMOD

Data:

CRE                           M                    0123456                                                         01/01/2016                         ZDCP                    H125,45                                                                                                                                                       
CRE                           M                    0123456                                                         01/01/2016                         ZDRTT                   H170                                                                                                                                                          
CRE                           M                    0123456                                                         01/01/2016                         ZDRTTN-1                H130                                                                                                                                                          
CRE                           M                    0123456                                                         01/01/2016                         ZDCPA                   H200                                                                                                                                                          
CRE                           M                    0123457                                                         01/01/2016                         ZDCP                    H124,45                                                                                                                                                       

Upvotes: -1

Views: 52

Answers (2)

chkdsk
chkdsk

Reputation: 1195

Assuming that you want to:

  1. Remove all the numbers and commas in the last field at the end of the field (prior to spaces)
  2. Truncate the spaces in the fixed length field at the end with smaller fixed length spaces

Using your data as input here is what the result looks like Remove the "."s in the regex with however many spaces you need. I have used "." to make the whitespace visible.

$ cat data | sed -r "s/[0-9,]* *$/..................../"
CRE                           M                    0123456                                                         01/01/2016                         ZDCP                    H....................
CRE                           M                    0123456                                                         01/01/2016                         ZDRTT                   H....................
CRE                           M                    0123456                                                         01/01/2016                         ZDRTTN-1                H....................
CRE                           M                    0123456                                                         01/01/2016                         ZDCPA                   H....................
CRE                           M                    0123457                                                         01/01/2016                         ZDCP                    H....................

Upvotes: 0

Mehdi Yedes
Mehdi Yedes

Reputation: 2375

This should do the trick:

sed "s/[0-9]\+[,]*[0-9]*$/         /g" your_file.txt

This replaces every number (including the comma) at the end of the line with 8 spaces.

Upvotes: 0

Related Questions