0

With grep filter a file, from that filtered lines do a math operation on a specific column, only in the lines that contains a string "value=", return all the lines from the first grep filter and the lines where the math operation was done.

Example:

Use grep to filter lines that contains the string "update", this is easy, I got it.

output:
...
col1 col2 update col4 col5
col1 col2 update value= 320
col1 col2 update col4 col5
col1 col2 update value= 210
...

Now I need to select only the lines that contains the string "value=" and multiply the next column by 25, but still get lines that doesn't have the value= string

output:
...
col1 col2 update col4 col5
col1 col2 update value= 8000
col1 col2 update col4 col5
col1 col2 update value= 5250
...

I have tried using an if statement and awk, but only returns the lines where the math operation was done.

  • Can you show us what commands you're using to get these segments of output? You might be almost there... – roaima Nov 1 '19 at 18:55
  • You don't need grep when you're using awk. – Ed Morton Nov 3 '19 at 22:36
0

I got this:

awk '$4=="value=" {$5=($5*25)}1' foo
  • Thanks it works, so the comand will only do the operation if in the fourth column the string "value=" is present? I understand this right? – Tapioca Nov 1 '19 at 19:28
0

To execute math operations you must use awk or higher (sed and grep can not do math operations).

Replacing the grep in awk is quite simple, just use a regex /update/:

awk '/update/ { if ($4=="value=") { $5*=25 }; print }' ./file

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.