Use of character range in awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Use of character range in awk
# 1  
Old 10-10-2011
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 :
Code:
...
ATOM     21  C   THR A   4      23.721 -26.194   1.909  1.00 32.07           C    
ATOM     22  O   THR A   4      24.874 -26.014   1.536  1.00 32.36           O    
ATOM     23  CB  THR A   4      22.548 -26.219  -0.334  1.00 31.56           C    
ATOM     26  N   CYS A   5      23.261 -25.726   3.072  1.00 32.18           N    
ATOM     27  CA  CYS A   5      24.119 -24.983   4.010  1.00 32.76           C    
...

It has thousands of lines like this. I wish to sort some data out, but only when column 5 (here an "A") is within a range of letters accessible via the variable data. data is a user input and can be any letters in the range A-Z, in any combinaison (i.e. "F", "ABCFIZ", "A-GX", "KJHGF", ...). So I do this :
Code:
awk '{if ($5 == "'[$data]'" ) {do_something}}' $in >> $out

That it is not working. If I put a single letter (e.g.
$5 == "A"), it works just fine, but as long as I want a variable, it doesn't output anything. I tried A LOT of combinaison around the metacharacters [] , like : "'[$data]'", "'['$data']'", "'\[$data\]'", "'["$data"]'", etc and nothing works - and often yielded errors. I'm out of ideas, if anyone has a suggestion... Smilie

I may add, for precision sake, that the quoted awk code is within a much longer script, called from and programmed into tcsh.
# 2  
Old 10-10-2011
Code:
nawk -v r='A-C' '$5 ~ "[" r "]"' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 10-10-2011
Thanks vgersh99, it does work.

That being said, nawk raise a "Command not found" error on my system. But it works fine with awk as well apparently.

many thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Removing letters after a certain character within a range of columns

Hi there, I am trying to remove al letters after : character on specific columns from 10th column till 827. I used sed and cut to do so but I am sure there is better one liner someone can think of from unix community members. Huge file but it has this structure (Total number of Columns =... (10 Replies)
Discussion started by: daashti
10 Replies

2. Shell Programming and Scripting

sed random \n for "n" range of character occurrences

I'd like to put paragraph breaks \n\n randomly between 5 - 10 occurrences of the dot character (.), for an entire text file. How to do that? In other words, anywhere between every 5 -10 sentences, a new paragraph will generate. There are no other uses of the (.) except for sentence breaks in... (11 Replies)
Discussion started by: p1ne
11 Replies

3. Shell Programming and Scripting

Grep - build character range with octal or hexa representation

Hello I would like to make a character range like that : echo "ABCDEF+1234" | grep -E ''or echo "ABCDEF+1234" | grep -E ''or echo "ABCDEF+1234" | grep -E ''Which should works on linux with english language And works also on linux with french language ( english install and after add french )... (4 Replies)
Discussion started by: jcdole
4 Replies

4. Shell Programming and Scripting

Replacing character "|" in given character range

Hi I am having file : 1|2443094 |FUNG SIU TO |CLEMENT 2|2443095 |FUNG KIL FO |REMENT This file contains only 3 fields delimeted by "|". Last field is a decsription filed and it contains character "|". Due to this my output if breaking in 4 fields. I... (7 Replies)
Discussion started by: krsnadasa
7 Replies

5. UNIX for Advanced & Expert Users

Finding a specific range of character in file

hi, I want to store from 102 character to 128 character to a variable of header record which can be identified as 'HDR' which is the first 3 characters in the same line of a same.txt file. Please advise. Thanks (4 Replies)
Discussion started by: techmoris
4 Replies

6. 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

7. 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

8. Shell Programming and Scripting

need help in expanding hexa decimal character range

Hi all, I have a input like this 3AF9:3B01 and need to expand to the below output 3AF9 3AFA 3AFB 3AFC 3AFD 3AFE 3AFF 3B00 3B01 Please let me know the easiest way for achieving this. Thanks for the help in advance... (6 Replies)
Discussion started by: leo.maveriick
6 Replies

9. Shell Programming and Scripting

How to print range of lines using sed when pattern has special character "["

Hi, My input has much more lines, but few of them are below pin(IDF) { direction : input; drc_pinsigtype : signal; pin(SELDIV6) { direction : input; drc_pinsigtype : ... (3 Replies)
Discussion started by: nehashine
3 Replies

10. 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
Login or Register to Ask a Question