Solved

New Line Character search in a text file

Posted on 2013-06-18
6
417 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 80

Accepted Solution

by:
David Johnson, CD, MVP earned 250 total points
ID: 39258404
\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
ID: 39258467
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 80

Expert Comment

by:David Johnson, CD, MVP
ID: 39258468
just need to open it in one format and save it in the desired format.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 250 total points
ID: 39258602
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
ID: 39259436
And you could check wether the "dos2unix" command exists on your linux box and gives the desired result

dos2unix yourfile newfile
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 39292370

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

820 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