• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 52
  • Last Modified:

unix replace line feeds

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

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
1 Solution
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Unsure if this is exactly what you're after. If you do the above, then you may end up with problems.

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.
here is how to achieve the first : change occurrences of LF into CRLF
sed 's/\x0A/\r\n/g'

Open in new window


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
mruffAuthor Commented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
Try this:
  cat myfile.txt | sed -e 's/WHATTOREPLACE/REPLACEINTO/g' > mynewfile.txt
  cat myfile.txt | sed -e 's/\x0A/\r\n/g' > newfile.txt
mruffAuthor Commented:
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.
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
In Unix or in C language:
CR                 \r                    short for "r"eturn
LF                  \n                   short for "n"ew line
CRLF             \r\n
mruffAuthor Commented:
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
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
I'm sorry to disappoint you but there is no NL in ASCII on Unix, this is an EBCDIC character.
decimal 21 or hex 15 is the NAK character.

Check your man page "ascii" on your unix brand (usually in section 7):
  man ascii
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now