extract columns using grep or regular expressions


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers extract columns using grep or regular expressions
# 1  
Old 01-18-2012
extract columns using grep or regular expressions

I am trying to print columns from a table whose name (header) matches a certain string.

E.g.,

patient1001 patient1002 patient2005 patient3005 patient4001
0 0 0 0 0
2 9 2 8 3
2 7 3 0 2

Say I want to print columns whose names end with "01"

patient1001 patient4001
0 0
2 3
2 2

I tried:

awk '{ for (i=1; i<=NF; i++) {if ($i ~ /01$/); print $i; }}' file > file.txt

but that failed. Thanks!
# 2  
Old 01-18-2012
Code:
awk 'NR==1 {
            for (i=1; i<=NF; i++) { if ($i~/*01$/) {arr[i]=i} 
            next
       } 
       { for(j=1; j<=NF; j++) { 
            if(j in arr) {printf("%s ", $j)}
          }
          print ""
       } ' inputfilename

# 3  
Old 01-19-2012
Quote:
Originally Posted by jim mcnamara
Code:
awk 'NR==1 {
            for (i=1; i<=NF; i++) { if ($i~/*01$/) {arr[i]=i} 
            next
       } 
       { for(j=1; j<=NF; j++) { 
            if(j in arr) {printf("%s ", $j)}
          }
          print ""
       } ' inputfilename

No, this is not working. It gives me the following error:
awk: cmd. line:9: }
awk: cmd. line:9: ^ unexpected newline or end of string


jim mcnamara, is it possible for you to correct it?
# 4  
Old 01-19-2012
Jim, thanks very much for the reply. However, I don't get anything returned if I run this on my test.txt file (as in the first post, space delimited). I also got an error in the pattern matching in the first line--so below I've put the entire header that I wanted to search for.

awk 'NR==1 {
for (i=1; i<=NF; i++) { if ($i~/patient1001/) {arr[i]=i}
next
}
{ for(j=1; j<=NF; j++) {
if(j in arr) {printf("%s ", $j)}
}
print ""
}} ' test.txt
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

grep and regular expressions

Hi All, For the past many days I have solved a lot of grep and regular expression questions, Now I am in a search for a good quality set of questions that can help me build and check my knowledge of grep with regular expressions, it would be great if anyone could help me with my requirement. ... (1 Reply)
Discussion started by: rahulkalra9
1 Replies

2. Homework & Coursework Questions

Regular Expressions with GREP

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Given a text file (big_english.txt) containing roughly 250,000 words, answer the following using grep and... (2 Replies)
Discussion started by: blahblahblah123
2 Replies

3. Shell Programming and Scripting

How to grep using a line break in regular expressions?

Hi, I have a file as below, {#### if file then file else file } print file i need to fine the count of all the pattern - file, inside the { } i'm using a grep command as grep -c \{'*file*'\} fake.sh\ It doesn't gives me any result, i think the problem here is the... (5 Replies)
Discussion started by: divak
5 Replies

4. UNIX for Dummies Questions & Answers

Regular expressions

In regular expressions with grep(or egrep), ^ works if we want something in starting of line..but what if we write ^^^ or ^ for pattern matching??..Hope u all r familiar with regular expressions for pattern matching.. (1 Reply)
Discussion started by: aadi_uni
1 Replies

5. Shell Programming and Scripting

searching regular expressions with special characters like dot using grep

hi everybody I am a new user to this forum and its previous posts have been very useful. I'm searching in a file using grep for patterns like 12.13.444 55.44.443 i.e. of form <digit><digit>.<digit><digit>.<digit><digit><digit> Can anybody help me with this. Thanks in advance (4 Replies)
Discussion started by: jpriyank
4 Replies

6. Shell Programming and Scripting

How to extract text from string using regular expressions

Hi, I'm trying to use sed to extract some text and assign it to a variable. Can anyone provide me with some help? it would be much appreciated! I"m looking to extract for example: filename=/output/R34/2005_13_R34_C1042S_T83_CRFTXT_20081015.txt I'm trying to extract the 1042... (9 Replies)
Discussion started by: jtung
9 Replies

7. Shell Programming and Scripting

Help with regular expressions

I have following content in the file CancelPolicyMultiLingual3=U|PC3|EN RestaurantInfoCode1=U|restID1|1 ..... I am trying to use following matching extression \|(+) to get this PC3|EN restID1|1 Obviously it does not work. Any ideas? (13 Replies)
Discussion started by: arushunter
13 Replies

8. UNIX for Dummies Questions & Answers

grep and regular expressions :

I wrote a simple korn shell where I am trying to filter all the good record layouts of a file to only leave the bad ones to look at. That file is hudge. Aside from '# comments' and 'var=ssss', all record should follow a specific record layout, with comma seperated fields. Some fields can have any... (2 Replies)
Discussion started by: Browser_ice
2 Replies

9. UNIX for Dummies Questions & Answers

More Grep - Regular Expressions

Hey all! I'm trying to search a file and return all instances of a word, let's say 'foo' in this case, as long as it's not a function name. For example: 1) int foo; //OK 2) //'this is totally fooed up' is also OK 3) int foo (int x, int y) //not ok to return I've tried a lot of regular... (7 Replies)
Discussion started by: Jombee
7 Replies

10. Programming

regular expressions in c++

How do I use the regular expressions in c++? (2 Replies)
Discussion started by: szzz
2 Replies
Login or Register to Ask a Question