Removing trailing spaces from delimited files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing trailing spaces from delimited files
# 1  
Old 03-28-2006
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?
# 2  
Old 03-28-2006
Quote:
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 |
i could not spot any difference bet i/p and o/p
Quote:
this is field 1 | field 2 | field 3 |
field 1 | more text | dhjdsk |
is this what you are looking for Smilie
Code:
echo "a  | b  | c |" | sed "s/[' ']*|/|/g"

o/p: a| b| c|
# 3  
Old 03-28-2006
works like a charm.... thanks mate Smilie
# 4  
Old 03-29-2006
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.
# 5  
Old 03-29-2006
try to concentrate on regex "[' ']*|". It means zero or more spaces followes by bar "|".

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

hope its clear now.
# 6  
Old 03-29-2006
Quote:
Originally Posted by djkane
sed -e "s/[' ']*|/|/g" $FILE.
[' ']*| Means all blanks ' ' before | , sed changes that for |.
You can see it better with an example:
Code:
echo "a        ssdddddddd| b       | c |" | sed "s/[d]*|/ |/g"
OUPUT:
a        ss | b        | c  |

Regards.
# 7  
Old 03-29-2006
thanks guys for your replies. this has helped me to understand Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Want to add trailing spaces to the variable

I want to keep string/varible length to 10 even its actual length is less than 10(may be no value). so, i want to add trailing spaces to my string. :wall: "typeset -L10 myvarible" is not working, its saying invalid typset -L option. Can you please advise. (4 Replies)
Discussion started by: djaks111
4 Replies

2. Shell Programming and Scripting

[solved] Trailing spaces

Hello People How to check whether lines in a text file have trailing spaces or not and if a line have trailing spaces then how many trailing spaces line have? Regards ARvind kumar (5 Replies)
Discussion started by: arvindk.monu
5 Replies

3. UNIX for Dummies Questions & Answers

Removing blank spaces from text files in UNIX

Hello, I am an super newbie, so forgive my sheer ignorance. I have a series of text files formatted as follows (just showing the header and first few lines): mean_geo mean_raw lat lon 0.000 0 -70.616 163.021 0.000 0 -70.620 163.073 0.000 ... (8 Replies)
Discussion started by: vtoniolo
8 Replies

4. Shell Programming and Scripting

parsing log files, removing spaces and replace with commas

Hello all i am working on a database to import log files from my systems, but i cannot seem to find the answer. I searched here for a good bit and couldnt peice together what i was looking for. I know you could do this with awk, i just dont know how. Any help would be greatly appreciated.... (6 Replies)
Discussion started by: caddyjoe77
6 Replies

5. Shell Programming and Scripting

Removing leading and trailing spaces only in PERL

Hi All, I have a file with the following contents with multiple lines 172445957| 000005911|8| 400 Peninsula Ave.#1551 | And,K |935172445957|000005911 607573888 |000098536 | 2|Ane, B |J |Ane |1868 |19861206|20090106|20071001 I want to trim the "leading and trailing spaces only" from... (2 Replies)
Discussion started by: kumar04
2 Replies

6. UNIX for Dummies Questions & Answers

How to remove trailing spaces

Hi, I have a file like this (ADD_MONTHS((Substr(Trim(BOTH FROM Translate(Maximum(closeDa ------------------------------------------------------------ 2007-06-30 00:00:00 I have a requirement where i need just the date. When i do: tail -1... (2 Replies)
Discussion started by: mahek_bedi
2 Replies

7. UNIX for Dummies Questions & Answers

Removing leading and trailing spaces of data between the tags in xml.

I am having xml document as below. <transactionid> 00 </transactionid> <tracknumber> 0 </tracknumber> <key> N/A </key> But the data contains leading and trailing spaces between the tags. Please let me know how can i remove these leading and trailing spaces between the tags.... (2 Replies)
Discussion started by: jhmr7
2 Replies

8. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies

9. Shell Programming and Scripting

Leading and Trailing Spaces

Hi, how to i remove leading and trailing spaces from a line? the spaces can be behind or in front of any field or line example of a line in the input data: Amy Reds , 100 , /bin/sh how to i get it to be: Amy Read,100,/bin/sh i saw something on this on the Man pages for AWK... (7 Replies)
Discussion started by: sleepster
7 Replies

10. UNIX for Dummies Questions & Answers

removing trailing spaces of a particular column in a file

Hi, I am currently confused. Suppose I have a file something like the one below. 4299|raj Telecommunications|12||||| 4302|anjali International Ltd.|86|ritchie||dong|(000)2890 9993 |(222)4881 3689 4305|フィデュシアリ・ト-スト・インター...ショ...ル投資顧問株式会社 |112||||01-9211-1931 |08-3677-1985 Now... (2 Replies)
Discussion started by: rooh
2 Replies
Login or Register to Ask a Question