how to arrange all lines in a file to a single line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to arrange all lines in a file to a single line
# 1  
Old 01-26-2009
how to arrange all lines in a file to a single line

Hi Gurus,

I am a starter with shell script. Help me to achieve this. I have a file with lines given below.

line1
line2
line3
.
.
etc

How can I arrange the file which should look like
line1,line2,line3,..,..,etc

Any help on this is appreciated.
# 2  
Old 01-26-2009
Try this:

Code:
cat file | while read a
do
echo "$a,\c"
done

# 3  
Old 01-26-2009
Hammer & Screwdriver This might work for you

Code:
> cat file145
ColA ColB
1 2
2 x
3 y
4 c
5 q

> tr "\n" "," <file145 ; echo
ColA ColB,1 2,2 x,3 y,4 c,5 q,

# 4  
Old 01-26-2009
Hi,

You can also do it like this:


Code:
tr "\n" "," < filename


/Lakris

It means "translate all newline to comma in the file filename"

Last edited by Lakris; 01-26-2009 at 04:24 PM.. Reason: clarification
# 5  
Old 02-18-2009
Thanks for the support.
I am able to do with options given by Joeyg and Lakris. The option given by Bluescreen make every line to print with additional ", \c" at the end.

Also I require help on the output. Like if the content of a file is given below

ABC
ACB
ADB
BCD
BDC
BEF

How to arrange the file in below given pattern?
ABC,ACB,ADB
BCD,BDC,BEF

Any help on this will be appreciated.
# 6  
Old 02-18-2009
Hope this will do

awk '{ if(substr($0,1,1) != prev_char)
{ printf("\n%s",$0); prev_char=substr($0,1,1); }
else printf(",%s",$0);
}' filename

Regards

Ranjith P R
# 7  
Old 02-18-2009
Another approach:

Code:
awk '{ORS=!(NR%3)?"\n":","}1' file

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting - need to arrange the columns from multiple file into a single file

Hi friends please help me on below, i have 5 files like below file1 is x 10 y 20 z 15 file2 is x 100 z 245 file3 is y 78 z 23 file4 is x 100 (3 Replies)
Discussion started by: siva kumar
3 Replies

2. Shell Programming and Scripting

Replacing a single line with multiple lines in a file

Hi Am confused with the usage of "sed" command I want to replace a single line with multiple lines of a file.. eg., A file has Hi, How are you? I need to replace as Am fine What are You doing? I used the script as string1="Hi, How are you?" echo "$string1 is the value"... (4 Replies)
Discussion started by: Priya Amaresh
4 Replies

3. Shell Programming and Scripting

joining multi-line file into single lines

Hi, I have a file like mentioned below..For each specific id starting with > I want to join the sequence in multiple lines to a single line..Is there a simple way in awk or sed to do this >ENST00000558922 cdna:KNOWN TCCAGGATCCAGCCTCCCGATCACCGCGCTAGTCCTCGCCCTGCCTGGGCTTCCCCAGAG... (2 Replies)
Discussion started by: Diya123
2 Replies

4. Shell Programming and Scripting

How to arrange xml tags in single row using shell script?

I want to put one xml record in one row and so on... sample two records are here. <?xml version="1.0"?> <Object> <Header> <XCOMVers>V1.0</XCOMVers> <REPORT>XXXXX</REPORT> <CODE>002</CODE> </Header> <IssueCard> <Record> <L>CAR SYSTEM -SSSSS -</L> ... (3 Replies)
Discussion started by: sene_geet
3 Replies

5. Shell Programming and Scripting

How to arrange xml tags in single row using shell script?

I want to put one xml record in one row and so on... sample two records are here. <?xml version="1.0"?> <Object> <Header> <XCOMVers>V1.0</XCOMVers> <REPORT>XXXXX</REPORT> <CODE>002</CODE> </Header> <IssueCard> <Record> <L>CAR SYSTEM -SSSSS -</L> ... (1 Reply)
Discussion started by: sene_geet
1 Replies

6. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

7. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

8. Shell Programming and Scripting

split single line into two line or three lines

Dear All, I want to split single line into two line or three lines wherever “|” separated values comes using Input line test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087, output shoule be test,DEMTEMPUT20100404010012,,,,,,,,0070086, test,DEMTEMPUT20100404010012,,,,,,,,0070087, (14 Replies)
Discussion started by: arvindng
14 Replies

9. Shell Programming and Scripting

Break lines up into single lines after each space in every line

It sounds a bit confusing but what I have is a text file like the example below (without the Line1, Line2, Line3 etc. of course) and I want to move every group of characters into a new line after each space. Example of text file; line1 .digg-widget-theme2 ul { background: rgb(0, 0, 0) none... (7 Replies)
Discussion started by: lewk
7 Replies

10. Shell Programming and Scripting

Edit number of lines in a file to single line

Greetings, I have a file: hostnames.txt which has - # cat hostnames.txt machine1 machine2 I need the output to be saved to a variable as: HOSTNAMELIST=machine1,machine2 Please advise. Thanks, Chiru (3 Replies)
Discussion started by: chiru_h
3 Replies
Login or Register to Ask a Question