Solved

New Line Character search in a text file

Posted on 2013-06-18
6
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 81

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 81

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

695 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