The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove lines, Sorted with Time based columns using AWK & SORT karthikn7974 Shell Programming and Scripting 1 05-09-2008 11:04 PM
sort columns by field kamel.seg Shell Programming and Scripting 4 02-20-2008 06:50 AM
add columns from file to another and sort kamel.seg Shell Programming and Scripting 12 12-12-2007 02:39 PM
Help needed to sort multiple columns in one file ahjiefreak UNIX for Dummies Questions & Answers 1 12-07-2007 05:50 AM
Sort by Columns murbina UNIX for Dummies Questions & Answers 1 05-10-2004 02:21 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-02-2008
mogabr mogabr is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 32
sort data in different columns

Hello all:

i have list with the following format
Id Name Iid Value
0x4440001 customerCode 44077
0x11d2a PrimaryAddress 57.217.41.201
0x129fa Model_Handle 0x11322800
0x4440000 usid fi00bxtaa4
0x4440008 customerName Zurich

while i needed to be in the next format
customerCode PrimaryAddress Model_Handle usid
44077 55.217.41.201 0x11322800 fi00bxtaa4

how to do so ?

regards
  #2 (permalink)  
Old 08-02-2008
cfajohnson's Avatar
cfajohnson cfajohnson is online now Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Code:
awk ' NR == 1 { next }
{
 name[NR-1] = $2
 value[NR-1] = $3
}
END {
 while ( ++n < NR - 1 ) printf "%s ", name[n]
 print ""
 while ( ++v < NR - 1 ) printf "%s ", value[v]
 print ""
}' "$FILE"
  #3 (permalink)  
Old 08-03-2008
mogabr mogabr is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 32
Quote:
Originally Posted by cfajohnson View Post
Code:
awk ' NR == 1 { next }
{
 name[NR-1] = $2
 value[NR-1] = $3
}
END {
 while ( ++n < NR - 1 ) printf "%s ", name[n]
 print ""
 while ( ++v < NR - 1 ) printf "%s ", value[v]
 print ""
}' "$FILE"
hello

but the input data will be repeated in the file in that way
Id Name Iid Value
0x4440001 customerCode 44077
0x11d2a PrimaryAddress 57.217.41.201
0x129fa Model_Handle 0x11322800
0x4440000 usid fi00bxtaa4
0x4440008 customerName Zurich
Id Name Iid Value
0x4440001 customerCode 44044
0x11d2a PrimaryAddress 57.210.40.20
0x129fa Model_Handle 0x11326500
0x4440000 usid fi00bxtbb0
0x4440008 customerName Zurich

and so on ...
  #4 (permalink)  
Old 08-03-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
Try and adapt the following script.
The variable NamesList contains all the required names in output.
Code:
awk -v NamesList="customerCode,PrimaryAddress,Model_Handle,usid" \
'

function output_datas(    i, out) {
   if (Valid) {
      for (i=1; i<=NamesCount; i++) {
         out = (out ? out OFS : "") Values[i];
      }
      print out;
   }
}

function reset_datas(    i) {
   Valid = "";
   for (i=1; i<=NamesCount; i++)
      Values[i] = "";
}

BEGIN {
   NamesCount = split(NamesList, Names, ",");
   Header = "";
   for (i=1; i<=NamesCount; i++) {
      Indexes[tolower(Names[i])] = i;
      Header = (Header ? Header OFS : "") Names[i];
   }
   print Header;
}

/^Id/ {
   output_datas();
   reset_datas();
   next;
}

{
   name = tolower($2);
   value = $3;
   if (name in Indexes)  {
      Values[Indexes[name]] = value;
      Valid++;
   }
}

END {
   output_datas();
}
' mogarb.dat
Input datas (mogarb.dat)
Code:
Id Name Iid Value
0x4440001 customerCode 44077
0x11d2a PrimaryAddress 57.217.41.201
0x129fa Model_Handle 0x11322800
0x4440000 usid fi00bxtaa4
0x4440008 customerName Zurich
Id Name Iid Value
0x4440001 customerCode 44044
0x11d2a PrimaryAddress 57.210.40.20
0x129fa Model_Handle 0x11326500
0x4440000 usid fi00bxtbb0
0x4440008 customerName Zurich
Output
Code:
customerCode PrimaryAddress Model_Handle usid
44077 57.217.41.201 0x11322800 fi00bxtaa4
44044 57.210.40.20 0x11326500 fi00bxtbb0
Jean-Pierre.
  #5 (permalink)  
Old 08-03-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
I'm not sure if you want to sort or to change the format only:
(use nawk or /usr/xpg4/bin/awk on Solaris)

Code:
awk 'BEGIN { 
  print "customerCode PrimaryAddress Model_Handle usid" 
  }
c && c < 5 { 
  v = v ? v FS $NF : $NF 
  } 
++c == 6 { 
  print v
  v = c = "" 
  }' filename
  #6 (permalink)  
Old 08-03-2008
mogabr mogabr is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 32
Quote:
Originally Posted by radoulov View Post
I'm not sure if you want to sort or to change the format only:
(use nawk or /usr/xpg4/bin/awk on Solaris)

Code:
awk 'BEGIN { 
  print "customerCode PrimaryAddress Model_Handle usid" 
  }
c && c < 5 { 
  v = v ? v FS $NF : $NF 
  } 
++c == 6 { 
  print v
  v = c = "" 
  }' filename

getting the same error
awk: syntax error near line 4
awk: bailing out near line 4
  #7 (permalink)  
Old 08-03-2008
mogabr mogabr is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 32
Quote:
Originally Posted by aigles View Post
Try and adapt the following script.
The variable NamesList contains all the required names in output.
Code:
awk -v NamesList="customerCode,PrimaryAddress,Model_Handle,usid" \
'
 
function output_datas(    i, out) {
   if (Valid) {
      for (i=1; i<=NamesCount; i++) {
         out = (out ? out OFS : "") Values[i];
      }
      print out;
   }
}
 
function reset_datas(    i) {
   Valid = "";
   for (i=1; i<=NamesCount; i++)
      Values[i] = "";
}
 
BEGIN {
   NamesCount = split(NamesList, Names, ",");
   Header = "";
   for (i=1; i<=NamesCount; i++) {
      Indexes[tolower(Names[i])] = i;
      Header = (Header ? Header OFS : "") Names[i];
   }
   print Header;
}
 
/^Id/ {
   output_datas();
   reset_datas();
   next;
}
 
{
   name = tolower($2);
   value = $3;
   if (name in Indexes)  {
      Values[Indexes[name]] = value;
      Valid++;
   }
}
 
END {
   output_datas();
}
' mogarb.dat
Input datas (mogarb.dat)
Code:
Id Name Iid Value
0x4440001 customerCode 44077
0x11d2a PrimaryAddress 57.217.41.201
0x129fa Model_Handle 0x11322800
0x4440000 usid fi00bxtaa4
0x4440008 customerName Zurich
Id Name Iid Value
0x4440001 customerCode 44044
0x11d2a PrimaryAddress 57.210.40.20
0x129fa Model_Handle 0x11326500
0x4440000 usid fi00bxtbb0
0x4440008 customerName Zurich
Output
Code:
customerCode PrimaryAddress Model_Handle usid
44077 57.217.41.201 0x11322800 fi00bxtaa4
44044 57.210.40.20 0x11326500 fi00bxtbb0
Jean-Pierre.
Hello Jean

when run script getting error
awk: syntax error near line 1
awk: bailing out near line 1
please advice
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl, unix commands

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 03:45 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