Computing.Net > Forums > Unix > Extract columns randomly

Extract columns randomly

Reply to Message Icon

Original Message
Name: Najib (by Nael_Najib)
Date: March 15, 2005 at 04:00:41 Pacific
Subject: Extract columns randomly
OS: Solaris 9
CPU/Ram: Sparc
Comment:

My problem is a file containing 150 columns and the needed columns are 145... how can i list the columns needed and spool them to a file without getting the unwanted 5 columns???

By the way, those unwanted columns are not in sequential state but they are randomly selected....

Thank you for your kind cooperation...

Aladimi


Report Offensive Message For Removal


Response Number 1
Name: Jim Boothe
Date: March 15, 2005 at 12:39:39 Pacific
Subject: Extract columns randomly
Reply: (edit)

You could have a really long print statement such as:

print $1,$2,$3...$150

and change it as required.  Somewhat less cumbersome might be a script such as follows that will output all columns except those specified for exclusion:

awk 'BEGIN {
exclude["3"]=1
exclude["6"]=1
exclude["9"]=1
exclude["12"]=1
#OFS=","
}

{out=""
 for (i=1;i<=NF;i++)
    if (i in exclude)
       continue
    else
       out=out ((out=="") ? "" : OFS) $i
 print out
}' filein

You did not say how the columns are randomly selected, but for example, the BEGIN procedure could load its exclusion array from a different file.


Report Offensive Follow Up For Removal

Response Number 2
Name: Najib (by Nael_Najib)
Date: March 15, 2005 at 12:58:37 Pacific
Subject: Extract columns randomly
Reply: (edit)

i really appreciate your response but its not working well...

awk: syntax error near line 11
awk: illegal statement near line 11
awk: syntax error near line 13
awk: illegal statement near line 13
awk: syntax error near line 14
awk: illegal statement near line 14

those are the out errors appearing when execution is committed.

Aladimi


Report Offensive Follow Up For Removal

Response Number 3
Name: Jim Boothe
Date: March 15, 2005 at 15:19:58 Pacific
Subject: Extract columns randomly
Reply: (edit)

If you have nawk, try that instead.  If that does not do it, try the version below.  I replaced the ? construct with a simple if-statement.

awk 'BEGIN {
exclude["20"]=1
exclude["33"]=1
exclude["35"]=1
#OFS=","
}

{out=""
 for (i=1;i<=NF;i++)
    if (i in exclude)
       continue
    else
       if (out=="")
          out=$i
       else
          out=out OFS $i
 print out
}' filein


Report Offensive Follow Up For Removal

Response Number 4
Name: Najib (by Nael_Najib)
Date: March 16, 2005 at 01:31:58 Pacific
Subject: Extract columns randomly
Reply: (edit)

Thank you very much ... that did work perfectly ... i added -F"|" after the nawk command.... i forgot to tell ya that the separator in my file in Pipe |...

Thanks anyway


Aladimi


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Extract columns randomly

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge