sort data in different columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort data in different columns
# 8  
Old 08-03-2008
Quote:
Originally Posted by mogabr
Hello Jean

when run script getting error
awk: syntax error near line 1
awk: bailing out near line 1
please advice
Try with nawk or gawk instead of awk

Jean-Pierre.
# 9  
Old 08-03-2008
Quote:
Originally Posted by mogabr
getting the same error
awk: syntax error near line 4
awk: bailing out near line 4
Did you try nawk and /usr/xpg4/bin/awk as suggested?
# 10  
Old 08-03-2008
Quote:
Originally Posted by aigles
Try with nawk or gawk instead of awk

Jean-Pierre.
i have tried on this script
nawk -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();
}
' list_of_customer_number

the out put was in wrong fromat


customerCode PrimaryAddress Model_Handle usid
57.217.41.201 0x11322800
57.215.49.14 0x113fa800
57.219.48.83 0x11395800
57.219.48.47 0x11389000
57.219.48.140 0x11384000
57.0.144.159 0x1131f000
57.217.46.212 0x11303489

also i need to put the output in a file.. how to fix above ?

regards
# 11  
Old 08-03-2008
The output can be redirected to a file :
Code:
awk awk_script inputfile > outputfile

.

Quote:
the out put was in wrong fromat

customerCode PrimaryAddress Model_Handle usid
57.217.41.201 0x11322800
57.215.49.14 0x113fa800
57.219.48.83 0x11395800
57.219.48.47 0x11389000
57.219.48.140 0x11384000
57.0.144.159 0x1131f000
57.217.46.212 0x11303489
Please show us the content of your input file.
Rarmark: The names in the NamesList variable must the values that are specified in the field two of the input records.
Code:
Id Name Iid Value
0x4440001 customerCode 44077
0x11d2a PrimaryAddress 57.217.41.201
0x129fa Model_Handle 0x11322800
0x4440000 usid fi00bxtaa4
0x4440008 customerName Zurich


Jean-Pierre.
# 12  
Old 08-03-2008
Quote:
Originally Posted by aigles
The output can be redirected to a file :
Code:
awk awk_script inputfile > outputfile

.


Please show us the content of your input file.
Rarmark: The names in the NamesList variable must the values that are specified in the field two of the input records.
Code:
Id Name Iid Value
0x4440001 customerCode 44077
0x11d2a PrimaryAddress 57.217.41.201
0x129fa Model_Handle 0x11322800
0x4440000 usid fi00bxtaa4
0x4440008 customerName Zurich


Jean-Pierre.
Hello Jean

the content of the file is too big but the infromation repeated as following

Id Name Iid Value
0x4440001 eq_customerCode 44077
0x11d2a PrimaryAddress 57.217.41.201
0x129fa Model_Handle 0x11322800
0x4440000 eq_usid fi00bxtaa4
0x4440008 eq_customerName Zurich Financial Services
Id Name Iid Value
0x4440001 eq_customerCode 55487
0x11d2a PrimaryAddress 57.215.49.14
0x129fa Model_Handle 0x113fa800
0x4440000 eq_usid fi00dlzuc8
0x4440008 eq_customerName T-Systems Business Services Gmbh
Id Name Iid Value
0x4440001 eq_customerCode 1908
0x11d2a PrimaryAddress 57.219.48.83
0x129fa Model_Handle 0x11395800
0x4440000 eq_usid fi00kgy094
0x4440008 eq_customerName Akzo Nobel Central Purchasing Bv


the needed columns will be like following
eq_customerCode PrimaryAddress Model_Handle eq_usid eq_customerName

regards
# 13  
Old 08-03-2008
You must modify the NamesList variable.
Another problem is that the values can be spanned over more than one field.

A new version of the script (changes have been colorized) :
Code:
awk -v NamesList="eq_customerCode,PrimaryAddress,Model_Handle,eq_usid,eq_customerName" \
'

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);
   if (name in Indexes)  {
      value = "";
      for (i=3; i<=NF; i++)
         value = value " " $i
      Values[Indexes[name]] = substr(value, 2);
      Valid++;
   }
}

