1

I have the next fragment of the output of a log:

Server Server: Server
Date and Time: June 04 2018 14:10:00
--

Product     Version         User         Host             IP             Duration

Software1      11.0     user               user         *.**.***.***     10:08
Software2      11.0      user                user        *.**.***.***     10:05
===================================================================================

I want with cut command only filter to the output like:

Product     Version         Host             IP             Duration

Software1      11.0    user             *.**.***.***     10:08
Software2      11.0     user            *.**.***.***     10:05

Also, I want to know if is possible apply the same filter many times, because the first example above, it repeats himself in the whole file.

  • The cut command selects columns (fields) to retain, but it does not help with filtering lines. From the example it appears the goal is to do both. It is not clear from the example if lines can be filtered strictly by pattern matching (eg. grep), but that's a possibility. Or it might be necessary to use an awk script or some other scripting language. It's not really clear from the sample fragments. – JonDeg Jul 26 '18 at 4:48
0

Using awk:

 awk '/--/{flag=1; next} /====/{flag=0} flag' filename

Using SED:

 sed -n '/--/,/===/{/--/!{/===/!p}}' filename

Using grep:

grep -E 'Product|Mathematica|MathKernel|Syntax' filename
1

You can't really use cut in this case as the columns of the data are not clearly defined. The cut utility works by cutting at exact numbers, e.g. exactly at 3 spaces or tabs or whatever delimiter you have. In this case, you will need to use awk which splits its input lines on any number of consecutive whitespaces (by default).

$ awk -v OFS="\t" '/^==/ { next } NR > 4 { print $1, $2, $4, $5, $6 }' file | column -t
Product    Version  Host  IP            Duration
Software1  11.0     user  *.**.***.***  10:08
Software2  11.0     user  *.**.***.***  10:05

The awk program skips any line starting with == and any line before the 5th line. For all other lines, it prints a selection of the fields (not the 3rd, User).

The column -t at the end formats the result into a nice table.

0

I found the simplest solution to this:

awk '{print $1,$2,$4,$5,$6}' file1.log > file2.log

This allows only print my preferred columns

Product     Version         Host             IP             Duration

Software1       11.0         User          *.**.***.***     10:08
Software2        11.0        User           *.**.***.***     10:05

I didn't realize the important use of awk

  • This is different from the expected output in the question. It furthermore does not produce that output for the data in the question. – Kusalananda Jul 26 '18 at 7:59
  • The awk code is also faulty (the parentheses should be curly braces). – Kusalananda Jul 26 '18 at 8:47
  • Mmm I don't think so @Kusalananda, because I had not knowledge about awk, if objective case I accomplished the objective: Give a specific Output. But I'll try to be more specific with alternatives in a future; Thank you for your interest – Mareyes Jul 26 '18 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.