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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging lines based on occurances of a particular character in a file
# 1  
Old 07-15-2008
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 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
# 2  
Old 07-15-2008
Code:
$ awk '
BEGIN {
FS=",";
maxFLD=6;
}
{
while (NF < maxFLD || $0 ~ /\,$/ ) {
getline record;
$0 = $0 record
}
print $0
}
' mt.txt

//Jadu
# 3  
Old 07-15-2008
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. UNIX for Beginners Questions & Answers

Merging multiple lines into single line based on one column

I Want to merge multiple lines based on the 1st field and keep into single record. SRC File: AAA_POC_DB.TAB1 AAA_POC_DB.TAB2 AAA_POC_DB.TAB3 AAA_POC_DB.TAB4 BBB_POC_DB.TAB1 BBB_POC_DB.TAB2 CCC_POC_DB.TAB6 OUTPUT ----------------- 'AAA_POC_DB','TAB1','TAB2','TAB3','TAB4'... (10 Replies)
Discussion started by: raju2016
10 Replies

2. UNIX for Advanced & Expert Users

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

3. UNIX for Dummies Questions & Answers

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

4. Shell Programming and Scripting

Merging 2 lines based on a string

Dear Unix gurus I need help with a command or script to merge 2 lines where ever we find the string. I have attached scanned document. First line has string value: VSIN, immediate line has value: SETTLEMENT Where it finds the 2 string values in the whole file, one below the other,... (8 Replies)
Discussion started by: Karunyam
8 Replies

5. UNIX for Dummies Questions & Answers

Merging lines based on one column

Hi, I have a file which I'd like to merge lines based on duplicates in one column while keeping the info for other columns. Let me simplify it by an example: File ESR1 ANASTROZOLE NA FDA_approved ESR1 CISPLATIN NA FDA_approved ESR1 DANAZOL agonist NA ESR1 EXEMESTANE NA FDA_approved... (3 Replies)
Discussion started by: JJ001
3 Replies

6. Shell Programming and Scripting

Joining lines in TXT file based on first character

Hi, I have a pipe delimeted text file where lines have been split over 2 lines and I need to join them back together. For example the file I have is similar to the following: aaa|bbb |ccc ddd|eee fff|ggg |hhh I ideally need to have it looking like the following aaa|bbb|ccc ddd|eee... (5 Replies)
Discussion started by: fuji_s
5 Replies

7. Shell Programming and Scripting

Merging two special character separated files based on pattern matching

Hi. I have 2 files of below format. File1 AA~1~STEVE~3.1~4.1~5.1 AA~2~DANIEL~3.2~4.2~5.2 BB~3~STEVE~3.3~4.3~5.3 BB~4~TIM~3.4~4.4~5.4 File 2 AA~STEVE~AA STEVE WORKS at AUTO COMPANY AA~DANIEL~AA DANIEL IS A ELECTRICIAN BB~STEVE~BB STEVE IS A COOK I want to match 1st and 3rd... (2 Replies)
Discussion started by: crypto87
2 Replies

8. Shell Programming and Scripting

Replace first 3 occurances of a character in a file for each line

Hi all, I am very new to unix - ksh. I want to replace some characters in a file with some other, but only for first three occurances for each line. For eg: the first 3 occurances character ']' has to be replaced with ',' in each line. Please help. thanks Sreejith (1 Reply)
Discussion started by: rsreejithmenon
1 Replies

9. Shell Programming and Scripting

Merging Lines based on criteria

Hello, Need help with following scenario. A file contains following text: {beginning of file} New: This is a new record and it is not on same line. Since I have lost touch with script take this challenge and bring all this in one line. New: Hello losttouch. You seem to be struggling... (4 Replies)
Discussion started by: losttouch
4 Replies

10. Shell Programming and Scripting

Count occurances of a character in a file

I want to find the number of occurences of a character in a file. How do i do it. Eg: $cat file1.txt Welcome to World of Unix. $ If i want to find the occurences of 'o' then I should be getting 3. Thanks. (6 Replies)
Discussion started by: Shivdatta
6 Replies
Login or Register to Ask a Question