Shell script for merging lines in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script for merging lines in a loop
# 1  
Old 07-13-2009
Network Shell script for merging lines in a loop

Dear All,
I need a script to merge lines of an input file in a loop, please guide me for the script or one liner(awk, sed, tr, shell, perl).
I/P File-------------------------
Code:
APaul,,,,SDH,,23,,,PPH,,2
,,,,KKH,,19,,,MMH,,12,
,,,,CCH,,22,,,MNH,,19,
,,,,TCH,,55,,,NNH,,67,
BPaul,,,,SDH,,44,,,PPH,,3
,,,,KKH,,12,,,MMH,,22,
,,,,CCH,,33,,,MNH,,66,
,,,,TCH,,62,,,NNH,,98,
CPaul,,,,SDH,,44,,,PPH,,3
,,,,KKH,,11,,,MMH,,10,
,,,,CCH,,03,,,MNH,,39,
,,,,TCH,,68,,,NNH,,97,

---------------------------------
O/P File
Code:
APaul,,,,SDH,,23,,,PPH,,2,,,,KKH,,19,,,MMH,,12,,,,,CCH,,22,,,MNH,,19,,,,,TCH,,55,,,NNH,,67,
BPaul,,,,SDH,,44,,,PPH,,3,,,,KKH,,12,,,MMH,,22,,,,,CCH,,33,,,MNH,,66,,,,,TCH,,62,,,NNH,,98,
CPaul,,,,SDH,,44,,,PPH,,3,,,,KKH,,11,,,MMH,,10,,,,,CCH,,03,,,MNH,,39,,,,,TCH,,68,,,NNH,,97,
-----------------------------------

Thanks in Advance For experts.:
Ace
Smilie

Last edited by vgersh99; 07-13-2009 at 10:52 AM.. Reason: code tags, PLEASE!
# 2  
Old 07-13-2009
cat filename | paste - - - -

Try this.
# 3  
Old 07-13-2009
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk 'END { print r }
r && !/^,/ { print r; r = "" }
{ r = r ? r $0 : $0 }
' infile

With sed (this is from the sed FAQ):

Code:
sed -e :a -e '$!N;s/\n,/,/;ta' -e 'P;D' infile


Or:

Code:
perl -lne'
print$x and undef$x if!/^,/&&$x;
$x.=$_ and next;END{print$x}
' infile


Last edited by radoulov; 07-13-2009 at 11:11 AM..
# 4  
Old 07-13-2009
Try:
Code:
perl -e  'while(<>) { chop; if($_ =~ /^[A-Z]/) { $_="\n".$_; } print; }' filename

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. UNIX for Dummies Questions & Answers

Loop for file merging in a folder

Dear all, I have a few files in a folder (lets say 5) and each have a few lines in them. I want to create merges of each of them with the other ones e.g. I need the following merges I tried to write a loop and started with combinations of 2, I get the write file names but for each... (17 Replies)
Discussion started by: A-V
17 Replies

3. Shell Programming and Scripting

Merging 2 lines together

I have a small problem, which due to my lack of knowledge, has left me unable to decipher some of the solutions that I looked at on these forums. So below is a piece of text, which I ran via cat -vet, which comes from within a program file. I have many such programs to process and repeatable,... (4 Replies)
Discussion started by: skarnm
4 Replies

4. Shell Programming and Scripting

Merging Files in UNIX shell script

I have the urge to merge some files using unix shell script but I'm very new using this language and I haven't succeeded yet. The requirement is to merge the header, body and footer into one file with the name "ANY-NAME" in below example. To identify which files should be merged, I have flagged... (9 Replies)
Discussion started by: black_soul
9 Replies

5. 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

6. Shell Programming and Scripting

File merging based on different counter loop

hello, File 1 main Group sub group MIT VAR_1D_DATA_TYPE 23-03-2012 MIT VAR_1D_DATA_TYPE 22-03-2012 MIT VAR_10D_DATA_TYPE 23-03-2012 MIT VAR_10D_DATA_TYPE 22-03-2012 MIT ... (0 Replies)
Discussion started by: manas_ranjan
0 Replies

7. 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

8. Shell Programming and Scripting

How to use while loop in bash shell to read a file with 4 lines of gap

Hi , I am currently using the while loop in bash shell, as follows. while read line do echo $line done < file.txt However, i want to use the while loop on file.txt, which will read the file with 4 lines of gap. Ex- if file.txt is a file of 100 lines, then i want to use the loop such... (3 Replies)
Discussion started by: jitendriya.dash
3 Replies

9. Shell Programming and Scripting

Merging lines - Tuning the script

Hi, Anybody can tune the below script? Its taking 1hour to process 7000 lines. addendaRecInd=7 entryDetailRecord=6 hdrDetailRecord=1 companyHdrRecord=5 companyCtrlRecord=8 trlRecord=9 recLength=94 FormatDone=false trlCompleted=false prevEntryCalc=false batchCnt=0 hdrFound=false... (3 Replies)
Discussion started by: senthil_is
3 Replies

10. UNIX for Dummies Questions & Answers

Merging lines into one

Hello. I would be very pleased if sb. help me to solve my problem. I've got a file with many non blank lines and I want to merge all lines into one not destroy the informations on them. I've tryed it with split and paste, tr, sed , but everything I've done has been wrong. I know about crazy... (8 Replies)
Discussion started by: Foxgard
8 Replies
Login or Register to Ask a Question