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.

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

 sed 's|/mnt/svn||g' 

I misread your requirement:

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

 awk '  for ( i=0; i<NF; i ++) { if $i is not /pattern/ }' 

Please excuse the awful pseudo code.

awk '{ for(i=0;i<=NF;i++) { if($i !~ /\/mnt\/svn/) s=s ? s OFS $i : $i } print s; s="" }' file
1 Like

Perfect! Would you mind explaining this code a bit

 s=s ? s OFS $i : $i } print s; s="" 

That is a conditional expression

If variable s is already defined, it appends $i value separated by OFS (space by default) else assigns just $i value.

Outside for loop the code prints s and resets s back to null.

Why not just do

 awk '{ for(i=0;i<=NF;i++) { if($i !~ /\/mnt\/svn/) {print $i} }' 

This code will print each field followed by a newline. But the code that I posted will help preserve the lines in your input file.

Is /mnt/svn embedded in a longer string or is it the entire field? If it is embedded, do you want to remove the entire string or just the part /mnt/svn ? If just the part, above solution will fail, as it will remove the entire field.

It is an entire field, delimited by space. Yoda's most recent answer worked.