Remove whitespaces between delimiter and characters

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Remove whitespaces between delimiter and characters
# 1  
Old 04-09-2018
Remove whitespaces between delimiter and characters

Hello All,

I have a pipe delimited file and below is a sample data how it looks:
Code:
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 is to replace the spaces using the below rule:

1. If the data field between delimiters doesn't have value then do nothing.
2. If the data field between delimiters have just spaces then replace the field content with 0
3. Remove extra trailing/leading spaces before and after any numeric/character data. For eg,
Code:
| 5897 | QXCYN87876

looks like
Code:
|5897|QXCYN87876

Any help is appreciated.
# 2  
Old 04-09-2018
Try:-
Code:
sed 's#\([|]\)\([ ]*\)\([|]\)#\10\2#;s#\([ ]*\)\([^ ]*\)\([ ]*\)#\2#g' file

# 3  
Old 04-09-2018
Hi Yoda,

Thanks. But the output actually looks like:
Code:
CS123|0||5897|QXCYN87876

As it displays, there is a missing data field |0| :- CS123|0|0|
Also, if the text field contains say QXCYN87876 some space then text then the code removes the spaces between the texts also and the result looks like:
Code:
CS123|0||5897|QXCYN87876somespacethentext

Sorry to have missed out in the earlier requirement.
Would be great if you can suggest.

Last edited by amvip; 04-09-2018 at 11:36 PM.. Reason: More observations
# 4  
Old 04-10-2018
Like so?
Code:
sed ':L; s/| \+|/|0|/; tL; s/ *| */|/g' file
CS123|0|0||5897|QXCYN87876 some text

# 5  
Old 04-11-2018
Hi amvip,
Your requirements are still a little bit ambiguous...

If there are one or more spaces before the 1st pipe symbol or after the last pipe symbol and there are no characters other than spaces in those fields; are those spaces supposed to be removed, are they supposed to be changed to a single zero character, or are they to be left unchanged? Of your three rules, the first two rules do not apply because the data field is not "between delimiters" and the third rule doesn't apply because there are no characters in the field other than spaces.

The code RudiC suggested in post #4 will turn leading and trailing fields containing only spaces into empty fields. Is that what you want?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. Shell Programming and Scripting

Remove whitespaces in the n first characters?

I assume removing whitespaces in the n first characters of a string would be an easy task for sed? If so, how? (7 Replies)
Discussion started by: KidCactus
7 Replies

6. Shell Programming and Scripting

Multiple characters including single quote in delimiter

Hello, I need to replace the comma to something else between the single quote: 1aaa,bbb,'cc,cc','ddd',1 2aaa,bbb,'ccc','d,d',0 to 1aaa,bbb,'cc<comma>cc','ddd',1 2aaa,bbb,'ccc','d<comma>d',0 Can someone help? Thanks. (2 Replies)
Discussion started by: bgirl
2 Replies

7. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: SRyan84
5 Replies

8. UNIX for Dummies Questions & Answers

Remove whitespaces between comma separated fields from file

Hello all, I am a unix dummy. I am trying to remove spaces between fields. I have the file in the following format 12332432, 2345 , asdfsdf ,100216 , 9999999 12332431, 2341 , asdfsd2 ,100213 , 9999999 &... (2 Replies)
Discussion started by: nitinbjoshi
2 Replies

9. Shell Programming and Scripting

Formatting a text file based on newline and delimiter characters

Hi Everybody, I need some help on formatting the files coming into unix box on the fly. I get a file some thing like this in a single line. ISA^M00^M ^M00^M ^M14^M006929681900 ^M01^M095449419 ... (5 Replies)
Discussion started by: ntekupal
5 Replies

10. Shell Programming and Scripting

help me to remove the whitespaces between the lines ...urgent

Hello all, i have a problem. please help me to remove the white spaces and tabs betweeen line. i.e., file1 contains some text.. text starts_hdsffdsd sdfsddssdds******** sdfsdsd*********** sdfsdsdfsdsdfsdsds*** ****fsd_test_ends one or 2 blank lines (* indicates white spaces or tabs) ... (5 Replies)
Discussion started by: kumar1
5 Replies
Login or Register to Ask a Question