Combine multilpe lines with various combinations


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Combine multilpe lines with various combinations
# 1  
Old 07-12-2007
Combine multilpe lines with various combinations

I have the following file that looks simular to this:

68 1 2 10
68 3
68 4
107 1
107 10
5 1 10

How can I make the file look like this:

5 1 10
68 1 2 3 4 10
107 1 10
# 2  
Old 07-13-2007
Code:
#!/opt/third-party/bin/perl

my %fileHash;
my @arr;

open(FILE, "<", "a") || die "Unable to open file 'a'<$!>\n";

while(<FILE>) {
  chomp;
  @arr = split(/ /);
  for(my $i = 1; $i <= ($#arr + 1); $i++ ) {
    $fileHash{$arr[0]} .= " $arr[$i]";
  }
}

close(FILE);

foreach my $key ( sort keys %fileHash ) {
  print "$key ==> $fileHash{$key}\n";
}

exit 0

# 3  
Old 07-13-2007
Thanks for all your help! My trouble is that I have never used perl before. Is there a way to do this using awk?
# 4  
Old 07-13-2007
if order is not important
Code:
awk '{  col1 = $1
	    $1=""
	    array[col1]=$0array[col1] 
	 }
END{
    for ( i in array ) print i " "array[i]	
}' "file"

output:
Code:
# ./test.sh
5  1 10
68  4 3 1 2 10
107  10 1

# 5  
Old 07-13-2007
something to start with:

nawk -f jojo.awk myFile.txt

jojo.awk:
Code:
{
 vars=substr($0, index($0, FS)+1)
 arr[$1] = ($1 in arr) ? arr[$1] FS vars : vars
}
END {
  for (i in arr)
    print i, arr[i]
}

# 6  
Old 07-13-2007
Here is a Korn shell version:
Code:
mTemp='Temp.txt'
mLast=''
sort -n input_file | \
while read mLine
do
  set -- `echo $mLine`
  if [ "$1" != "${mLast}" ]; then
    if [ "${mLast}" != "" ]; then
      (echo ${mLast};sort -n ${mTemp}) | paste -d' ' -s -
    fi
    rm -f ${mTemp}
    mLast=$1
  fi
  shift
  echo $@ | tr ' ' '\n' >> ${mTemp}
done
if [ "${mLast}" != "" ]; then
  (echo ${mLast};sort -n ${mTemp}) | paste -d' ' -s -
  rm -f ${mTemp}
fi

# 7  
Old 07-13-2007
This will work for me, thanks for all the help.Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

awk to combine lines if fields match in lines

In the awk below, what I am attempting to do is check each line in the tab-delimeted input, which has ~20 lines in it, for a keyword SVTYPE=Fusion. If the keyword is found I am splitting $3 using the . (dot) and reading the portion before and after the dot in an array a. If it does have that... (12 Replies)
Discussion started by: cmccabe
12 Replies

3. UNIX for Dummies Questions & Answers

Combine lines in file

Hi All, I am trying to understand if its possible to carry out the following. I have a text file which contains output from multiple commands, within the file a node will be quiered twice if there was 2 commands for example. Is it possible do combine 2 lines into 1 if the first word is the... (1 Reply)
Discussion started by: mutley2202
1 Replies

4. Shell Programming and Scripting

Combine 2 lines

All, i am new to linux script... source Filter: vlan281-BUM-5M BUM-5M 0 0 Filter: vlan282-BUM-5M BUM-5M 0 0 Filter: vlan2828-BUM-5M Filter:... (2 Replies)
Discussion started by: samoptimus
2 Replies

5. UNIX for Dummies Questions & Answers

how can I combine 2 lines into one?

Hi, I have a file like this: A 1 B 2 C 3 ... And I want to have just one line like this: A 1 B 2 C 3 ... How can I do it? Thanks! (4 Replies)
Discussion started by: elsagarcia
4 Replies

6. Shell Programming and Scripting

combine 2 lines

Hello, I want to combine 2 lines in one I have a text file example: bla123 blo31 xx:yy:zz ->bla43 bli532 00:01:02 bla1237 blo351 aa:ss:dd ->bla433 bli34332 55:10:28 I want the result to be: bla123 blo31 xx:yy:zz, ->bla43 bli532 00:01:02 bla1237 blo351 aa:ss:dd, ->bla433 bli34332... (3 Replies)
Discussion started by: Petko Meshov
3 Replies

7. Shell Programming and Scripting

How to combine lines?

Hi, I have a file like this: "sdfc@abc.com","arovls","some addr ", "more stuff" "ssss@email.com","arovls","some addr", "sss" "edx@email.com","arovls","some addr", "sssdfvv" "ssss@a55.com","arovls","some addr", "lsdsdgf" "ssss@0234.com","aro vls","123 Main", "lSdfv" I want to... (4 Replies)
Discussion started by: erniel
4 Replies

8. Shell Programming and Scripting

combine 2 lines

Moderator, kindly delete this thread because I already found what I needed... thanks. (0 Replies)
Discussion started by: Deanne
0 Replies

9. UNIX for Dummies Questions & Answers

Combine two lines

Hi I have a file with the records 1 A B C D 2 E F G H 3 I J K L 4 M N O P In the ouput I want 1 A B C D 2 # F G H 3 I J K L 4 M N O P How to achieve this? (10 Replies)
Discussion started by: superprg
10 Replies

10. UNIX for Advanced & Expert Users

Combine two lines

I have a file called test.txt. Inside this file is the following: tcenh100.telkom.co.za 100.200.300.400 tcenh101.telkom.co.za 500.600.700.800 I want to take out the new lines and spaces, then I want to put the ip address of the host name next to the host name on the same line, as soon as... (7 Replies)
Discussion started by: wolf
7 Replies
Login or Register to Ask a Question