1

I'm trying to filter some mail logs and I need to get some specific information from certain columns that are not always the same. The information can be on different columns each line. I always use awk to print only the columns I want, like:

cat file.log | awk '{print $1" "$2" "$3}' >> output.txt

but in this case, I don't know which column contains the string I'm looking for.

Example file.log where I'm looking for columns that contain the string "5":

A B C 222 586 999 724 644  
A B C 510 333 987 678 633  
A B C 348 488 920 566 240  

Result I want:

A B C 586  
A B C 510  
A B C 566  

Any help is appreciated

1
  • if at all you can consider perl then you can do $perl -pe "s/^(\D+).*\b(5\d*).*/$1$2/" your_file.txt Commented Aug 30, 2018 at 5:08

3 Answers 3

2

Using a ~ /5/ and iterating on each columns on each rows/lines (KISS) :

$ awk '
    {
        for (i=1; i<=NF; i++) {
            if (i==1 || i==2 || i==3 || $i ~ /5/) {
                printf "%s ", $i
            }
         }
         print ""
    }
' file

Output:

A B C 586 
A B C 510 
A B C 566 
Sign up to request clarification or add additional context in comments.

Comments

1

a tricky perl one-liner

perl -anE 'say "@{[ @F[0..2], grep {/5/} @F[3..$#F] ]}"' file

where

  • -n iterates over the input file by line
  • -a splits each line into words, and stores into the @F array
  • @F[0..2] is the first 3 words; @F[3..$#F] is the list of the 4th up to the last word
  • grep {/5/} filters the list, returning only the words containing a "5"
  • "@{[ ... ]}" is a syntax trick to stringify a list as space-separated.

Can also use this, just as tricky but a little less "syntax-y"

perl -anE 'push @F, grep {/5/} splice @F,3; say "@F"'

2 Comments

I guess perl -pe "s/^(\D+).*\b(5\d*).*/$1$2/" will do the job
perl -ape "s/\d.*//s;$_=$_. join(' ', grep{/5/} @F).\"\n\"" file
1
$ awk '{
    for(i=4;i<=NF;i++)    # iterate fields starting from the 4th
        if($i~/5/)        # if there is a 5 in the field
            $3=$3 OFS $i  # append field value to the 3rd field
    print $1,$2,$3        # output 1st thru 3rd
}' file

Output:

A B C 586
A B C 510
A B C 566

1 Comment

perhaps OFS instead of " "?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.