Operating System - HP-UX
1821983 Members
3221 Online
109638 Solutions
New Discussion юеВ

awk - removing whitespace before and after text

 
SOLVED
Go to solution
u856100
Frequent Advisor

awk - removing whitespace before and after text

people,

bit of a dilema with this one.

I have a file that has data similar to the following :

20000003985|| | 32| |KINGS 20000003988|||41||SADLER
20000003991||FLAT 1| 21| |JACKSONS
20000003993|| |DOVE COTTAGE | |HOCKLEY
20000004152|| | 115| |AMBLESIDE
20000004157|||51||FARADAY 20000004158|||20||MILLENNIUM
20000004215|||HAMPTON ||LEICESTERSHIRE
20000004216|||46||BURGESS
20000004218|||65||CROMFORD WAY
20000004220|| | 8| |DUCKWORTH
20000004257| CHEZ SOI | | 27| |HIGH


What I need to do is trim the whitespace(both ends) off all the pipe delimtted fields. For example the row :

20000004257| CHEZ SOI | | 27| |HIGH

should idealy be :

20000004257|CHEZ SOI||27||HIGH

The fields containing the whitespaces are arbitrary (so I would have to check all fields).
I was attempting to do this using an awk statement, but am too inexperienced at awk to figure it out. I am confident I could do it in java, but prefereably via a UNIX script.

eternally grateful
John
chicken or egg first?
7 REPLIES 7
T G Manikandan
Honored Contributor
Solution

Re: awk - removing whitespace before and after text

you can do a

#cat |tr -s "| |" "||"

Leif Halvarsson_2
Honored Contributor

Re: awk - removing whitespace before and after text

Hi,
try with:
cat |tr -d " " >newfile
Ian Dennison_1
Honored Contributor

Re: awk - removing whitespace before and after text

If you are only likely to get one space leading or trailing,....

cat /tmp/file |sed 's/| /|/g' |sed 's/ |/|/g'.

If not, perform this several times until 'grep " |" /tmp/file' and 'grep "| " /tmp/file' returns zero lines.

Cheers, Ian
Building a dumber user
H.Merijn Brand (procura
Honored Contributor

Re: awk - removing whitespace before and after text

/must/ it be awk?

# perl -pe's/\s*\|\s*/\|/g' infile

to trim start and end of line too

# perl -pe's/\s+$//;s/^\s+//;s/\s*\|\s*/\|/g' infile
Enjoy, Have FUN! H.Merijn
Robin Wakefield
Honored Contributor

Re: awk - removing whitespace before and after text

Hi John,

sed -e 's/ *| */|/g' filename

Rgds, Robin
Trever Furnish
Regular Advisor

Re: awk - removing whitespace before and after text

Of note perhaps is that all of the replies using sed deal with a space character but you actually asked about whitespace. Whitespace may include tab characters and others.

The way to match zero or more whitespace characters in perl is: \s*

The way to match zero or more whitespace characters in sed (and grep, vi, ex, more, etc) is: [[:space:]]*

So if you want to strip whitespace before and after the pipes and before and after the text on the line, then you'd use this:

cat file |sed -e 's/^[[:space:]]*//' |sed -e 's/[[:space:]]*$//' |sed -e 's/[[:space:]]*|[[:space:]]*/|/g'

As for awk... well, can't help ya there. :-)
Hockey PUX?
Chuck J
Valued Contributor

Re: awk - removing whitespace before and after text

Procura

Do you dream about writing perl scripts when you sleep? hehe

Chuck J