awk print all fields except matching regex


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users awk print all fields except matching regex
# 1  
Old 06-17-2013
awk print all fields except matching regex

grep -v will exclude matching lines, but I want something that will print all lines but exclude a matching field. The pattern that I want excluded is '/mnt/svn'

If there is a better solution than awk I am happy to hear about it, but I would like to see this done in awk as well. I know I can use sed to substitute it away, but I wanted something a little more eloquent.
# 2  
Old 06-17-2013
Code:
awk '!/\/mnt\/svn/' file

# 3  
Old 06-17-2013
Quote:
Originally Posted by Yoda
Code:
awk '!/\/mnt\/svn/' file

That seems to cause the entire line which contains the pattern to be excluded I want all lines to be printed but the pattern to be removed, equivalent to
Code:
 sed 's|/mnt/svn||g'

# 4  
Old 06-17-2013
I misread your requirement:
Code:
awk '{gsub(/\/mnt\/svn/,x)}1' file

# 5  
Old 06-17-2013
Quote:
Originally Posted by Yoda
I misread your requirement:
Code:
awk '{gsub(/\/mnt\/svn/,x)}1' file

Ah, yes gsub.. I actually knew that one! I was wondering if there is a way to do something more along the lines of
Code:
 awk '  for ( i=0; i<NF; i ++) { if $i is not /pattern/ }'

Please excuse the awful pseudo code.
# 6  
Old 06-17-2013
Code:
awk '{ for(i=0;i<=NF;i++) { if($i !~ /\/mnt\/svn/) s=s ? s OFS $i : $i } print s; s="" }' file

This User Gave Thanks to Yoda For This Post:
# 7  
Old 06-17-2013
Quote:
Originally Posted by Yoda
Code:
awk '{ for(i=0;i<=NF;i++) { if($i !~ /\/mnt\/svn/) s=s ? s OFS $i : $i } print s; s="" }' file

Perfect! Would you mind explaining this code a bit
Code:
 s=s ? s OFS $i : $i } print s; s=""

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print fields that match using conditions and a default value for non-matching in two files

Trying to use awk to match the contents of each line in file1 with $5 in file2. Both files are tab-delimited and there may be a space or special character in the name being matched in file2, for example in file1 the name is BRCA1 but in file2 the name is BRCA 1 or in file1 name is BCR but in file2... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Beginners Questions & Answers

Awk: matching multiple fields between 2 files

Hi, I have 2 tab-delimited input files as follows. file1.tab: green A apple red B apple file2.tab: apple - A;Z Objective: Return $1 of file1 if, . $1 of file2 matches $3 of file1 and, . any single element (separated by ";") in $3 of file2 is present in $2 of file1 In order to... (3 Replies)
Discussion started by: beca123456
3 Replies

3. Shell Programming and Scripting

Print matching fields (if they exist) from two text files

Hi everyone, Given two files (test1 and test2) with the following contents: test1: 80263760,I71 80267369,M44 80274628,L77 80276793,I32 80277390,K05 80277391,I06 80279206,I43 80279859,K37 80279866,K35 80279867,J16 80280346,I14and test2: 80263760,PT18 80279867,PT01I need to do some... (3 Replies)
Discussion started by: gacanepa
3 Replies

4. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

5. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

6. UNIX for Dummies Questions & Answers

Regex matching column awk

Hi all, I want to extract rows with the pattern ALPHANUMERIC/ALPHANUMNERIC in the 2nd column. I dont wan rows with more than 1 slash or without any slash in 2nd column. a a/b b a/b/c c a/b//c d t/y e r f /f I came up with the regex grep '\/$' file a a/b b a/b/c d t/y (3 Replies)
Discussion started by: jianp83
3 Replies

7. Shell Programming and Scripting

Awk: adding fields after matching $1

Dear AWK-experts! I did get stuck in the task of combining files after matching fields, so I'm still awkward with learning AWK. There are 2 files: one containing 3 columns with ID, coding status, and score for long noncoding RNAs: file1 (1.txt) (>5000 lines) ... (12 Replies)
Discussion started by: kben
12 Replies

8. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

9. Shell Programming and Scripting

sed - print only matching regex

Hi folks, Lets say I have the following text file: name, lastname, 1234, name.lastname@test.com name1, lastname1, name2.lastname2@test.com, 2345 name, 3456, lastname, name3.lastname3@test.com 4567, name, lastname, name4.lastname4@test.com I now need the following output: 1234... (5 Replies)
Discussion started by: domi55
5 Replies

10. Shell Programming and Scripting

AWK Matching Fields and Combining Files

Hello! I am writing a program to run through two large lists of data (~300,000 rows), find where rows in one file match another, and combine them based on matching fields. Due to the large file sizes, I'm guessing AWK will be the most efficient way to do this. Overall, the input and output I'm... (5 Replies)
Discussion started by: Michelangelo
5 Replies
Login or Register to Ask a Question