LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to turn a file to a one liner thru sh script? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-turn-a-file-to-a-one-liner-thru-sh-script-534977/)

jillann 03-06-2007 02:56 AM

How to turn a file to a one liner thru sh script?
 
Good day to all!

Please help me. I wish to turn a file into a one liner using shell script.

For example, my file is below:

Quote:

36
dsf
sdf
dsfs
Is there a command in shell that would turn above file to this:

Quote:

36,dsf,sdf,dsfs
Thanks to all who would help. :)

timmeke 03-06-2007 03:07 AM

Yes, you can use awk, sed or tr to do this.

If it's a *nix format text file (ie only \n as line terminator), then you can do:
Code:

tr -s "\n" "," your_file.txt
The -s option will eliminate empty lines (which should otherwise give you ",," in the result).

AwesomeMachine 03-06-2007 03:09 AM

tr<space>filename<space>'<space>','

But don't actually type <space>. Put in regular spaces.

jillann 03-06-2007 03:17 AM

Hi, I am trying tr right now. But it is taking a long time for the command to complete. :(
The file only has 37 lines... How would I know if it is working?

Can't sed be used? or awk maybe?

timmeke 03-06-2007 03:17 AM

Sorry, the file should be given to stdin of tr, rather than being passed as a command line parameter.
Code:

cat your_file.txt | tr -s "\n" ","
#or
tr -s "\n" "," < your_file.txt

Your running tr is probably waiting for some lines of input on stdin. Just use Ctrl-D (end of input character) to finish it.

jillann 03-06-2007 03:54 AM

still not working
 
Hi,

I have tried your suggestion. It now executes but the next line was not converted to "," Is it because tr executes only one line a time same as sed?

Thanks so much for helping out

sohny 03-06-2007 04:17 AM

i assume u may have to give the -g option right after -s like this

tr -s -g "\n" "," < your_file.txt

#or#

tr -sg "\n" "," < your_file.txt

timmeke 03-06-2007 05:13 AM

There's no -g (or "global") option for tr as there is in sed. This "g" option is used in sed's tr or y transliteration operators.

The tr utility also works on all lines at once, not just the first one.
And my command does work for me (on a short example text file).

Have you checked if your text format is DOS-like (\n\r terminated) or *nix-like?
Maybe you should first run "dos2unix your_file" if it's in DOS text format.

AwesomeMachine 03-06-2007 05:32 AM

tr '\n' ',' < filename.txt > filename1.txt

jillann 03-06-2007 06:42 PM

http://www.freewebs.com/jill_ann/active%5Fmachines

Here's the link of the file I am trying to turn into one line... I'm so desperate :(

Sepero 03-06-2007 08:24 PM

cat "file.txt" | while read i; do echo -n "$i,"; done > newfile.txt

Sepero 03-06-2007 08:38 PM

Quote:

Originally Posted by AwesomeMachine
tr '\n' ',' < filename.txt > filename1.txt

That command seems to work fine for me too.

jillann 03-06-2007 09:23 PM

Got It!
 
Hi all! Finally made it work!

Got the info from this site: http://ka1fsb.home.att.net/shfio.html

Code:

#----- The file to read
        myFile="/root/somefile"

#----- The "big" data variable
        myData=""

#----- Now the read
        myData=`cat $myFile`

#----- Show that the data is really in the variable...
#----- This is in the same format as the orginal file, new lines preserved
        echo "$myData"

#----- Show the data in non-quoted format, the space becomes the separator
        echo $myData

All I needed was $myData

I have learned something new. hehehehe
Thanks for all your help! :)

Sepero 03-06-2007 11:34 PM

I would've told you that originally, except you said you needed this:
"36,dsf,sdf,dsfs" (With commas)

jillann 03-07-2007 01:09 AM

Quote:

Originally Posted by Sepero
I would've told you that originally, except you said you needed this:
"36,dsf,sdf,dsfs" (With commas)

I am sorry to have confused you... My main goal was really to have the content of the file be in just one line (with or without the comma)

I prefer replacing \n with commas, however (for reasons that I do not know of) tr does not work in my code. I really dont understand why.

Anyways, thanks to all who had helped :)


All times are GMT -5. The time now is 02:38 AM.