0

I've tried numerous answers that I searched for here, and for some reason nothing is working

I have a bash script which I'd like to use to process a series of files.

Here's a simplified example

a sql file: foo.bar.users.sql:

-- mysql script

a bash script foo.sh which wants to insert a "use" statement at the top of the sql file

db=1ドル
cat foo.bar.users.sql | sed "1s/^/use $db;\'$'\n/" > tmp.txt; mv tmp.txt foo.bar.users.sql

running foo.sh this way

foo.sh foo

My desired goal is for foo.bar.users.sql to wind up like this:

use foo;
-- mysql script

I've tried so many variations of advice found so far, nothing works, here is what I've tried and what it does

script:

db=1ドル
cat foo.bar.users.sql | sed "1s/^/use $db;\'$'\n/" > tmp.txt; mv tmp.txt foo.bar.users.sql

result:

use foo;'$'n-- mysql script

script:

db=1ドル
cat foo.bar.users.sql | sed "1s/^/use $db;\\$'\n'/" > tmp.txt; mv tmp.txt foo.bar.users.sql

result

use foo;$'n'-- mysql script

script:

db=1ドル
cat foo.bar.users.sql | sed "1s/^/use $db;\\$'\\n'/" > tmp.txt; mv tmp.txt foo.bar.users.sql

result

use foo;$'n'-- mysql script

script:

db=1ドル 
cat foo.bar.users.sql | sed "1s/^/use $db;\\$\\n/" > tmp.txt; mv tmp.txt foo.bar.users.sql

result

use foo;$n-- mysql script

script:

db=1ドル 
cat foo.bar.users.sql | sed "1s/^/use $db;\$\n/" > tmp.txt; mv tmp.txt foo.bar.users.sql

result

use foo;$n-- mysql script

script:

db=1ドル 
cat foo.bar.users.sql | sed "1s/^/use $db;\'$'\n/" > tmp.txt; mv tmp.txt foo.bar.users.sql

result:

use foo;'$'n-- mysql script

script:

db=1ドル 
cat foo.bar.users.sql | sed "1s/^/use $db;\\\n/" > tmp.txt; mv tmp.txt foo.bar.users.sql

result:

use foo;\n-- mysql script

script:

db=1ドル 
cat foo.bar.users.sql | sed "1s/^/use $db;\\n/" > tmp.txt; mv tmp.txt foo.bar.users.sql

result:

use foo;n-- mysql script

script:

db=1ドル 
cr="\n"
cat foo.bar.users.sql | sed "1s/^/use $db;${cr}/" > tmp.txt; mv tmp.txt foo.bar.users.sql

result:

use foo;n-- mysql script

script:

db=1ドル 
cr="
"
cat foo.bar.users.sql | sed "1s/^/use $db;${cr}/" > tmp.txt; mv tmp.txt foo.bar.users.sql

result:

use foo;^M-- mysql script

script:

db=1ドル 
cr="$'\n'"
cat foo.bar.users.sql | sed "1s/^/use $db;${cr}/" > tmp.txt; mv tmp.txt foo.bar.users.sql

result:

use foo;$'n'-- mysql script

script:

db=1ドル 
cr="\n"
cat foo.bar.users.sql | sed "1s/^/use $db;\\${cr}/" > tmp.txt; mv tmp.txt foo.bar.users.sql

result:

use foo;\n-- mysql script

Any clues welcomed

asked May 14, 2021 at 21:13
2
  • 1
    I believe BSD (and by extension usually OSX) does not support \n as a representation of a newline (it turns it into \n). Which, as you can see, is extremely aggravating. I believe (but haven't tried it) that you can escape a new line - so in the sed quoted part do a \ and then hit return. Yes, it can be difficult. I use gawk :-) Commented May 14, 2021 at 21:24
  • 1
    Bash supports ANSI C quoting which means you can do this: nl=$'\n' in the script. I believe what you did with cr="<return>" is POSIX but when I looked it up, the example used single quotes. Getting ^M made me laugh out loud, sorry... Commented May 14, 2021 at 21:30

1 Answer 1

2

sed is great for editing within lines, but bad at working with line ends. If you must use sed, you could insert an unused character (like "§") and transform it into a CR later:

db=1ドル 
cat foo.bar.users.sql | sed "1s/^/use $db;\\§/" | tr "§" "\n" > tmp.txt; mv tmp.txt foo.bar.users.sql

but the easiest way might be to combine the output of echo and cat in order to create the prefix line:

db=1ドル 
(
 echo "use $db;"
 cat foo.bar.users.sql
) > tmp.txt; mv tmp.txt foo.bar.users.sql
answered May 14, 2021 at 21:36
3
  • OMG, that tr hack is horrible. I love it! Commented May 14, 2021 at 21:39
  • You're right I was fixated on using sed. love this answer, thanks! Commented May 14, 2021 at 21:41
  • 1
    @slashdottir I admire your systematic approach with sed though; did the same thing at the time :) Commented May 15, 2021 at 8:02

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.