2

Ok I am locked in a situation where I am trying to insert a line based on a search string . I am familiar doing this w AWK and SED using a single work or something uncomplicated.

How I'd like to insert a string that has Single Quotes and Brackets e.g. this will work

     awk '/('Backup/ {print "NEWLINE"  } {print}' filename.sh 

and so will this

    sed -e '/\(\'backup/i\
    newline ' filename.sh

Instead of newline work- I want this " ('Drop the no good white.cap ;' ) , "

everything enclosed within the " and "

2 Answers 2

2

What you're trying to do has nothing to do with awk or sed. For example, see:

print " '\'' data_between_two_single_quotes '\'' "

The first ' character closes the opening ' shell string literal. The shell literal does not support a backslash escape for this. The sequence '\'' does the trick: it closes the single-quote literal, specifies the quote character (using an escape that is supported outside of single-quote literals) and then re-opens a new single-quote literal. You can think of it as a four-character escape sequence to get a single quote.

So, your awk command becomes:

awk '/\('\''Backup/ {print " ('\''Drop the no good white.cap ;'\'' ) , " } { print }' filename.sh 

And your sed command becomes:

sed -e '/\('\''Backup/i\
('\''Drop the no good white.cap ;'\'' ) , ' filename.sh

EDIT:

for i in *; do awk -v database="$DB" -v table="$TB" 'BEGIN { print "header" } /backup/ { print " ('\''DROP TABLE " database "." table ";'\'' ) , " }1' "$i" > "${i}.new"; done

Explanation:

Loop over all files in the directory using a shell for loop. Add a header to the files with the BEGIN block. Find the word backup and print a line containing the shell variables DB and TB. ... }1' at the end of the awk script enables printing by default. It's shorthand for { print }. HTH.

13
  • awk '/BACKUP/{print " ('\''Drop the no good white.cap ;'\'' ) , " } { print }' filename.sh does seem to work but lets say I have variables inside ... awk '/BACKUP/{print " ('\''DROP TABLE $DB.$TB ;'\'' ) , " } { print }' filename.sh How can we expand them. THANKS SO MUCH for your help Dec 4, 2012 at 9:01
  • sorry that was a dup of the above post- Saul Dec 4, 2012 at 9:02
  • it gives syntax error awk: syntax error near line 1 awk: bailing out near line 1 Dec 4, 2012 at 13:22
  • I think you use awk -v database=$DB -v table=$TB '$0 ~ database , $0 ~ table' filename BUT I dont know how to couple the logic with the rest of the stuff that follows Dec 4, 2012 at 13:54
  • Hi Steve I dont see the edit . Can you pl repost.. thanks galore Dec 4, 2012 at 13:55
1

Another way using awk:

Keep your content to be inserted in a separate file:

$ cat file1
 " ('Drop the no good white.cap ;' ) , "

Using awk:

$ awk '/\('\''Backup/{getline x<"file1";print x;}1'  filename.sh

Using getline, the line in the file1 is read into the variable x, and printed on encoutering a pattern. This will allow you not to you any escape characters to escape the quotes.

1
  • even this one awk -v database="$DB" -v table="$TB" '/BACKUP/ {print " ('\''DROP TABLE " database "." table ";'\'' ) , " } { print }' filename.sh gives the same error Dec 4, 2012 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.