Removing whitespace issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing whitespace issue
# 1  
Old 10-22-2010
Question Removing whitespace issue

Hi,

I have a file with rows like below delimited with pipe (|)
Quote:
LT4677582LN |ABC |12 erttt | 345345.0000|
I want to remove all the leading and trailing white space from each and every fields keeping the delimiter intact.

I have tired this
Code:
sed 's/[  ]*//g;s/[  ]*$//g'

but the result is incorrect
Quote:
LT4677582LN|ABC|12erttt|345345|
it is removing a whitespace from field "12 erttt".

I just want to remove the leading and trailing white space from all the fields.

Thanks,

Last edited by COD4; 10-22-2010 at 11:47 AM.. Reason: Incorrect result at the end
# 2  
Old 10-22-2010
Code:
sed 's:[ ]*|[ ]*:|:g' input > output


sed 's/[ ]*//g;s/[ ]*$//g' your code is not correct :

s/[ ]*//g this would remove any number of successive blank space g = globally so the pattern is replaced even if appearing many times in the same line ...

... including when it appear at the enf of line ! ... so the next s/[ ]*$//g is useless since your previous command would already have removed all space found wherever they appear in the line

... unfortunately including in a middle of a field

What you need is just to remove the space that are next to the | delimiter (see my code)

you could also write it
Code:
sed 's/[ ]*|[ ]*/|/g' input > output


Last edited by ctsgnb; 10-22-2010 at 12:08 PM..
# 3  
Old 10-22-2010
Hi,

Another one:

Inside brackets there are two characters: one space and one tab.
Code:
sed 's/|[     ]*/|/g;s/[      ]*|/|/g' infile

Regards,
Birei
# 4  
Old 10-22-2010
Quote:
Originally Posted by birei
Hi,

Another one:

Inside brackets there are two characters: one space and one tab.
Code:
sed 's/|[     ]*/|/g;s/[      ]*|/|/g' infile

Regards,
Birei
You can gather it in one substitution command :
Code:
sed 's/[     ]*|[     ]*/|/g' infile

This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 10-22-2010
Code:
$ ruby -ne 'puts $_.strip' file

# 6  
Old 10-23-2010
Full solution

Code:
$  awk 'BEGIN{FS=OFS="|"}{for (i=1;i<=NF;i++) {sub(/^ */,"",$i);sub(/ *$|\..*$/,"",$i)}}1' infile

LT4677582LN|ABC|12 erttt|345345|

# 7  
Old 10-23-2010
Code:
sed 's/ *| */|/g' input > output

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

adding whitespace

Hi guys, I am working with large data sets and often times realize that not all of the columns are aligned correctly (sometimes rows will be shifted). So when I try to do something like: awk '{ if ($2 > 30 && $5 == $3){print}}' file > output it won't really work since some of the rows... (2 Replies)
Discussion started by: verse123
2 Replies

2. Homework & Coursework Questions

Issue with tr, removing [:alnum:]

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: You should also know how to find files based on their characteristics. Use stat to list the meta data... (1 Reply)
Discussion started by: alindner
1 Replies

3. Shell Programming and Scripting

Getting rid of whitespace

Hello I am working aon script, that tells me how many users or on the system when i run it. The script is #!/bin/bash w | cut -f 1 -d ' ' |sort -u | wc -l When ran it shows 16 users including myself and a line of white space. I was wondering what I need to add to remove my user... (2 Replies)
Discussion started by: mosdojaf
2 Replies

4. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

5. UNIX for Advanced & Expert Users

whitespace problem

I have a single string as below: Rat run after Cat i.e. there is a single whitespace after Cat. This causes my file to fail. Is there a way I can remove any whitespace at the end of any string. I tried sed 's/ *//g', but it removes all white space and the above string becomes... (10 Replies)
Discussion started by: RubinPat
10 Replies

6. Shell Programming and Scripting

Whitespace Issues

Hello forums! I've been tinkering with a shell script to partition and restore content to a drive based on a type of file in a given directory. My goal is for my script to assemble several restore images, partition the drive based on the images and to then restore those images to the partitions... (1 Reply)
Discussion started by: rkasowan
1 Replies

7. UNIX for Advanced & Expert Users

Issue with Removing Carriage Return (^M) in delimited file

Hi - I tried to remove ^M in a delimited file using "tr -d "\r" and "sed 's/^M//g'", but it does not work quite well. While the ^M is removed, the format of the record is still cut in half, like a,b, c c,d,e The delimited file is generated using sh script by outputing a SQL query result to... (7 Replies)
Discussion started by: sirahc
7 Replies

8. Shell Programming and Scripting

removing whitespace from middle of file -help

I have a file in which I clean out a bunch of nonsense text as well as path information. What I end up with is something like the following: johnson.........................................................933 Where the periods represent the whitespace The file comes out originally with... (2 Replies)
Discussion started by: roninuta
2 Replies

9. Shell Programming and Scripting

trim whitespace?

I'm trying to find a command that will trim the white space off a string. e.g. $str = " stuf " $str = trim ( $str ) echo $str // ouput would just be stuf Thanks, Mark (4 Replies)
Discussion started by: msteudel
4 Replies

10. Shell Programming and Scripting

Removing whitespace from files

Hello, I was wondering if there was a way to write a script to do the following: turn a file that contains: 1234 Kevin Smith 12:09 456235 1234 John Robger 12:09:09 353657 into: 1234%Kevin%Smith%12:09%456235... (10 Replies)
Discussion started by: kevin80
10 Replies
Login or Register to Ask a Question