Column grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Column grep
# 1  
Old 09-30-2010
Column grep

hi everyone,

I am looking for an easy way to extract columns from a text file based on a regular expression, kind of like grep but searching and returning columns instead.

for example, suppose i have the following file, 'file.txt':

A B C D B
1 2 3 4 5
6 7 8 9 0
5 6 7 8 9
2 3 8 9 0

I would like a command, let's call it transpose_grep, that does this:

Code:
cat file.txt | transpose_grep 'C'

It would return:

C
3
8
7
8

To give another example:

Code:
cat file.txt | transpose_grep 'B'

would return two columns, since 'B' appears twice:

B B
2 5
7 0
6 9
3 0

Is there a unix tool I can use to do this? Thanks for the help!
# 2  
Old 09-30-2010
Code:
$ ruby -ane 'BEGIN{h={}};$F.each_with_index{|x,y|h[y+1]=x} if $.==1; h.select {|k,v| print "#{$F[k-1]} " if v == "B"} and puts if $.>1 ' file
2 5
7 0
6 9
3 0

$ ruby -ane 'BEGIN{h={}};$F.each_with_index{|x,y|h[y+1]=x} if $.==1; h.select {|k,v| print "#{$F[k-1]} " if v == "C"} and puts if $.>1 ' file
3
8
7
8

# 3  
Old 09-30-2010
I tried it for single occurrence, just extend the same for multiple occurrences.

Code:
use strict;
use warnings;

open(F, '<', 'a') or die;

my $data;
$data = <F>;
chomp($data);
my $cnt = 0;
foreach my $d ( split(/  */, $data) ) {
    last if ( $d =~ /$ARGV[0]/ );
    $cnt++;
}

print $ARGV[0], "\n";
while ( $data = <F> ) {
    chomp($data);
    my $val = (split(/  */, $data))[$cnt];
    print $val, "\n";
}

close(F);

# 4  
Old 09-30-2010
And another one:

Code:
perl -lane'BEGIN { $k = shift }
  @cols = grep $F[$_] eq $k , 0..@F 
    if $. == 1; 
  print "@F[@cols]"
  ' <pattern> infile

Given your sample data it outputs the following result:

Code:
% perl -lane'BEGIN { $k = shift }
  @cols = grep $F[$_] eq $k , 0..@F
    if $. == 1;
  print "@F[@cols]"
  ' B infile 
B B
2 5
7 0
6 9
3 0
% perl -lane'BEGIN { $k = shift }
  @cols = grep $F[$_] eq $k , 0..@F
    if $. == 1;
  print "@F[@cols]"
  ' C infile 
C
3
8
7
8

# 5  
Old 09-30-2010
bash code:
  1. #!/bin/bash
  2. COLS=$(head -1 file)
  3. for C in $COLS
  4. do
  5.    ((i++))
  6.    &#91; $C = $1 ] && IDX+="$i,"
  7. done
  8. cut -d' ' -f${IDX%,} file
# 6  
Old 09-30-2010
hey everyone, this is perfect! Thanks! the bash script is exactly what i was looking for (but perl and ruby are great too)
# 7  
Old 10-01-2010
Code:
var=B
awk -v s=$var '
NR==1 {for (i=1;i<=NF;i++) if ($i==s) a[++j]=i}
{for (i=1;i<=j;i++) printf $a[i] FS;printf RS}
' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep values from column 2 in reference of column 1

Gents Is it possible to update the code to get the desired output files from the input list. I called variable to the first column. I need to consider the first column as key to grep the values in the second column according to the desired request. input list (attached ) output1 ... (12 Replies)
Discussion started by: jiam912
12 Replies

2. UNIX for Beginners Questions & Answers

Grep A Column Based On Column Name

I have a file with two columns separated by white space. Dog Cat fido sneaky dopey poptart ears whisker barky herd Trying to list the words under the column named Dog. Tried a few variations of awk but can't... (4 Replies)
Discussion started by: jimmyf
4 Replies

3. Shell Programming and Scripting

Can grep a specific column?

Hello All I have an input file with data below. I would like to grep and display the data where 3rd column contains string or at least one character. Kindly please help me with this! Input: tjfa3|zznpou|224fdd.34.ff3.35 |Tiv|Otj|1 fgduul7|zznikj| ... (7 Replies)
Discussion started by: DoveLu
7 Replies

4. Shell Programming and Scripting

How to grep from the third column?

PROL\tests_li004_developers:VAS:3543346:q34243,d3hs35,34bdf3,24sfgg,a3s234 Im trying to grep all text after VAS:3543346, so im trying to just have this q34243,d3hs35,34bdf3,24sfgg,a3s234 Im confused on how I would do this (7 Replies)
Discussion started by: ajetangay
7 Replies

5. UNIX for Dummies Questions & Answers

Grep -v value in 2nd column

Trying to do a grep -v on a value in the 2nd column of text. So if the word apple appears in a line in the 2nd column, it would not show up when the file was cat. Seems like a simple enough operation but I just can't figure it out. Any help would be appreciated. Thanks in advance. Are apples... (4 Replies)
Discussion started by: jimmyf
4 Replies

6. Shell Programming and Scripting

How to awk or grep the last column in file when date on column contains spaces?

Hi have a large spreadsheet which has 4 columns APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96034 Storage Mgmt Team APM00111803814 server_2 96152 GWP... (6 Replies)
Discussion started by: kieranfoley
6 Replies

7. Shell Programming and Scripting

Grep 5 biggest column

please help, file1.txt id,week,ict,outgoing_call,blackberry_problem,gprs_problem,sms_problem,flash_problem,sinyal_lemah,blankspot,incoming_call,mms_problem,kualitas_suara,drop_call,data_probl em,cross_connect,connect_no_voice,vas_problem ,1,sumbagsel,96,127,52,70,28,29,21,18,18,8,5,3,0,0,3... (1 Reply)
Discussion started by: radius
1 Replies

8. Shell Programming and Scripting

Grep but ignore first column

Hi, I need to perform a grep from a file, but ignore any results from the first column. For simplicity I have changed the actual data, but for arguments sake, I have a file that reads: MONACO Monaco ASMonaco MANUTD ManUtd ManchesterUnited NEWCAS NewcastleUnited NAC000 NAC ... (5 Replies)
Discussion started by: danhodges99
5 Replies

9. UNIX for Dummies Questions & Answers

Grep by column number

I have a data file that is arranged like this: Marketing Ranjit Singh Eagles Dean Johnson FULL Marketing Ken Whillans Eagles Karen Thompson FULL Sales Peter RobertsonGolden TigersRich Gardener PART President Sandeep Jain Wimps Ken Whillans CONT... (7 Replies)
Discussion started by: hitman247m
7 Replies

10. UNIX for Dummies Questions & Answers

Grep by column-Please help!

This is the sample of a data file that I have. It has 10 columns: 0,285,1,2,5,285,1,2,5,0 0,168,1,1,61,168,1,1,61,1512 0,102,3,1,60,102,3,1,60,1525 0,282,3,3,2,282,3,3,2,1503 0,167,3,4,10,167,3,1,24,1472 0,101,1,0,0,101,1,0,0,0 0,263,3,2,0,263,3,2,0,1470 0,245,2,1,50,245,2,1,50,1425... (15 Replies)
Discussion started by: bobo
15 Replies
Login or Register to Ask a Question