Before this gets closed as a dup, lets at least clean up your code. This:
awk '/'"'"'test'"'"' =>./{c++}(c==2){sub("'"'"'test'"'"' =>.","'"'"'test'"'"' => '"'"'test1'"'"',")}1' testfile
is extremely hard to read. I assume all those '"'"'
s are trying to get single quotes into the code. If so, to improve clarity and so it'd work if/when the script is stored in a file, use the octal representation \047
for every single quote instead:
awk '/\047test\047 =>./{c++} (c==2){sub("\047test\047 =>.","\047test\047 => \047test1\047,")}1' testfile
Now use regexp delimiters for the regexp that's the first arg to sub()
:
awk '/\047test\047 =>./{c++} (c==2){sub(/\047test\047 =>./,"\047test\047 => \047test1\047,")}1' testfile
There are several other possible improvements including using a backreference instead of hard-coding the original sub()
string in the replacement and using match()
so you don't need to test for the same regexp in the condition part of the script and then again in the sub()
so something like this (with GNU awk for the 3rd arg to match()) is probably all you need:
awk 'match($0,/(.*\047test\047 =>.)(.*)/,a){c++} c==2{$0=a[1] "\047test1\047" a[2]} 1' testfile
but without sample input output we can't know for sure - post a new question with sample input/output if you'd like more help.