Removing trailing spaces from delimited files

Hi All

I have a file of the following format (delimited by |)

this is field 1 | field 2 (lots of blank spaces) | field 3 (lots of blank space) |
field 1 | more text (lots of blank spaces) | dhjdsk |

Is there a way I can remove trailing spaces between the end of the text in each field and the delimiter? E.g. I would like to end up with the following...

this is field 1 | field 2 | field 3 |
field 1 | more text | dhjdsk |

Is this possible?

i could not spot any difference bet i/p and o/p

is this what you are looking for :slight_smile:

echo "a  | b  | c |" | sed "s/[' ']*|/|/g"

o/p: a| b| c|

works like a charm.... thanks mate :smiley:

sed -e "s/[' ']*|/|/g" $FILE

Using the line of code above, i get trailing spaces to be removed between the end of the field and the delimiter. I was hoping someone might explain how the command knows to take from the last space.

For example, if i have a field a row in $FILE such as...

this is field 1 | this is field 2 | a lot of text in field 3

...how does the command know to remove the space after the "1" instead of taking everything after the first space, i.e., after "this".

Any explanation would be hugely appreciated.

try to concentrate on regex "[' ']*|". It means zero or more spaces followes by bar "|".

"[' ']*|" is replaced by "|".

hope its clear now.

[' ']*| Means all blanks ' ' before | , sed changes that for |.
You can see it better with an example:

echo "a        ssdddddddd| b       | c |" | sed "s/[d]*|/ |/g"
OUPUT:
a        ss | b        | c  |

Regards.

thanks guys for your replies. this has helped me to understand :slight_smile: