Awk, ouptut columns in a file by column name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Awk, ouptut columns in a file by column name
# 8  
Old 01-06-2012
Thanks ctsgnb
we work in Database environment, but as part of text file processing in unix shell i want to avoid loading into a table just to get few columns back to unix for next processing.
Loading to a table would nee a sql job, table DDL (have to go thru modellers and approvals) and a lot of red tape because of database object involved

I found this link to be little useful, almost close but didn't work for me:
awk.info Using field names to reference columns
# 9  
Old 01-06-2012
how about this:
nawk -v fld='colC colF' -v OFS=, -F'|' -f js.awk myFile
where js.awk is:
Code:
BEGIN {
  n=split(fld, t, " ")
  for(i=1;i<=n;i++)
     fldN[t[i]]
}
FNR==1{
  for(i=1;i<=NF;i++)
    if($i in fldN)
      fldA[i]=++c
  next
}
{
  for(i=1;i<=NF;i++)
    if (i in fldA)
       printf("%c%s%c", (fldA[i]==1)?"":OFS, $i, (i==NF)?ORS:"")
}


Last edited by vgersh99; 01-06-2012 at 04:02 PM.. Reason: defined OFS amd FS on cli
# 10  
Old 01-06-2012
Thanks vgersh99
but nawk is not supported on our HP-UX 11.11 box :-(
# 11  
Old 01-06-2012
Quote:
Originally Posted by ysrini
Thanks vgersh99
but nawk is not supported on our HP-UX 11.11 box :-(
try either awk or gawk instead.
# 12  
Old 01-06-2012
vgersh99,
it's working with awk (i replaced nawk with awk)
Code:
awk -v fld='colC colF' -v OFS=, -F'|' -f js.awk myFile

However, the output doesn't contain new line characters, it's all appended in one line
k2,778,9ad,0ds9,i
There's delimiter between each set of the 2columns, but the next first column is appeneded to the previous 2nd column

Thanks,
-sri

Last edited by radoulov; 01-06-2012 at 05:14 PM.. Reason: Code tags.
# 13  
Old 01-06-2012
Quote:
Originally Posted by ysrini
vgersh99,
it's working with awk (i replaced nawk with awk)
Code:
awk -v fld='colC colF' -v OFS=, -F'|' -f js.awk myFile

However, the output doesn't contain new line characters, it's all appended in one line
k2,778,9ad,0ds9,i
There's delimiter between each set of the 2columns, but the next first column is appeneded to the previous 2nd column

Thanks,
-sri
Not all of your records/lines have the same number of columns/fields as stated in the header.
Try this:
Code:
 BEGIN {
  n=split(fld, t, " ")
  for(i=1;i<=n;i++)
     fldN[t[i]]
}
FNR==1{
  for(i=1;i<=NF;i++)
    if($i in fldN)
      fldA[i]=++c
  next
}
{
  for(i=1;i<=NF;i++)
    if (i in fldA)
       printf("%c%s", (fldA[i]==1)?"":OFS, $i)
  print ""
}

This User Gave Thanks to vgersh99 For This Post:
# 14  
Old 01-06-2012
AWESOME, It's working perfectly !!!
vgresh99, you are the Unix Man !!!

Thanks a lot !
-sri
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using awk to split a column into two columns

Hi, I am trying to split the following output into two columns, where each column has Source: Destination: OUTPUT TO FILTER $ tshark -r Capture_without_mtr.pcap -V | awk '/ (Source|Destination): /' | more Source: x.x.x.x Destination: x.x.x.x Source:... (2 Replies)
Discussion started by: sand1234
2 Replies

2. Shell Programming and Scripting

awk split columns to row after N number of column

I want to split this with every 5 or 50 depend on how much data the file will have. And remove the comma on the end Source file will have 001,0002,0003,004,005,0006,0007,007A,007B,007C,007E,007F,008A,008C Need Output from every 5 tab and remove the comma from end of each row ... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

3. Shell Programming and Scripting

awk add all columns if column 1 name matches

Hi - I want to add all columns if column1 name matches. TOPIC1 5 1 4 TOPIC2 3 2 1 TOPIC3 7 2 5 TOPIC1 6 3 3 TOPIC2 4 1 3 TOPIC3 9 5 4 . . . . . . . . . . . . Result should look like TOPIC1 11 4 7 TOPIC2 7 3 4 (1 Reply)
Discussion started by: oraclermanpt
1 Replies

4. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

5. UNIX for Advanced & Expert Users

Conversion of rows to columns using awk based om column value

HI, My Input file data is dn:adcfgeneral id:13343 Name:xxxxxx Password:iutyerwuitywue wpuwt tuiytruityrutyrwtyrwp dn:cdferwjyyyy id:3875 Name:yyyy Password :hgfdsjkfhdsfkdlshf dshfkldshfdklsfh interset:uiuiufj My output should be ... (6 Replies)
Discussion started by: dineshaila
6 Replies

6. Shell Programming and Scripting

Need help with awk statement to break nth column in csv file into 3 separate columns

Hello Members, I have a csv file in the format below. Need help with awk statement to break nth column into 3 separate columns and export the changes to new file. input file --> file.csv cat file.csv|less "product/fruit/mango","location/asia/india","type/alphonso" need output in... (2 Replies)
Discussion started by: awk-admirer
2 Replies

7. Shell Programming and Scripting

Please Help!!!! Awk for summing columns based on selected column value

a,b,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,bb,cc,dd,ee,ff,gg,hh,ii a thru ii are digits and strings.... The awk needed....if coloumn 9 == i (coloumn 9 is string ), output the sum of x's(coloumn 22 ) in all records and sum of y's (coloumn 23 ) in all records in a file (records.txt).... (6 Replies)
Discussion started by: BrownBob
6 Replies

8. Shell Programming and Scripting

help sum columns by break in first column with awk or sed or something.

I have some data that is something like this? item: onhand counted location ITEM0001 1 0 a1 ITEM0001 0 1 a2 ITEM0002 5 0 b5 ITEM0002 0 6 c1 I want to sum up... (6 Replies)
Discussion started by: syadnom
6 Replies

9. Shell Programming and Scripting

Single column to multiple columns in awk

Hi - I'm new to the awk programming language. I'm trying to print a single column of data to several columns, and I found an article on iTWorld.com (ITworld.com - Printing in columns). It looks like the mkCols2 script is very close to what I need to do, but it looks like the end of the code... (2 Replies)
Discussion started by: astroDave
2 Replies

10. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies
Login or Register to Ask a Question