Operating System - HP-UX
1842162 Members
2722 Online
110187 Solutions
New Discussion

Re: trimming a large file

 
SOLVED
Go to solution
u856100
Frequent Advisor

trimming a large file

people,

I am attempting to trim quite a large file.
the file has entries like the following :

1234|abcd|defg|one
5678|abcdefg|1|6

etc.....

but because the file was created in SQLPLUS using a large column format, there are lots of whitespaces after the last pipedelimitted field. What I was trying to do is trim this so all that remains is the pipe del'd fields.

I have asked a similar question before regarding trimming from both ends of the data entry, but this time I am after just trimming whitespace off the end. If anyone has previously answered to that one, then please bare with me as I will eventually assign points.

thanks a bunch
John
chicken or egg first?
11 REPLIES 11
harry d brown jr
Honored Contributor
Solution

Re: trimming a large file


sed "s/\( \)*$//g"


live free or die
harry
Live Free or Die
James R. Ferguson
Acclaimed Contributor

Re: trimming a large file

Hi John:

Since whitespace can be spaces and/or tabs:

# sed -e 's/[ \t]*$//' filename

...note: Actually type a TAB character instead of the \t since many 'sed's will not honor it.

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: trimming a large file

# delete leading whitespace (spaces, tabs) from front of each line
# aligns all text flush left
sed 's/^[ \t]*//'
# delete trailing whitespace (spaces, tabs) from end of each line
sed 's/[ \t]*$//'
# delete BOTH leading and trailing whitespace from each line
sed 's/^[ \t]*//;s/[ \t]*$//'


Does that help?

Pete

Pete
u856100
Frequent Advisor

Re: trimming a large file

Thanks everyone,

Pete, I will gladly have your babies! superb!

thanks again
John
chicken or egg first?
Chuck J
Valued Contributor

Re: trimming a large file

How about assigning them some points?!

Chuck J
harry d brown jr
Honored Contributor

Re: trimming a large file


And of course we can use another regexp to remove just trailing "whitespace"
sed "s/\([[:space:]]*$\)//"

so many ways to wok the cat

live free or die
harry
Live Free or Die
Robin Wakefield
Honored Contributor

Re: trimming a large file

Hi John,

perl -ple '{s/\s*$//}' filename

Rgds, Robin
Pete Randall
Outstanding Contributor

Re: trimming a large file

Jeez, John - thanks a lot (I guess) but I really don't need any more babies running around. I've got four mostly grown kids who thank I'm the unemployment office/low income housing agency. I have to admit that my posting was an excerpt that (I think) I picked up from Princess Paula (if it wasn't Paula I should be shot for forgetting). I'll post the whole thing for your enlightenment when I get to work tomorrow.

Pete

Pete
Pete Randall
Outstanding Contributor

Re: trimming a large file

Sorry - think (not thank).

Pete
Pete Randall
Outstanding Contributor

Re: trimming a large file

Here's the complete "Sed One-Liners".

Pete

Pete
harry d brown jr
Honored Contributor

Re: trimming a large file

Pete,

That is a great list of "compiled" sed statements! Thanks!

BTW, My ex-wife thinks I'm the World Bank - ready to give money to those that don't need it or can't pay it back. As for my kids, the two oldest (still in high school) have to work part time for their car and insurance.

live free or die
harry
Live Free or Die