END {
   output_datas();
}
' mogarb2.dat

Input file (mogargb2.dat)
Code:
Id Name Iid Value
0x4440001 eq_customerCode 44077
0x11d2a PrimaryAddress 57.217.41.201
0x129fa Model_Handle 0x11322800
0x4440000 eq_usid fi00bxtaa4
0x4440008 eq_customerName Zurich Financial Services
Id Name Iid Value
0x4440001 eq_customerCode 55487
0x11d2a PrimaryAddress 57.215.49.14
0x129fa Model_Handle 0x113fa800
0x4440000 eq_usid fi00dlzuc8
0x4440008 eq_customerName T-Systems Business Services Gmbh
Id Name Iid Value
0x4440001 eq_customerCode 1908
0x11d2a PrimaryAddress 57.219.48.83
0x129fa Model_Handle 0x11395800
0x4440000 eq_usid fi00kgy094
0x4440008 eq_customerName Akzo Nobel Central Purchasing Bv

Output:
Code:
eq_customerCode PrimaryAddress Model_Handle eq_usid eq_customerName
44077 57.217.41.201 0x11322800 fi00bxtaa4 Zurich Financial Services
55487 57.215.49.14 0x113fa800 fi00dlzuc8 T-Systems Business Services Gmbh
1908 57.219.48.83 0x11395800 fi00kgy094 Akzo Nobel Central Purchasing Bv

# 14  
Old 08-03-2008
Quote:
Originally Posted by aigles
You must modify the NamesList variable.
Another problem is that the values can be spanned over more than one field.

A new version of the script (changes have been colorized) :
Code:
awk -v NamesList="eq_customerCode,PrimaryAddress,Model_Handle,eq_usid,eq_customerName" \
'
 
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);
   if (name in Indexes)  {
     value = "";
     for (i=3; i<=NF; i++)
        value = value " " $i
     Values[Indexes[name]] = substr(value, 2);
      Valid++;
   }
}
 
END {
   output_datas();
}
' mogarb2.dat

Input file (mogargb2.dat)
Code:
Id Name Iid Value
0x4440001 eq_customerCode 44077
0x11d2a PrimaryAddress 57.217.41.201
0x129fa Model_Handle 0x11322800
0x4440000 eq_usid fi00bxtaa4
0x4440008 eq_customerName Zurich Financial Services
Id Name Iid Value
0x4440001 eq_customerCode 55487
0x11d2a PrimaryAddress 57.215.49.14
0x129fa Model_Handle 0x113fa800
0x4440000 eq_usid fi00dlzuc8
0x4440008 eq_customerName T-Systems Business Services Gmbh
Id Name Iid Value
0x4440001 eq_customerCode 1908
0x11d2a PrimaryAddress 57.219.48.83
0x129fa Model_Handle 0x11395800
0x4440000 eq_usid fi00kgy094
0x4440008 eq_customerName Akzo Nobel Central Purchasing Bv

Output:
Code:
eq_customerCode PrimaryAddress Model_Handle eq_usid eq_customerName
44077 57.217.41.201 0x11322800 fi00bxtaa4 Zurich Financial Services
55487 57.215.49.14 0x113fa800 fi00dlzuc8 T-Systems Business Services Gmbh
1908 57.219.48.83 0x11395800 fi00kgy094 Akzo Nobel Central Purchasing Bv


Hello

thanks a lot for ur help Smilie but awk is not working so i am using nawk instead ..
IT WORKS GREAT NOW ... THANKS VERY MUCH FOR UR HELP

regards

Last edited by mogabr; 08-03-2008 at 05:51 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to Sort into columns

Hi geeks! I want to convert the following: EPC-NotificationData: sms:2348034503643 EPC-GroupIds: 300H:10:22-01-2014T07:30:14,22-04-2014T07:30:14 To: EPC-NotificationData: sms:2348034503643, EPC-GroupIds: 300H:10:22-01-2014T07:30:14,22-04-2014T07:30:14 I want them to be on the same... (13 Replies)
Discussion started by: infinitydon
13 Replies

