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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.