Solved

New Line Character search in a text file

Posted on 2013-06-18
6
413 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 79

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 79

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 26

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

777 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