Merging lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging lines in a file
# 1  
Old 07-14-2008
Merging lines in a file

Hi,

I want to merge the lines starting with a comma symbol with the previous line of the file.

Input :

cat file.txt

name1,name2
,name3,name4
emp1,emp2,emp3
,emp4
,emp5
user1,user2
,user3

Output

name1,name2,name3,name4
emp1,emp2,emp3,emp4,emp5
user1,user2,user3

IS there any command in unix to do this conversion for the entire file?

Mohan
# 2  
Old 07-14-2008
Code:
awk 'END { print r }
r && !/^,/ { print r; r = "" }
{ r = r ? r $0 : $0 }
' file

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 3  
Old 07-15-2008
Merging Lines based on specific occurance of a character

Thanks for the reply. This script works fine.

Is there any way to merge two lines based on specific occurance of a character

I am having a flat file which contains multiple records.
Each row in the file should contain specified number of delimiter.
For a particular row , if the delimiter count is not matched with the specified count, then then next row should be merged with the previous row. Again the same check has to be done.

The script should accept number of occurances of a particular delimiter as the parameter.

For Example if the number of occurances of comma in every line is 5 in a flat file

Sample Input

1,2,3,
4,5,6
a,b,c,d,e,f
10,20,30
,40,50,60
11,22
,33
,44,
55,
66

Output (Each row should contain 5 commas)

1,2,3,4,5,6
a,b,c,d,e,f
10,20,30,40,50,60
11,22,33,44,55,66

Mohan
# 4  
Old 07-15-2008
Set the desired number of columns on the command line (cols= ...),
use nawk or /usr/xpg4/bin/awk on Solaris:
(I feel I'm reinventing the wheel ...)

Code:
awk -F, '{ 
  for (i=1; i<=NF; i++)
    if ($i) 
      printf $i (++c % cols ? FS : RS)
    }
END { 
  if (c % cols) 
    print 
	}' cols=5 input


Last edited by radoulov; 07-15-2008 at 06:08 AM..
# 5  
Old 07-15-2008
another way

Quote:
Originally Posted by mohan_tuty
Hi,

I want to merge the lines starting with a comma symbol with the previous line of the file.

Input :

cat file.txt

name1,name2
,name3,name4
emp1,emp2,emp3
,emp4
,emp5
user1,user2
,user3

Output

name1,name2,name3,name4
emp1,emp2,emp3,emp4,emp5
user1,user2,user3

IS there any command in unix to do this conversion for the entire file?

Mohan
Hi,

Try this for the above scenario

Code:
paste -sd# inp.txt | sed 's/#,/,/g' | tr '#' '\n'

Regards,
Chella
# 6  
Old 07-15-2008
Quote:
Originally Posted by mohan_tuty
Thanks for the reply. This script works fine.

Is there any way to merge two lines based on specific occurance of a character

I am having a flat file which contains multiple records.
Each row in the file should contain specified number of delimiter.
For a particular row , if the delimiter count is not matched with the specified count, then then next row should be merged with the previous row. Again the same check has to be done.

The script should accept number of occurances of a particular delimiter as the parameter.

For Example if the number of occurances of comma in every line is 5 in a flat file

Sample Input

1,2,3,
4,5,6
a,b,c,d,e,f
10,20,30
,40,50,60
11,22
,33
,44,
55,
66

Output (Each row should contain 5 commas)

1,2,3,4,5,6
a,b,c,d,e,f
10,20,30,40,50,60
11,22,33,44,55,66

Mohan
Hi,

Try this for the above scenario,

Code:
paste -sd# inp.txt | sed 's/#,/,/g;s/,#/,/g' | tr '#' '\n'

Regards,
Chella
# 7  
Old 07-16-2008
Merging lines

Thanks for the reply.

This works fine if the word is not splitted in two lines.

But it a word is splitted in two lines it is not working.

Sample input

one,two,three,
four,five
six,se
ven,eight,nine,ten
eleven,twelve,thirteen,fourteen,fif
teen
1,2,3,4,5

