\n have to make a newline forever?

I'm trying to make a little script, but I have a problem...

I'm trying to sed a list of files made with a ls > filename.txt...

Two variables (or i may call them constant because they are fixed values :D):
ststr1 and ststr2

I want to

sed s/"$ststr1"/"$ststr2"/g filename.txt > listofsqlcommands.txt

The problem is that in ststr2 there is a string like "into table xxxxxx field terminated by ',' lines terminated by '\n' (col1, col2, col3 etc. etc.);"...

I can't change that because these files are created on windows and BY DESIGN when u create a text file with any method u get a newline at the end of file... :X

So, when i create the file with the list of sql command which have to insert the data of every file listed in filename.txt

filename.txt

 filename1
 filename2
 filename3...

listofsqlcommands.txt

 insert filename1 data into
 insert filename2 data into
 insert filename3 dat....

I get this result:

 
insert blah blah... ...lines terminated by '
' (col1, col2, col3...);

It's possible to made it like:

 
insert blah blah... ...lines terminated by '\n' (col1, col2, col3...);

????????

To make it simple, it's possible when sed a file that contains \n to avoid that the output file redirected contains a REAL newline????

Thx to everyone and sorry for my terrible way of speaking... :slight_smile:

Maybe silly..., but have you tried to convert your .txt file using dos2ux or similar before any further processing?

You could try double-escaping the \n to make it interpret it as a literal backslash...

ststr2=$(sed 's/\\n/\\\\n/g' <<< "$ststr2")

On the other hand, I think strings like

lines terminated by '
'

are actually valid, albeit ugly :smiley:

Thx all for the suggest...
I try right now...

P.S. Ugly but short and functional? What's the matter... Better than long and heavy... Ops! and beautiful obviously... :smiley:

I think you misunderstood me, I agree with that sentiment, to a point. :wink: Actually, you might want to convert all backslashes into double-backslashes to prevent sed from eating them. You should also escape forward slashes in case there's a forward slash in ststr2, that could bomb your script. And do that for ststr1 too.

ststr2=$(sed 's/\\/\\\\/g;s/\//\\\//g' <<< "$ststr2")
ststr1=$(sed 's/\//\\\//g' <<< "$ststr1")

Ugh, that almost looks like ASCII art... Too bad there's no way I know of to just tell sed not to escape anything in the input string, that'd make this easy. Or if there is a way, my sed knowledge isn't strong enough to find it.

Great!
Corona688 you are a lifesaver! Although your code has to be refined like this:

ststr2=$(sed 's/\\n/\\n/g' <<< "$ststr2")

so the result in the file are now:

 
insert blah blah... ...lines terminated by '\n' (col1, col2, col3...);

With your original code there was a '\\n'...

Now i have to understand fully the final code so i can better comprenhend sed tricking in future scenarios... :smiley:

P.S. I have to mantain the text format because i don't want to add future problems, my outfile is destined to feed batch processing in mysql and i can't miss ever a single char... :slight_smile:

Thx