2

I am trying to replace the customernumber in my text file with the input assigned to the variable named input. But unable to complete this using sed, Below is the method am trying

input=$(cat customernumber.txt)
echo $input
cat CUSTOMER_REPORT.txt | sed -e s/%customernumbers%/$input/g > temp.sql

My customernumber.txt contains the values (12345,67890), my CUSTOMER_REPORT.txt contains the below values:

SELECT CUSTOMERNUMBER,ORDERNUMBER
FROM ORDER WHERE CUSTOMERNUMBER IN %customernumbers% WITH UR;

I am getting the below error:

sed: -e expression #1, char 31: unterminated `s' command

How to replace the %customernumbers% with the value in $input? Is there any other option available other than sed?

P_A
  • 1,724
  • 1
  • 16
  • 28
simbu3306
  • 23
  • 3
  • see also https://mywiki.wooledge.org/Quotes and https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed – Sundeep Jan 31 '18 at 06:53

1 Answers1

0

You should put your substitution sed commands between double quotes

sed -e "s/%customernumbers%/$input/g" > temp.sql

After debugging a bit:

cat CUSTOMER_REPORT.txt | sed -e "s/%customernumbers%/(12345,67890)/g" > temp.sql 

was working fine so we narrowed the root cause until we reached the file customernumber.txt by using wc -l customernumber.txt or cat -vTE customernumber.txt

after a bit of fixing it works fine.

Allan
  • 11,170
  • 3
  • 22
  • 43
  • Tried the same but getting the same error cat CUSTOMER_REPORT.txt | sed -e "s/%customernumbers%/$input/g" > temp.sql sed: -e expression #1, char 31: unterminated `s' command – simbu3306 Jan 31 '18 at 06:55
  • 1
    ok let's debug this together!!! ;-) try this command and let me know if it works: `cat CUSTOMER_REPORT.txt | sed -e "s/%customernumbers%/(12345,67890)/g" > temp.sql` If it works than it means that the problem does not come from the sed command itself but to what is contained in the variable, (example an empty line at the end of the `customernumber.txt` file – Allan Jan 31 '18 at 06:57
  • yes, the above one works cat temp.sql SELECT CUSTOMERNUMBER,ORDERNUMBER FROM ORDER WHERE CUSTOMERNUMBER IN (12345,67890) WITH UR; – simbu3306 Jan 31 '18 at 06:59
  • 1
    ok then the problem does not come from the `sed` command but from your variable `$input` I suspect there is an `EOL` at the end of it could you do the following command: `cat -vTE customernumber.txt` and `wc -l customernumber.txt` and give me the inputs? – Allan Jan 31 '18 at 07:02
  • 1
    Great, the problem is with the customernumber.txt file. I have corrected it now and its working as expected..Thank you !! – simbu3306 Jan 31 '18 at 07:04
  • you are welcome! ;-) happy to help you!!! Keep in mind to always try to get the problem location with a step by step approach! In the end you will reach it for sure ;-) – Allan Jan 31 '18 at 07:04
  • Capturing things in a shell variable so you can then substitute them sounds like a very roundabout way to do things. You should probably be looking at parametrizing your SQL query anyway, which will make all of this moot. – tripleee Jan 31 '18 at 08:22
  • Tangentially, [the `cat` is useless](https://stackoverflow.com/questions/11710552/useless-use-of-cat) – tripleee Jan 31 '18 at 08:23