Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for file do

Status
Not open for further replies.

Igaduma

Technical User
Nov 23, 2001
322
0
0
BE
Hi List,

Got a file with comma seperated values:

190,C:\test\test

I'm loading them in a script like:

while IFS=',' read NUMBER VALUE
do
echo $VALUE
done

The value is echo's= "C:testtest"
It stripped the \ from the column.

How can I make it not eat \ characters here ?

Thanks!
 
The problem here is that you're processing Windows paths with a Unix script and Unix sees the backslash as an escape character

What will work is
Code:
IFS=','
cat input_file | tr '\\' '/' | while read NUMBER VALUE
do
  echo $VALUE | tr '/' '\\'
done
Note also that I've moved the IFS statement outside the loop

Ceci n'est pas une signature
Columb Healy
 
Hi Columb,

Your solution works on commandline, but not from within this script.
I'm baffled, this should just work.
It's windows os, with cygwin bash, might be a problem there ?
Using tr during read, doesn't do anything, using tr on the commandline works perfect...

Thanks!
Iga
 
Ok, got it, I'm using now cat input_file and not done > input file.

Works nicely, thanks!
 
try

Code:
...
done < input_file

instead.

The UUOC police is still patrolling this forum!


HTH,

p5wizard
 
It's windows os, with cygwin bash, might be a problem there ?
Whilst I'm all in favour of using Cygwin you have to take into consideration the differences between Unix and Windows. The two biggies are
[ol]
[li]path seperators. Unix uses the forward slash, windows the backslash. This may seem trivial except that Unix, along with its cousin the 'C' programming language, uses the backslash to change the meaning of certain characters. \t, for example is the tab character[/li]
[li]Line terminators in text files. Unix uses Line Feed (0x0A) and Windows uses a combination of Carriage Return and Line Feed (0x0D and 0x0A). This means that a text document prepared on Windows will have Ctrl-M at the end of each line when viewed under Unix and a text document created under Unix may not look right (it depends on what's used for viewing) when viewed under Windows.[/li]
[/ol]
When putting Windows type paths into Cygwin shell scripts the answer is to 'escape' the backslashes. There are two ways to do this.
[ol][li]Quote all relevant strings in single quotes. i.e. 'C:\windows\path' - the single quote implies that ALL characters are to be interpreted as typed - i.e. no variable expansion etc.[/li]
[li]escape the backslash by using the escape character, the backslash - i.e. C:\\windows\\path[/li][/ol]
In the script that were discussing the backslashes need to be converted to forward slashes on input to prevent the shell misinterpreting them. Once the processing has been completed we can change them back again.

Ceci n'est pas une signature
Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top