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

Replacing end of line character with another

Status
Not open for further replies.

richardii

Programmer
Jan 8, 2001
104
GB
How can I replace every end of line character in my input file with another character?

I tried gsub(\n,"cChar"), and gsub(/\n/,"cChar")

Nothing seems to work!
 
Not sure if you want to remove the EOF character or not. Does this do what you want?

sed 's/$/X/g' filename

Greg.
 
Thanks - but I was hoping to use GNU awk, I used to have something called wsed (I'm using a pc). Now I can't find it....
 
OK, is this any good?

gawk '{print $0"X"}' filename

Greg.
 

richardii, hello!


A few words about end-of-line character

Unix text file marks the end of the line with newline character ('\n').
DOS text file has a two-character sequence, carriage return and newline ('\r\n') at the end of the line.

For example Unix text file

1st row
2nd row

would be stored as a character sequence

1st row\n 2nd row\n


Text tools (like vi and awk) handle automaticaly with end-of-line character (abbr. EOL).

To replace EOL you can:
(1) join two lines, for example (see Unix text file example)

awk code:
{ line = line $0; print line }

result:
1 st row
1 st row 2nd row

(2) add character after EOL-character

awk code:
{ print $0 "X" }

result:
1 st rowX
2nd rowX


I hope this helps.

Bye!

KP.
 
Thank you for all your help. Following from your comments I have found that:

BEGIN{ORS=""}

{print $0}

will do what I want.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top