datahappy
datahappy

Reputation: 856

Replace White Spaces with sed

I have a large file (100M rows) in the following format:

Week      |ID           |Product |Count     |Price
---------- ------------- -------- ---------- -----   
2016-01-01|00056001     |172     |23        |3.50
2016-01-01|1            |125     |15        |2.75

I am trying to use sed to add Xs to the missing digits on the second customer ID, but maintain the number of spaces after the full ID. So, the table would look like:

Week      |ID           |Product |Count     |Price
---------- ------------- -------- ---------- -----   
2016-01-01|00056001     |172     |23        |3.50
2016-01-01|1XXXXXXX     |125     |15        |2.75

I have tried

 sed -i "s/\s\{29,\}/XXXXXXX                      /g" *.csv

and

sed -i -- "s/1                              /1XXXXXXX                       /g" *.csv

Neither with any change to the file. What am I missing?

Thanks.

EDIT for clarification: There are 29 spaces after the 1 in the actual data. I used less on the example table for readability sake. I assume whatever solution works will apply no matter the number of spaces.

Upvotes: 1

Views: 113

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140148

That works for me (not using \s but merely space, and dropped the useless g option because needed once per line only):

sed -i "s/[ ]\{29,\}/XXXXXXX                      /" *.csv

Although for safety reasons I would rather use a more restrictive script which would perform the substitution only if |1 is encountered:

sed -i "s/\(\|1\)[ ]\{29,\}/\1XXXXXXX                      /" *.csv

Upvotes: 2

Related Questions