mruff
asked on
unix replace line feeds
Hi
with a unix command I want to replace all occurrences in a text file
1. LF by CRLF
2. LF by NL
3. CRLF by NL
5. NL by LF
Where
Abbreviation hexvalue decvalue Escape sequence
LF 0A 10 \n
CR LF 0D 0A 13 10 \r\n
CR 0D 13 \r
RS 1E 30
LF+CR 0A 0D 10 13 \n\r
NL 15 21 \025
Many thanks
with a unix command I want to replace all occurrences in a text file
1. LF by CRLF
2. LF by NL
3. CRLF by NL
5. NL by LF
Where
Abbreviation hexvalue decvalue Escape sequence
LF 0A 10 \n
CR LF 0D 0A 13 10 \r\n
CR 0D 13 \r
RS 1E 30
LF+CR 0A 0D 10 13 \n\r
NL 15 21 \025
Many thanks
here is how to achieve the first : change occurrences of LF into CRLF
basically it's sed s/TRANSFORMWHAT/TRANSFORMI NTO/
you can easily extrapolate the others given you have the hex codes ( which you can also use in the transform into part )
as a side note, dos2unix unix2dos tofrodos can also be helpful
sed 's/\x0A/\r\n/g'
basically it's sed s/TRANSFORMWHAT/TRANSFORMI
you can easily extrapolate the others given you have the hex codes ( which you can also use in the transform into part )
as a side note, dos2unix unix2dos tofrodos can also be helpful
ASKER
Hi thx
How do I replace LF by NL, I've tried
sed -i ':a;N;$!ba;s/\n/x15/g' myfile.txt
sed -i ':a;N;$!ba;s/\n/\025/g' myfile.txt
none of them worked
thx
How do I replace LF by NL, I've tried
sed -i ':a;N;$!ba;s/\n/x15/g' myfile.txt
sed -i ':a;N;$!ba;s/\n/\025/g' myfile.txt
none of them worked
thx
Try this:
cat myfile.txt | sed -e 's/WHATTOREPLACE/REPLACEIN TO/g' > mynewfile.txt
e.g.
cat myfile.txt | sed -e 's/\x0A/\r\n/g' > newfile.txt
cat myfile.txt | sed -e 's/WHATTOREPLACE/REPLACEIN
e.g.
cat myfile.txt | sed -e 's/\x0A/\r\n/g' > newfile.txt
ASKER
Hi Hanno
Thx yes replacing LF by CRLF that works
BUT replacing LF by NL does not work, I tried to state LN by it's hex and escape sequence, x15 in this case x15 was written to the result file, and by \025 and in this cas 025 was written to the output file, instead of the NL char.
Thx yes replacing LF by CRLF that works
BUT replacing LF by NL does not work, I tried to state LN by it's hex and escape sequence, x15 in this case x15 was written to the result file, and by \025 and in this cas 025 was written to the output file, instead of the NL char.
In Unix or in C language:
CR \r short for "r"eturn
LF \n short for "n"ew line
CRLF \r\n
CR \r short for "r"eturn
LF \n short for "n"ew line
CRLF \r\n
ASKER
Hi Hanno
I know, I was just want to replace LF by NL *hex x15, escape sequence \025
NOT by LF or CRLF this works
I know, I was just want to replace LF by NL *hex x15, escape sequence \025
NOT by LF or CRLF this works
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
LF -> CRLF (#1) -> NL -> (#3) -> LF (#5)
So the above setup of transforms, converts LF back to LF.
Maybe clarify a bit more what you're trying to do... or maybe you're saying to only apply one transform per line + if the transform occurs, then abandon any further transforms.