Remove spaces before a delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove spaces before a delimiter
# 1  
Old 11-14-2008
Remove spaces before a delimiter

Hi All,

I need to modify a script to remove spaces from a csv file.
The csv file is delimited by the '~' character and I need to remove the spaces which appear before this character.
i.e
Sample input:
LQ001 SWAT 11767727 ~9104 ~001 ~NIRSWA TEST 18 ~2 ~Standard Test ~0011

Desired Output:
LQ001 SWAT 11767727~9104~001~NIRSWA TEST 18~2~Standard Test~0011

Am new to shell scripting and have been playing around with the sed
command but with limited success!!

Any help appreciated, thanks.
# 2  
Old 11-14-2008
redoubtable@Tsunami ~ $ echo "LQ001 SWAT 11767727 ~9104 ~001 ~NIRSWA TEST 18 ~2 ~Standard Test ~0011"|sed -e 's/\s~/~/g'
LQ001 SWAT 11767727~9104~001~NIRSWA TEST 18~2~Standard Test~0011
redoubtable@Tsunami ~ $
# 3  
Old 11-14-2008
Thanks for that.

The output I am getting though is

LQ001 SWAT 11767727 ~9104 ~001 ~NIRSWA TEST 18 ~2 ~Standard Test ~0011

??

Thanks
Sue
# 4  
Old 11-14-2008
Try this:
Code:
echo 'LQ001 SWAT 11767727 ~9104 ~001 ~NIRSWA TEST 18 ~2 ~Standard Test ~001'|sed 's/ ~/~/g'

# 5  
Old 11-14-2008
Thanks for that - Almost there but not quite!!

Unfortunately there is often more than one space before the '~' delimiter. Sorry, its not clear from my example above:

So

echo 'LQ001 SWAT 11767727 ~9104 ~001 ~NIRSWA TEST 18 ~2 ~Standard Test ~001'|sed 's/ ~/~/g'

LQ001 SWAT 11767727 ~9104~001~NIRSWA TEST 18 ~2~Standard Test~001

is only removing 1 space before the '~' rather than all spaces

Thanks
Sue
# 6  
Old 11-14-2008
Hi Sue,

Quote:
Originally Posted by SRyan84
Thanks for that - Almost there but not quite!!

Unfortunately there is often more than one space before the '~' delimiter.
In this case this should solve all your problems:

Code:
sed 's/[<spc><tab>]*~/~/g'

Replace "<spc>" and "<tab>" with literal spaces and tab characters. It will remove any number of whitespace before a tilde ("~") character everywhere on the line.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicates separated by delimiter

First post, been browsing for 3 days and came out with nothing so far. M3 C2 V5 D5 HH:FF A1-A2,A5-A6,A1-A2,A1-4 B4-B6,B2-B4,B4-B6,B1-B2output should be M3 C2 V5 D5 HH:FF A1-A2,A5-A6,A1-A4 B2-B4,B4-B6,B1-B2On col 6 and 7 there are strings in form of Ax-Ax and Bx-Bx respectively. Each string are... (9 Replies)
Discussion started by: enrikS
9 Replies

2. UNIX for Beginners Questions & Answers

Remove whitespaces between delimiter and characters

Hello All, I have a pipe delimited file and below is a sample data how it looks: CS123 | | || 5897 | QXCYN87876 As stated above, the delimited files contains sometimes only spaces as data fields and sometimes there are extra spaces before/after numeric/character data fields. My requirement... (4 Replies)
Discussion started by: amvip
4 Replies

3. UNIX for Advanced & Expert Users

How to remove the delimiter from the column value within a file?

Hello All, we have some 10 files wherein we are using the ASCII NULL as separator which is nothing but '^@' and we need to change it to pipe delimited file before loading to database. Most of the data seems to be fine but there are instances where this separator tends to appear in the middle of... (9 Replies)
Discussion started by: dJHa
9 Replies

4. UNIX for Dummies Questions & Answers

Remove Extra Delimiter

Hi , I have file like this.. aaa|bbbb|cccc|dddd|fff|dsaaFFDFD| Adsads|sas|sa|as|asa|saddas|dsasd|sdad| dsas|dss|sss|sss|ddd|dssd|rrr|fddf| www|fff|refd|dads|fsdf|00sd| 5fgdg|dfs00|d55f|sfds55|445fsd|55ds|sdf| so I do no have any fix pattern and I want to remove extra... (11 Replies)
Discussion started by: pankajchaudhari
11 Replies

5. UNIX for Dummies Questions & Answers

Remove text in particular delimiter

I have input file like this 551|552|553|554|555|556|557|558|559|560 I need any one offset need to be blank for eg. 551|552|553||555|556|557|558|559|560 My Shell is csh (1 Reply)
Discussion started by: nsuresh316
1 Replies

6. UNIX for Dummies Questions & Answers

LINUX - How to remove the final delimiter from a command output

Hi All, I am trying to list the various dates for which the file is available in a directory using the command below, (& subsequently pass the command output to a loop) Command : ls dir|grep 'filename'|cut -d '_' -f1|cut -c1-8|tr '\n' ',' However, it is giving me an extra comma... (6 Replies)
Discussion started by: dsfreddie
6 Replies

7. Shell Programming and Scripting

remove delimiter

hy all, i need case with input like this 1::||10334|11751| 2::10324|17541||| i want output like this 1::||1033411751| 2::1032417541||| how i can do that for get like output thx before your advice (3 Replies)
Discussion started by: zvtral
3 Replies

8. Shell Programming and Scripting

replace spaces/tabs with delimiter |

Hi, I'm looking for a command that replaces spaces/tabs with pipe symbol and store the result to the same file instead of routing it to another file. infile outfile Thanks. (11 Replies)
Discussion started by: dvah
11 Replies

9. Shell Programming and Scripting

How to remove delimiter from specific column?

I have 5 column in sample txt file where in i have to create report based upon 1,3 and 5 th column.. I have : in first and third coulmn. But I want to retain the colon of fifth coulmn and remove the colon of first column.. 5th column contains String message (for example,... (7 Replies)
Discussion started by: Shirisha
7 Replies

10. UNIX for Dummies Questions & Answers

Remove Trailing spaces after a delimiter

Hi, I am trying to remove trailing white spaces using this command in awk nawk -F '|' '/^TR/{t = $4 }/^LN/{gsub(/ */,"");printf "%s|%s\n", t, $0 }' $i>>catman_852_files.txt My delimiter is '|'. THere are some description fields which are being truncated. I dont want to remove spaces... (1 Reply)
Discussion started by: kiran_418
1 Replies
Login or Register to Ask a Question