Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to rid off leading and trailing spaces/tabs in each column? 1

Status
Not open for further replies.

ogniemi

Technical User
Nov 7, 2003
1,041
PL

a text file contains columns delimetered with "|" sign.

example:
aaaaaaaa | fffffff | cccc | ddd

how to get rid off empty leading/trailing signs in each column and get in result:

aaaaaaaa|fffffff|cccc|ddd

regards.
 
One way:
Code:
sed 's!^ *!!;s! *| *!|!g;s! *$!!' /path/to/input > output

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
thx but tabs are still kept....

Code:
$ echo "  ssss     |     sss\tsss\t   |ddd   |a"|sed 's!^ *!!;s! *| *!|!g;s! *$!!'
ssss|sss        sss     |ddd|a
$ echo "  ssss     |     sss\tsss\t   |\tddd   |a"|sed 's!^ *!!;s! *| *!|!g;s! *$!!'
ssss|sss        sss     |       ddd|a
$
 
Well, you'll need to specify spaces [red]or tabs[/red] in the sed commands like so

[tt]sed 's!^[ (tab)]*!!;'[/tt]

and so on. Substitute [tt]" *"[/tt] with "[tt][ (tab)]*[/tt]" in PHV's example. For sed, you'll need to type the tab character literally as it doesn't understand \t.

[xyz]* means any number of the characters inside the brackets

HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top