The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
combine 2 lines Deanne Shell Programming and Scripting 0 10-24-2007 03:08 AM
Combine two lines superprg UNIX for Dummies Questions & Answers 10 02-01-2006 08:47 AM
SImple HELP! how to combine two lines together using sed or awk.. forevercalz UNIX for Dummies Questions & Answers 10 09-29-2005 06:33 AM
Combine two lines wolf UNIX for Advanced & Expert Users 7 11-15-2002 12:14 PM
combine 2 lines (command & echo) primal UNIX for Dummies Questions & Answers 13 10-09-2001 06:51 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-12-2007
jojojmac5 jojojmac5 is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 11
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 (permalink)  
Old 07-13-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,960

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 (permalink)  
Old 07-13-2007
jojojmac5 jojojmac5 is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 11
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 (permalink)  
Old 07-13-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,557
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 (permalink)  
Old 07-13-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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 (permalink)  
Old 07-13-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
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 (permalink)  
Old 07-13-2007
jojojmac5 jojojmac5 is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 11
This will work for me, thanks for all the help.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:27 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0