user58602
user58602

Reputation: 37

Removing trailing and leading spaces of a field using AWK or SED

I have a database text-file (csv-like) that contains excessive trailing and leading spaces. These incidents only occur around character "|", which is used to separate columns. My goal is to remove these spaces using awk or sed. I can't get this seemingly simple task to work; is it perhaps because | is a special character?

Input.txt

| |COL1 |COL2  |COL3      |COL4 |COL11|COL99|COL19     |COL88     |CAL9            |COL84           |COL98           | 
| |500  |0001  |0100000000|1995 |001  |     |Test  This|00.00.0000|           6,14 |          12,00 |           0,00 | 
| |500  |0001  |0100000000|1995 |002  |     |Separ ated|00.00.0000|          18,14 |          12,00 |           0,00 | 

Output.txt

||COL1|COL2|COL3|COL4|COL11|COL99|COL19|COL88|COL9|COL84|COL98|
||500|0001|0100000000|1995|001||Test  This|00.00.0000|6,14|12,00|0,00|
||500|0001|0100000000|1995|001||Separ ated|00.00.0000|18,14|12,00|0,00|

Upvotes: 1

Views: 1623

Answers (3)

repzero
repzero

Reputation: 8412

use sed like:

   sed 's/ *\(|\) */\1/g' file 

use awk like

awk '{gsub(/ *\| */, "|");print}' file

edit: use gawk like

gawk '{print gensub(/ *(\|) */, "\\1","g")}' file

Upvotes: 0

Etan Reisner
Etan Reisner

Reputation: 80921

This will remove all spaces with awk by forcing awk to recalculate the output using the new value of OFS to separate the fields (which were split on whitespace to begin with).

awk -v OFS='' '{$1=$1; print}' in.csv

To remove just "field separating" spaces try this:

awk -F '[[:space:]]*\\|[[:space:]]*' -v OFS='|' '{$1=$1; print}' in.csv

Upvotes: 0

Jotne
Jotne

Reputation: 41446

This should do:

sed 's/ *| */|/g' file
||COL1|COL2|COL3|COL4|COL11|COL99|COL19|COL88|CAL9|COL84|COL98|
||500|0001|0100000000|1995|001||Test  This|00.00.0000|6,14|12,00|0,00|
||500|0001|0100000000|1995|002||Separ ated|00.00.0000|18,14|12,00|0,00|

It change any <space>|<space> with | so other space are not removed.

Upvotes: 4

Related Questions