1

I want to INSERT a string with "'" as special character in multiple files. All the files in which I want to insert have a line after which I want to perform my INSERT.

eg:

File Before INSERT:
...
FROM LOCAL :LOAD_FILE
REJECTED DATA :REJECT_PATH
...
File After INSERT:
...
FROM LOCAL :LOAD_FILE
DELIMITER AS '|'
REJECTED DATA :REJECT_PATH
...

I've tried writing down many SED commands but they are generating errors. One of them is:

sed 'LOAD_FILE/a/ DELIMITER AS \'\|\'/g' SOURCE > DESTINATION

4 Answers4

2
awk  -v line='DELIMITER AS '"'|'"'' '1; /LOAD_FILE/{print line }' input
FROM LOCAL :LOAD_FILE
DELIMITER AS '|'
REJECTED DATA :REJECT_PATH
P....
  • 17,421
  • 2
  • 32
  • 52
1

Using surrounding double quotes:

sed "/FROM LOCAL :LOAD_FILE/s//&\nDELIMITER AS '|'/" file

or single quotes (safer to avoid unwanted variable expansion):

sed '/FROM LOCAL :LOAD_FILE/s//&\nDELIMITER AS '"'|'"'/' file
SLePort
  • 15,211
  • 3
  • 34
  • 44
1

This might work for you (GNU sed):

sed '/LOAD_FILE/aDELIMITER AS '\'\|\' file

This appends the line DELIMITER AS \'\|\' following the match on LOAD_FILE

N.B. The sed command is in two parts, the /LOAD_FILE/aDELIMITER AS is concatenated with \'\|\'

If you prefer:

sed 's/LOAD_FILE/&\nDELIMITER AS '\'\|\''/' file
potong
  • 55,640
  • 6
  • 51
  • 83
0

Another way of putting it :

sed -e ':a;N;$!ba;s/LOAD_FILE\n/LOAD_FILE\nDELIMITER AS \x27|\x27\n/g'

about syntax I used :

How can I replace a newline (\n) using sed?

Community
  • 1
  • 1
JChetan
  • 13
  • 3