escape space characters in loop from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting escape space characters in loop from file
# 1  
Old 12-04-2009
escape space characters in loop from file

Hi Everyone!

I want to build sql inserts from a list of countries/regions saved in a file. The list looks like this:

Code:
United Kingdom
Czech Republic
...

The script I run is:

Code:
while read i; 

do 
   var=`expr $var + 1`; 
   echo "INSERT INTO calltypes VALUES($var, '$i','$i');" >> inserts.sql; 

done < countries.txt;

The problem is that this is the result I get:

Code:
INSERT INTO calltypes VALUES(2573, 'United','United');
INSERT INTO calltypes VALUES(2574, 'Kingdom','Kingdom');
INSERT INTO calltypes VALUES(2571, 'Czech','Czech');
INSERT INTO calltypes VALUES(2572, 'Republic','Republic');

How can I escape the space characters from the file?

I've seen the solutions in this forum, but they don't work for me.
# 2  
Old 12-04-2009
Hi.

What should the SQL look like?

I get quite a different result to you:

Code:
$ cat file1
United Kingdom
Czech Republic

$ cat Test
var=1
while read i;
do
   echo "INSERT INTO calltypes VALUES($var, '$i','$i');"
   let var=$var+1
done < file1

$ ./Test
INSERT INTO calltypes VALUES(1, 'United Kingdom','United Kingdom');
INSERT INTO calltypes VALUES(2, 'Czech Republic','Czech Republic');

# 3  
Old 12-04-2009
My output looks like scottn's. i should contain the whole line, not only what is separated by spaces.

---------- Post updated at 03:57 PM ---------- Previous update was at 03:53 PM ----------

What shell are you using?

And could you try out a simple
Code:
while read LINE; do
    echo $LINE
done < countries.txt


Last edited by zaxxon; 12-04-2009 at 11:01 AM.. Reason: missing ending code tag
# 4  
Old 12-04-2009
Thanks!

It seems that I if I echo it to the screen it works, but when I save it in a file (>> inserts), it splits the lines by spaces.

Does this happen to you too?
# 5  
Old 12-04-2009
Hm no. Did you change IFS maybe? Does this also happen in a fresh shell with the mini script I posted before? Ie. add >> to somefile in the line with echo.
# 6  
Old 12-04-2009
Thanks again!

Sorry, silly me! The first time I run the script, I did it with a for loop (giving me the bad result) and after I changed the script, I forgot to remove the inserts.sql file, so the correct solution was being appended to the file and I never saw it.

Cheers.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Escape characters in a variable

Debian 9 64x - LXDE How can i disable escape sequences in a variable? #!/bin/bash #mainscript . "./links.bash" echo "$red_start This text should be red $color_end"#!/bin/bash #links.bash #colors red_start="\eOutput that i get: \e Output expected: This text should be... (5 Replies)
Discussion started by: int3g3r
5 Replies

2. Shell Programming and Scripting

How to escape all special characters?

I have an application which I am integrating with that accepts the password via a CLI. I am running in to issues with passwords that contain special characters. I tried to escape them all, but I ran in to an issue where I cannot escape the characters ' ] My attempt is as follows: $... (2 Replies)
Discussion started by: AMG1978
2 Replies

3. Shell Programming and Scripting

Escape characters

i am executing script from A server which will execute the script in B server , as below. ssh A 'ssh B echo 'select * from testing where name ='test'' i am getting the below output. select * from testing where name=test but i need the output where clause with quotes , tried with... (3 Replies)
Discussion started by: expert
3 Replies

4. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

5. Shell Programming and Scripting

Escape space in for loop

I have a file with the following contents # more hello.txt man hello man whereru The shell script i have tries to echo the contents of the file hello.txt for i in `cat hello.txt` do echo $i done but the output i am getting is taking the space as a new line.. #... (3 Replies)
Discussion started by: Tuxidow
3 Replies

6. UNIX for Dummies Questions & Answers

Escape Characters on various shells

Hi, I want to know if escape charaters work on all the popular UNIX shells. More specifically I want to know if echo "\c" will work on most of the UNIX shells and are there any specific shells on which \c won't work. Please help. Thanks, Vineet (2 Replies)
Discussion started by: vineetd
2 Replies

7. Shell Programming and Scripting

Searching for escape characters

Hi all I have been trying to write a script to look for a set of specific escape characters in a file. On viewing the file via vi it shows this : ^ I understand this means no end of line. I have tried a vary of grep parameters such as grep ^\^. filename grep --binary-file=binary without... (8 Replies)
Discussion started by: timcs
8 Replies

8. Shell Programming and Scripting

escape characters..

hey i want to know the unix commands to replace all the character escape sequences with their "C" values in a string... thanks in advance..! Regards, Sharanya (9 Replies)
Discussion started by: sharsin2001
9 Replies

9. Shell Programming and Scripting

number of escape characters?

Hi, I am trying to execute the following command from a batch script, but no matter how many escape characters I put in it doesn't execute properly. It works fine from the command line with quotes around the -exec part. #!/bin/sh /usr/local/bin/sudo /usr/atria/bin/cleartool setview -exec... (0 Replies)
Discussion started by: Sebarry
0 Replies

10. UNIX for Advanced & Expert Users

lp FormFeed Escape characters

I'm trying to modify the /usr/lib/lp/model/netstandard file to generate a header for all the print jobs that are sent, but there is no formfeed defined so the the job prints right after the header with no page break. What is the sequence I need in order to generate a formfeed? Or, do you have... (4 Replies)
Discussion started by: jgordon
4 Replies
Login or Register to Ask a Question