Find pattern and replace using sed

Hi,
i want to replace the following lines in such a way that if the word merge exists in first column it must replace the 3rd column as M and if parse exists in first column then the last column must P, if neither it must mark it as X. I have tried the solution using awk, but it is saying nawk:empty regular expression

Input file:
INDmerge1 <dns > data
INDparse2 <dns > data
IND2 <dns > data

Result file:
INDmerge1 <dns > M
INDparse2 <dns > P
IND2 <dns > X

Can anyone please help here?
OS: SunOS 5.11

Show your attempt(s).

nawk -v host=$HOSTNAME '{
if(match($2,host) && !match($1,"#")) {
if(match($1,"merge"))
print $1"\t"$2"\tM";
else if(match($1,"parser"))
print $1"\t"$2"\tP";
else
print $1" \t"$2"\tX";
} }'

I can't see an "empty regular expression" in your attempt, except if $HOSTNAME doesn't expand. Do you get the quoted error message with above?
parser won't match your sample file, and the latter has > as third field.

Try

awk '{$4 = ($1 ~ /merge/)?"M":($1 ~ /parse/)?"P":"X"} 1' file
INDmerge1 <dns > M
INDparse2 <dns > P
IND2 <dns > X
1 Like

Yes, you are right! if i'm executing this bit of code standalone it doesnt throw any errors! however when this is plugged into a startup script it is giving that empty regular expression error.

Thanks a lot for you prompt reply.
I'll try the snippet you have suggested and let you know how it went!

--- Post updated at 11:58 AM ---

Unfortunately RudiC i'm getting this error now

awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

Didn't you say you're using nawk ?

1 Like

my bad! yes it was a copy paste error! :stuck_out_tongue:
Thanks RudiC so far so good the command is working when it is plugged in the startup script

Moderator comments were removed during original forum migration.