output (Contains 4 commas in each line)

one,two,three,four,five
six,seven,eight,nine,ten
eleven,twelve,thirteen,fourteen,fifteen
1,2,3,4,5
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

2. Shell Programming and Scripting

Merging the lines of a file

Hello, I have a file with few lines starting with a digit (1-5 only ) followed by a dot (.). Remaining all the lines to be merged with its previous numbered lines. Merging must be done with a space. E.g., Source file: 3. abc def xyz 5. pqr mno def 4. jkl uvw 7. ghi 1. abc xyz 6. mno... (4 Replies)
Discussion started by: magnus29
4 Replies

3. Shell Programming and Scripting

Merging multiple files using lines from one file

I have been working of this script for a very long time and I have searched the internet for direction but I am stuck here. I have about 3000 files with two columns each. The length of each file is 50000. Each of these files is named this way b.4, b.5, b.6, b.7, b.8, b.9, b.10, b.11, b.12... (10 Replies)
Discussion started by: iconig
10 Replies

4. Shell Programming and Scripting

Merging lines

Thanks it worked for me. I have one more question on top of that. We had few records which were splitted in 2 lines instead of one. Now i identified those lines. The file is too big to open via vi and edit it. How can i do it without opening the file. Suppose, I want line number 1001 & 1002 to... (2 Replies)
Discussion started by: Gangadhar Reddy
2 Replies

5. Shell Programming and Scripting

merging two .txt files by alternating x lines from file 1 and y lines from file2

Hi everyone, I have two files (A and B) and want to combine them to one by always taking 10 rows from file A and subsequently 6 lines from file B. This process shall be repeated 40 times (file A = 400 lines; file B = 240 lines). Does anybody have an idea how to do that using perl, awk or sed?... (6 Replies)
Discussion started by: ink_LE
6 Replies

6. Shell Programming and Scripting

merging of 2 consecutive lines in a file for a specific pattern

Hi , I'm looking for a way to merge two lines only for a given pattern / condition. Input : abcd/dad + -49.201 2.09 -49.5 34 ewrew rewtre * fdsgfds/dsgf/sdfdsfasdd + -4.30 0.62 -49.5 45 sdfdsf cvbbv * sdfds/retret/asdsaddsa + ... (1 Reply)
Discussion started by: novice_man
1 Replies

7. Shell Programming and Scripting

Merging lines in a text file

hi, I have a file as below: Name: some_name Date: some_date Function Name: <some_function_name(jjjjjjjjj, fjddddd, gggg, ggg)> Changes:<Change A more of change A> Name: some_name Date: some_date Function Name: some_function_nameB(jjjjjjjjj, fjddddd, gggg, ggg) Changes:Change B... (15 Replies)
Discussion started by: flamingo_l
15 Replies

8. Shell Programming and Scripting

Merging lines based on occurances of a particular character in a file

Hi, Is there any way to merge two lines based on specific occurance of a character in a file. I am having a flat file which contains multiple records. Each row in the file should contain specified number of delimiter. For a particular row , if the delimiter count is not matched with... (2 Replies)
Discussion started by: mohan_tuty
2 Replies

9. Shell Programming and Scripting

Urgent Need Help! Merging lines in .txt file

I need to write a script that reads through an input .txt file and replaces the end value with the end value of the next line for lines that have distance <=4000. The first label line is not actually in the input. In the below example, 3217 is the distance from the end of the first line to the... (12 Replies)
Discussion started by: awknerd
12 Replies

10. UNIX for Dummies Questions & Answers

merging two lines in a file

Hi All, I want to merge two lines in a file till the end of the file. So what could be the command to get so. say file name : sample.txt contents: country=1 send apps =1 rece=2 country=2 send apps =3 rece=3 .. ... output: country=1;send apps =1 rece=2 country=2;send apps =3... (6 Replies)
Discussion started by: thaduka
6 Replies
Login or Register to Ask a Question