LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Removing new lines from a file (https://www.linuxquestions.org/questions/programming-9/removing-new-lines-from-a-file-539248/)

psandeepnair1985 03-21-2007 04:22 AM

Removing new lines from a file
 
Can any body tell me whether there is any command in bash shell that removes new line(i.e"\n") from a file

IBall 03-21-2007 04:25 AM

You need to give us more information if you want a decent response.

This can be done with tools like sed, or you could write a script in perl.

I hope this isn't homework ;)

--Ian

Nick_Battle 03-21-2007 04:48 AM

This thread might help:
http://www.linuxquestions.org/questi...d.php?t=530376

varun_shrivastava 03-21-2007 11:28 PM

sed -n 'H;${g;s/\n/ /g;p}'

cfaj 03-22-2007 12:50 PM

Quote:

Originally Posted by psandeepnair1985
Can any body tell me whether there is any command in bash shell that removes new line(i.e"\n") from a file

Code:

tr -d '\012' < FILE
If you want a purely bash solution:

Code:

NL=$'\n'
file=$( < FILE )
printf "%s" "${file//"$NL"/}"


sundialsvcs 03-25-2007 11:46 AM

For this general class of problems, there are three Unix/Linux tools that you need to be aware of.

(a) sed ("stream editor") is a tool that can take an input stream (file...), apply a filter or change to it, and write it to an output stream. This tool is usually used with the "|" (pipe) operator in a command.

(b) grep ("regular expressions") is a great tool for finding files and for extracting useful pieces from those files.

(c) awk is a very powerful tool for ripping a file apart, record-by-record and field-by-field, and doing very sophisticated things with them.

Obviously, above-and-beyond this, programming languages like perl and python feature hundreds of pre-built packages of existing code for doing these things and more.

Important Tip... In the Unix/Linux world, there are many ways to do just about anything, and most if not all of them have already been done. Usually, when you think about "building" a solution, you just need to "find" one.
Quote:

Originally Posted by a wise and very lazy sage:

Dictum ne agas -- Do not do a thing already done.



All times are GMT -5. The time now is 04:11 AM.