?
Solved

New Line Character search in a text file

Posted on 2013-06-18
6
Medium Priority
?
438 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 82

Accepted Solution

by:
David Johnson, CD, MVP earned 1000 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 82

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 1000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
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.
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 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