1

Example file having 6 columns with ";" separator:

temp;abcd;YES;1234;pqrs;YES

aaaa;bccc;YES;1234;pqrs;YES

ramy;uqq;YES;adda;1234;YES
..

..

..

Now Search for multiple patterns(pattern to be searched can be 1 or more) say temp and bccc, and replacing the 6th(or the last) delimited word with NO, in the line that matched the pattern.

i.e expected output should be

temp;abcd;YES;1234;pqrs;NO

aaaa;bccc;YES;1234;pqrs;NO

ramy;uqq;YES;adda;1234;YES

..
..
..

It would be really good to have in-file editing, as I would be using the code in a recursive loop, where multiple patterns to be searched would be dynamically assigned in a shell variable.

Something like this:

var='temp|abcd'

grep $var filename | based on pattern matched, replace the 6th or last work with a NO, with file having a ; delimiter.

Zong
  • 6,160
  • 5
  • 32
  • 46
Raghav Rao
  • 43
  • 1
  • 10

1 Answers1

0

Let's try to do it with !

Say your data is in a.txt file, and you write a small piece of code a.awk:

#!/bin/awk -f
BEGIN { OFS=";" ;  FS=";" ; print "start\n---------------" }
{
  if ( $0 ~ var )
  { print $1,$2,$3,$4,$5,"NO" }
  else
  { print $0 }
}
END { print "---------------\nend" }
  • NB1: interesting trick in here is how to pass a shell variable to hawk.
  • NB2: also note how you use ~ to parse the inside var
  • NB3: there is much useless code above, but it is good to understand how awk works: 3 parts, the middle part being repeated for each line of your input file.

Then you can call your code like below:

awk -f a.awk -v var="abcd|temp" a.txt

Or as a one-liner:

 awk -v var="abcd|temp" -F";" \
 '{ if($0~var){print $1";"$2";"$3";"$4";"$5";NO"} else {print} }'  a.txt

To understand more about it, here is a great tutorial.

Community
  • 1
  • 1
J. Chomel
  • 8,193
  • 15
  • 41
  • 69
  • Wow, Thanks so much, You made my day!!! I will just give it a try today or tomorrow, and will mark your answer as complete. – Raghav Rao Apr 29 '16 at 16:31
  • Just tried this and get the below error, this is on Unix Solaris just for your information. Can you please help me on what mistake i am doing? > awk -f ../Scripts/a.awk -v var="DEU0194|DEU0197|DEU0198|DEU0366" Reported.csv awk: syntax error near line 4 awk: illegal statement near line 4 awk: syntax error near line 6 awk: illegal statement near line 6 – Raghav Rao Apr 29 '16 at 17:02
  • Oh sorry, I don't have `solaris` here to try scripting. From the line number it looks like the `~`might be an issue. [Here](http://www.unix.com/shell-programming-and-scripting/120324-deleting-last-n-lines-output.html) someone suggest using `/usr/xpg4/bin/awk` ... do you have it installed? Do you still get same error if you replace `~`with `=`? – J. Chomel Apr 29 '16 at 19:11
  • Thanks, yes had the /usr/xpg4/bin/awk installed. :) This works, Only thing is, if there is any option to update the file inline would have been good, as i mentioned i would like to use this with in a recursive loop, so it would be a bit bad to put it into a temp file everytime. If possible, and you get to know about infile editing you can let me know. However, this returns the expected output, So thanks for that again. – Raghav Rao Apr 30 '16 at 15:13
  • Happy to read it works for you. Now, about recursion, I know it's always possible to replace it with a `while` loop. Since `a.awk` uses `var`, it's kind of dynamic. And yes with this technique you must do the changes over and over on a temporary file. – J. Chomel May 01 '16 at 07:06
  • Yes, i tried it and it perfectly works :) Thank for your solution! – Raghav Rao May 02 '16 at 13:50