I’ve been racking my brain with this for the past half an hour and everything I’ve tried so far has failed miserably!
Within an html file, there is a field within tags, but the field itself is not separated with a space from the > sign so it’s hard to read with awk. I would basically like to add a single space after the opening tag, but gsub and awk are refusing to cooperate.
I’ve tried
awk 'gsub("class\\\'\\\'>","class\\\'\\\'>")' filename
since one backslash is needed to escape the single quote, the second to escape the backslash itself, and the third to escape the sequence \’ but Terminal (I’m working on a Mac) refuses to execute, and instead goes in the next line awaiting some other input from me.
Please help 🙁
In Bash, single quotes accept absolutely no kind of escape. Suppose e.g. I write this command:
Bash will consider the string opened by
'closed at the second', generating a string containing only\. The next', then, is considered the opening of a new string, so bash expects for more input in the next line (signalled by the>).If you are not aware of this fact, you may think that the string after the
echocommand below will be open yet, but it is closed:So, when you write
you are writing the string
gsub("class\\\concatenated with a backslash and a quote (\'); then a greater than signal. After this, the","is interpreted as a string containing a comma, because the single quote of the beginning of the expression was closed before. For now, the result is:After the comma, you have the string
class, followed by a backslash and a quote, followed by another backslash and another quote, and finally by a greater than symbol and a space. This is the current string:This is no valid awk expression! Anyway, it gets worse: the double quote
"will start a string, which will contain a closing parenthesis and a single quote, but this string is never closed!Summing up, your problem is that, if you opened a string with
'in Bash, it will be forcedly close at the next', no matter how many backslashes you put before it.Solution: you can make some tricks opening and closing strings with
'and"but it will become cumbersome quickly. My suggested solution is to put your awk expression in a file. Then, use the-fflag from awk – this flag will make awk to execute the following file:If you do not want to write a file, use the so called here documents: