lundi 18 octobre 2021

With awk, how to match the backslash character when followed by single quote (yes that is two characters)

I've a log file which is periodically generated by an automated process I do not control. There are many instances in this log file of a backslash character (not as an 'escape') immediately followed by a single quote mark \'. Thus it is a two character text pattern.

I am looking for awk or sed examples which will match the two character \' pattern, and replace it with the single character pattern of a single quote '. In my environment, I'm limited to bash and standard gnu-utils; fancy stuff like python or perl aren't available to the target environment where the solution to this problem will operate.

I've attempted:

cat F1.log | awk '{gsub(/\\t/,"\t")}1' | awk '{gsub(/\\'/,"\'")}1' > F2.log

cat F1.log | awk '{gsub(/\\t/,"\t")}1' | sed 's/$(echo "\134\047")/$(echo \047")/g' > F2.log

Side note: The 1st in-line awk has been a long-standing working tool to match/transform the two-character pattern of \t instances into the single character t. I simply added a new in-line awk instance of the same command and replaced the t with ', but the result isn't syntactically correct.

For the 1st, the error is: -bash: syntax error near unexpected token `)'

For the 2nd, the error is: sed: -e expression #1, char 36: Invalid back reference

Here is an instance from a recent log file with an appropriate data line piped to od -bc:

0000000 040 040 040 040 040 040 040 040 040 040 040 040 040 040 040 147
                                                                      g
0000020 160 147 072 040 153 145 171 142 157 170 040 134 047 057 164 155
          p   g   :       k   e   y   b   o   x       \   '   /   t   m
0000040 160 057 056 147 156 165 160 147 057 160 165 142 162 151 156 147
          p   /   .   g   n   u   p   g   /   p   u   b   r   i   n   g

I know I've got something wrong with single/double-quotes and/or paren's. I have extensively searched both StackOverflow first and then google, without finding any solution.

Your help is greatly appreciated, thank you.

Aucun commentaire:

Enregistrer un commentaire