Help in unix script to join similar lines of input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in unix script to join similar lines of input
# 1  
Old 06-27-2012
Help in unix script to join similar lines of input

Hi,
I have been thinking of how to script this but i have no clue at all..
Could someone please help me out or give me some idea on this?
I would like to group those lines with the same first variable in each line, joining the 2nd variables with commas.
Let's say i have the following input.
Code:
aa c1
aa c2
aa c3
cc d1
dd e1
dd e2
ee f1

I would like the output to be like this.
Code:
aa c1,c2,c3
cc d1
dd e1,e2
ee f1

Could this be easily done with bash script?
Or should i try perl script then?
I'm a beginner in bash script and perl.
Thank you.
# 2  
Old 06-27-2012
Is file sorted on first column?
# 3  
Old 06-28-2012
Try this ,
Code:
sort Filename| awk '{if(fld1==$1){printf ",%s",$2}else{printf "\n%s",$0}fld1=$1}'

# 4  
Old 06-29-2012
Quote:
Originally Posted by pravin27
Try this ,
Code:
sort Filename| awk '{if(fld1==$1){printf ",%s",$2}else{printf "\n%s",$0}fld1=$1}'

GREAT !!!
I've found a solution to this by doing the following but i think your's is so smart. It just takes 1 line...Smilie
Code:
for m in `cat ${input} | awk '{print $1}' | sort | uniq `
do
        var=`grep "^${m} " ${output} | awk '{print $2}' | tr '\n' ',' | sed '$s/,$//'`
        echo "${m} ${var}"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove Comment Lines From Script/Input File

Hello, I have a SAS code that predominantly has comments line and the real code like below and i want to remove ONLY THE COMMENTS from the code in the single line or spanned across multiple lines. /******************************************************************** *** This Is a Comment... (4 Replies)
Discussion started by: arooonatr
4 Replies

2. Shell Programming and Scripting

Comparing two files in UNIX and create a new file similar to equi join

I have 2 files namely branch.txt file & RXD.txt file as below Ex:Branch.txt ========================= B1,Branchname1,city,country B2,Branchname2,city,country B3,Branchname3,city,country B4,Branchname4,city,country B5,Branchname5,city,country RXD file : will... (11 Replies)
Discussion started by: satece
11 Replies

3. Shell Programming and Scripting

Script that takes IP address as an Input and deletes 7 lines

I have a flat file that contains a list of IP address following 6 additional lines. I would like to get a help in the following. a shell script that would take ip address or list of ip addresses as input, search one by one the ip address in the file and as soon as it find the exact match it... (12 Replies)
Discussion started by: knijjar
12 Replies

4. Shell Programming and Scripting

awk script to perform an action similar to vlookup between two csv files in UNIX

Hi, I am new to awk/unix and am trying to put together an awk script to perform an action similar to vlookup between the two csv files. Here are the contents of the two files: File 1: Date,ParentID,Number,Area,Volume,Dimensions 2014-01-01,ABC,247,83430.33,857.84,8110.76... (9 Replies)
Discussion started by: Prit Siv
9 Replies

5. Shell Programming and Scripting

Join all the lines matching similar pattern

I am trying to Join all the lines matching similar pattern. Example ; I wanted to join all the lines which has sam to a single line. In next line, i wanted to have all the lines with jones to a single line....etc > cat sample.txt sam 2012/11/23 sam 2012/12/5 sam 2012/12/5 jones... (2 Replies)
Discussion started by: evrurs
2 Replies

6. UNIX for Dummies Questions & Answers

how to join all lines in afile in unix

Hi, I have a unix file which has many lines, i need to join all the lines to single line. Eg: myfile.txt contains: a 123 45fg try and i need the output as : a 123 45fg try Please help me on this. Thanks! (2 Replies)
Discussion started by: RP09
2 Replies

7. Shell Programming and Scripting

how do i join two lines from a input file

HI all, i have a input file,i have to join 2nd line into firstline and 4th line into 2nd line and so on.. for this we also consider odd no. of line.It's operate on original input file but output file should temp file. like .. filename=cdr.cfg line1 line2 line3 line4Desired output should be... (9 Replies)
Discussion started by: suryanarayan
9 Replies

8. Shell Programming and Scripting

awk script - reading input lines

Can I do something like, if($0==/^int.*$/) { print "Declaration" } for an input like: int a=5; If the syntax is right, it is not working for me, but I am not sure about the syntax. Please help. Thanks, Prasanna (1 Reply)
Discussion started by: prasanna1157
1 Replies

9. Shell Programming and Scripting

How can i join three lines into one in unix?

Hi all, I am trying to merge three lines into one in my unix text file. My text file sis omething like this. xxxxxxxxx yyyyyyyyyyy zzz aaaaaaaaa bbbbbb ccccc Expected out put is xxxxxxxxx yyyyyyyyyyy zzz aaaaaaaaa bbbbbb ccccc I tried with awk as shown below. (23 Replies)
Discussion started by: rdhanek
23 Replies

10. Shell Programming and Scripting

Counting similar lines from file UNIX

I have a file which contains data as below: nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/common/index.jsf nbk1j7o pageName=/jsp/common/index.jsf nbk1wqe... (6 Replies)
Discussion started by: mohsin.quazi
6 Replies
Login or Register to Ask a Question