Solved

New Line Character search in a text file

Posted on 2013-06-18
6
406 Views
Last Modified: 2014-08-25
How can I highlight a "new line" characters in a text file? Is there a tool I could use to search and replace '\n' so that I can process data.
0
Comment
Question by:mohammadzahid
6 Comments
 
LVL 78

Accepted Solution

by:
David Johnson, CD, MVP earned 250 total points
Comment Utility
\n = 0x0a

a text editor will not display it, you will need a binary editor.

You could use notepad ++ to change the format from msdos <> unix format.

What do you want to change the \n to ?

You could make a simple C program to do the change for you.
0
 
LVL 11

Author Comment

by:mohammadzahid
Comment Utility
Thanks for providing a solution. '\n' appear in a data file that is in text format. When I moved the file to a linux server from a windows workstation, error occured in Python script that processes the file and performs data calculation.

I will examine the file tomorrow morning using Notepad++. Will update this thread soon. Does Notepad++ have search and replace function? Thanks again.
0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
just need to open it in one format and save it in the desired format.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 250 total points
Comment Utility
I asssume that with "new line" you mean "carriage return" (\r) instead of "line feed" (\n).

Line feed (\n) is the standard line break character on Linux and must stay in place if you want to have "lines" in your text file.

If I'm right with the above assumption -  to remove "\r" from the line ends just do (on Linux):

CR=$(printf "\r")
sed "s/$CR\$//" file.txt > file.unix.txt

Or, to change in place:

CR=$(printf "\r")
sed -i "s/$CR\$//" file.txt

By the way, if you used FTP to transfer the file to Linux it would have been sufficient to use "binary" transfer mode.

And "cat -v file.txt" will show CR ("\r") characters as "^M", no need for a hex editor here.

If you really want to remove "\n" (for what reasons ever) just replace "\r" with "\n" in the respective "printf" statement above.

Finally, if you want to replace "\r" or "\n" with something else throughout the whole file:

OLD=$(printf "\r")
NEW=$(printf "replacement_character")

sed "s/$OLD/$NEW/g" file.txt > file.new.txt
or
sed -i "s/$OLD/$NEW/g" file.txt
0
 
LVL 20

Expert Comment

by:flow01
Comment Utility
And you could check wether the "dos2unix" command exists on your linux box and gives the desired result

dos2unix yourfile newfile
0
 
LVL 26

Expert Comment

by:skullnobrains
Comment Utility

By the way, if you used FTP to transfer the file to Linux it would have been sufficient to use "binary" transfer mode.

i guess it is is just a typo, but actually, you'd need ascii if you wanted the line endings to be converted

---

on most unix boxes, the more command will display "^M" if i recollect properly when it sees a "\r"

---

if dos2unix is not available, you may find tofrodos instead
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now