AWK, read a range of coloumns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers AWK, read a range of coloumns
# 1  
Old 12-21-2009
AWK, read a range of coloumns

Hi,

I've a file with some coloumns (separated by coma) and some lines, like so:

Code:
A,B,C,D
1,2,3,4
5,6,7,8 
9,1,2,3

I want to print a range of coloumns: for example all coloumns between the coloumn with the charachter B in the first line, and the coloumn with the char D in the first line.

like so:

Code:
C
3
7 
2


I don't know how many coloums are there before and after this range, I know only the String in the first line.

Can you help me Smilie?


Saludos.

Last edited by radoulov; 12-21-2009 at 03:05 PM.. Reason: Please use code tags!
# 2  
Old 12-21-2009
How about:
Code:
awk -F, -v v1="B" -v v2="D" 'NR==1{for (i=1;i<=NF;i++) if ($(i-1)==v1 && $(i+1)=v2) c=i}{print $c}' infile

output:
Code:
C
3
7
2


Last edited by Scrutinizer; 12-21-2009 at 03:57 PM..
# 3  
Old 12-21-2009
Code:
awk -F, 'NR == 1 { 
  for (; ++i<=NF;) {
    $i == from && _from = i
    if ($i == to) { _to = i ; break }
    }
  } 
{ 
  for (i=_from; ++i<_to;)
    printf "%s", $i (i == _to - 1 ? RS : FS)
  }' from=B to=D infile

I used a for loop to handle a range of columns.

Last edited by radoulov; 12-21-2009 at 03:55 PM.. Reason: Corrected.
# 4  
Old 12-23-2009
How can I print all coloumns between a coloumn which contains the charachter B and the last coloumn of the file?

so:

Code:
C,D
3,4
7,8 
2,3


Last edited by radoulov; 12-23-2009 at 03:54 PM.. Reason: Added code tags.
# 5  
Old 12-23-2009
This is a new version:

Code:
awk -F, 'NR == 1 { 
  for (; ++i<=NF;) {
    if ($i == from) { _from = i
      if (!to) { _to = ++NF; break }
     }         
    if ($i == to) { _to = i ; break }
    }
  } 
{ 
  for (i=_from; ++i<_to;)
    printf "%s", $i (i == _to - 1 ? RS : FS)
  }' from=B to=D infile

To get the rest of the columns just remove the to= part,
like this:


Code:
awk -F, 'NR == 1 { 
  for (; ++i<=NF;) {
    if ($i == from) { _from = i
      if (!to) { _to = ++NF; break }
     }         
    if ($i == to) { _to = i ; break }
    }
  } 
{ 
  for (i=_from; ++i<_to;)
    printf "%s", $i (i == _to - 1 ? RS : FS)
  }' from=B infile

# 6  
Old 12-23-2009
Code:
awk -F, -v v1="B" -v v2="D" 'NR==1{for (i=1;i<=NF;i++) if ($(i-1)==v1 && $(i+1)=v2) c=i}
                             {print $c","$(c+1)}' infile


Last edited by Scrutinizer; 12-23-2009 at 04:48 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk field range

I was reading about different methods of awk field ranges. Does the second method have any advantage? Its quite a bit longer so I am wondering what the advantage is. awk '{for(i=5;i<=9;i++)printf "%s ",$i;print ""}' filename ... (2 Replies)
Discussion started by: cokedude
2 Replies

2. Shell Programming and Scripting

read into a range of character

i have this problem: i must hide a string with a character such as _ by command WORD=string; XXX=`echo $WORD | sed 's//_/g' but after, users must send in input a character and i must to replace the _ with the input character or better i can do this -$CHARS_INPUT i have think to use command... (3 Replies)
Discussion started by: tafazzi87
3 Replies

3. UNIX for Dummies Questions & Answers

Use of character range in awk

Hi all, I am having a bit of a hard time using awk. I must do something wrong, but I don't know what... Any help would be greatly appreciated! I read a file, as follows :... ATOM 21 C THR A 4 23.721 -26.194 1.909 1.00 32.07 C ATOM 22 O THR A 4 ... (2 Replies)
Discussion started by: hypsis
2 Replies

4. UNIX for Dummies Questions & Answers

Merging two text files with variable coloumns

Hi All, I have two text files containing space delimited columns. The first file contains 9 columns and the second one contain 3 columns. I want to copy the 3 coloumns from the 2nd file and paste them in 1st file after 9 coloumns. Ex. File1.txt contains 9 coloumns C1 C2 C3 C4 C5 C6 C7 C8 C9... (6 Replies)
Discussion started by: Unilearn
6 Replies

5. Shell Programming and Scripting

range patterns in awk

Hi All, I am new to awk command and I had a question in using it. I want to filter a code file and print specific functions (that contain menu word in the function name). for example, if the file has: function menu1() { } function f2() { } function menu3() { }so I want... (5 Replies)
Discussion started by: ghoda2_10
5 Replies

6. Shell Programming and Scripting

awk search within a range.

I have this kind of file ABC UUIIIIIIIIIIII , HJHKJKL XYZ HHJJJJJJMMM ABC BBOOIO, PPLIOJK XYZ NMJKJKK ABC MMMM ABC OPOPO XYZ LLKLKLL I need to get all data from ABC till XYZ so output should be UUIIIIIIIIIIII (6 Replies)
Discussion started by: dinjo_jo
6 Replies

7. Shell Programming and Scripting

range in if using awk

Hi All, I would like to assign the following values to each column in my file. if $i is between 1 and -1 (ie -1 < $i < 1) then print A; if $i is between -2 and -1 && 1 and 2 (ie. -2 < $i < -1 && 1 < $i < 2) then print B; if $i is between -3 and -2 && 2 and 3 (ie. -3 < $i < -2 && 2 < $i < 3)... (1 Reply)
Discussion started by: Fredrick
1 Replies

8. Shell Programming and Scripting

Read Write byte range/chunk of data from specific location in file

I am new to Unix so will really appreciate if someone can guide me on this. What I want to do is: Step1: Read binary file - pick first 2 bytes, convert from hex to decimal. Read the next 3 bytes as well. 2 bytes will specify the number of bytes 'n' that I want to read and write... (1 Reply)
Discussion started by: Kbenipel
1 Replies

9. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

10. Shell Programming and Scripting

reversing the rows and coloumns ina given matrix?

hi plz can any body giv ethe code to perform transpose matrix of agiven matrix in shell scripts program????? (1 Reply)
Discussion started by: chaitunanduri
1 Replies
Login or Register to Ask a Question