2. Shell Programming and Scripting

sort on multiple columns

hi all, i have a file , having few columns. i wanted to sort it based on 2nd column and then based on 1st column. But i have some problem in first column. first column have characters and numbers, but problem is number of characters are not same in all rows. Few rows have 13 characters and then... (3 Replies)
Discussion started by: deepakiniimt
3 Replies

3. Shell Programming and Scripting

sort second columns in file.

File will have two columns key column and second column which is pipe separated and that need to be sorted. Below is input file. 1, D|B|A|C 2, C|A|B 3, E|A|F|G|H|D|B|C 4, A|B|D|C|F Output should be 1, A|B|C|D 2, A|B|C 3, A|B|C|D|E|F|G|H 4, A|B|D|C|F (11 Replies)
Discussion started by: girish119d
11 Replies

4. Shell Programming and Scripting

sort on multiple columns

Howdy! Need to sort a large .txt file containing the following, using sort. First based on the 1st column, and then on the 2nd column: Group01.01 1000500 31 0.913 -1.522974494 Group01.01 1001500 16 0.684 -0.967496041 Group01.01 36500 19 0.476 na Group01.02 365500 15 0.400 na... (1 Reply)
Discussion started by: sramirez
1 Replies

5. UNIX for Dummies Questions & Answers

Sort by columns

Hello, I have a text file that looks like this and I need a bash script to: 12:48:32 PM 002* OUT 000418 01:10:34 PM 002* ONL 000418 01:49:17 PM 001* OUT 000364 01:52:09 PM 001* ONL 000364 ... The fields are: 12-hour format time, some number, state (online, offline) and another... (2 Replies)
Discussion started by: Ravendark
2 Replies

6. UNIX for Dummies Questions & Answers

Suggestion to convert data in rows to data in columns

Hello everyone! I have a huge dataset looking like this: nameX nameX 0 1 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ............... nameY nameY 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ..... nameB nameB 0 1 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ..... (can be several thousands of codes) and I need... (8 Replies)
Discussion started by: kush
8 Replies

7. UNIX for Dummies Questions & Answers

Sort 2 columns numerically

Hi, A basic query. In the example file below, I want to sort by column 1 and then by column 2 numerically. I have tried sort -k2n,1 file1 but while this sorts the columns in the correct order, it does not sort column 2 numerically. Any help would be much appreciated. Also, if you have time to... (3 Replies)
Discussion started by: auburn
3 Replies

8. UNIX for Dummies Questions & Answers

Sort on multi columns

Hope someone can help, I am trying to sort a generic file like this test2 A 468 0 test2 R 468 1 test2 E 468 1 test2 R 468 3 test2 R 468 4 test2 R 468 459 test2 Z 468 0 test1 A 191 0 test1 R 191 191 test1 Z 191 0 test3 A 3 0 test3 R 3 3 test3 Z 3 0 test0 A 1 0 test4 A 1 0 test4 E... (2 Replies)
Discussion started by: gio001
2 Replies

9. Shell Programming and Scripting

sort columns by field

hello, i have a table contain many columns delimited by blank. i want to sort this table by the 2 columns and 3 one and i want to keep the first line inchanged? how can i do using the sort command? thanks table like : field1 field2 field3 field4 x y z b t h r n .. (4 Replies)
Discussion started by: kamel.seg
4 Replies

10. UNIX for Dummies Questions & Answers

Sort by Columns

Hello, I am new in UNIX I am looking for a instrction to sort a file by columns 6,25 and 41 this is what I tried but not getting the correct result: sort -t= -k1.6,1.25,1.41 to_sort.txt > sorted.txt I used -t= just to get the whole line as one field. INVS80993596SUM994338602XX... (1 Reply)
Discussion started by: murbina
1 Replies
Login or Register to Ask a Question