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:
Quote:
|
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 |
tr<space>filename<space>'<space>','
But don't actually type <space>. Put in regular spaces. |
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? |
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" "," |
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 |
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 |
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. |
tr '\n' ',' < filename.txt > filename1.txt
|
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 :( |
cat "file.txt" | while read i; do echo -n "$i,"; done > newfile.txt
|
Quote:
|
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 I have learned something new. hehehehe Thanks for all your help! :) |
I would've told you that originally, except you said you needed this:
"36,dsf,sdf,dsfs" (With commas) |
Quote:
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. |