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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove newline character from a variable

Status
Not open for further replies.

umeman

Technical User
Oct 2, 2001
59
US
Admins,

I have a variable, $TESTVAR that has a '\n' character in it
how can I remove all space characters and new line characters on either side of this variable value.

Thanks
 
Do you man by what method can you remove them? As in editing the file with
vi and deleting the characters with an x and then saving with wq. Can you be more specific please?
 
You can do this with sed, but you may have to know how many newline chars there are. For example:
$ T1="this \nis a \ntest"
$ echo $T1 | sed '{
N
N
s/\n//g
s/ //g
}'

produces: thisisatest

The sed script says: cancatenate the next two lines (the N's), then replace newlines with nothing, then replace spaces with nothing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top