LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-06-2007, 02:56 AM   #1
jillann
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Rep: Reputation: 0
Question 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.
 
Old 03-06-2007, 03:07 AM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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).
 
Old 03-06-2007, 03:09 AM   #3
AwesomeMachine
Senior Member
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian jessie/sid; OpenSuSE; Fedora
Posts: 1,591

Rep: Reputation: 162Reputation: 162
tr<space>filename<space>'<space>','

But don't actually type <space>. Put in regular spaces.
 
Old 03-06-2007, 03:17 AM   #4
jillann
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Original Poster
Rep: Reputation: 0
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?
 
Old 03-06-2007, 03:17 AM   #5
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.

Last edited by timmeke; 03-06-2007 at 03:19 AM.
 
Old 03-06-2007, 03:54 AM   #6
jillann
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Unhappy 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
 
Old 03-06-2007, 04:17 AM   #7
sohny
Member
 
Registered: Aug 2004
Location: bangalore
Distribution: Redhat,Ubuntu
Posts: 64

Rep: Reputation: 15
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
 
Old 03-06-2007, 05:13 AM   #8
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.
 
Old 03-06-2007, 05:32 AM   #9
AwesomeMachine
Senior Member
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian jessie/sid; OpenSuSE; Fedora
Posts: 1,591

Rep: Reputation: 162Reputation: 162
tr '\n' ',' < filename.txt > filename1.txt
 
Old 03-06-2007, 06:42 PM   #10
jillann
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Original Poster
Rep: Reputation: 0
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
 
Old 03-06-2007, 08:24 PM   #11
Sepero
Member
 
Registered: Jul 2004
Location: Tampa, Florida, USA
Distribution: Ubuntu
Posts: 734
Blog Entries: 1

Rep: Reputation: 31
cat "file.txt" | while read i; do echo -n "$i,"; done > newfile.txt
 
Old 03-06-2007, 08:38 PM   #12
Sepero
Member
 
Registered: Jul 2004
Location: Tampa, Florida, USA
Distribution: Ubuntu
Posts: 734
Blog Entries: 1

Rep: Reputation: 31
Quote:
Originally Posted by AwesomeMachine
tr '\n' ',' < filename.txt > filename1.txt
That command seems to work fine for me too.
 
Old 03-06-2007, 09:23 PM   #13
jillann
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Talking 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!
 
Old 03-06-2007, 11:34 PM   #14
Sepero
Member
 
Registered: Jul 2004
Location: Tampa, Florida, USA
Distribution: Ubuntu
Posts: 734
Blog Entries: 1

Rep: Reputation: 31
I would've told you that originally, except you said you needed this:
"36,dsf,sdf,dsfs" (With commas)
 
Old 03-07-2007, 01:09 AM   #15
jillann
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Smile

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
 
  


Reply

Tags
line


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
turn a script to a service gsr_kashyap Linux - Software 1 04-05-2006 02:31 AM
Share your greatest one-liner!! PenguinPwrdBox General 3 08-20-2005 10:45 PM
How do you turn an executable into a shell script? Thaidog Linux - Newbie 1 09-20-2004 07:20 PM
one-liner to see current internet bandwidth usage doorbits Debian 6 11-01-2003 02:10 PM
one liner needed for search and replace chr15t0 Linux - General 2 09-24-2002 10:00 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration