Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have written a small script as follows

#!/usr/bin/ksh
cat test |while read line1
do
echo "$line1"
done

The input file "test" has the following lines

Kensington K64391US C\i70 Wireless
Desktop Nintendo Wii Wireless Nunchuck
\M470 DeLonghi HHP1500 Mica Panel
Vi\20 Radiator Heater

But "\" is missing in the output which is as follows

Kensington K64391US Ci70 Wireless
Desktop Nintendo Wii Wireless Nunchuck
M470 DeLonghi HHP1500 Mica Panel Vi20
Radiator Heater

How can I get the "\" to come in the output?

share|improve this question
    
It's not necessary to use cat, just redirect the file into the done like this: while ... done < test –  Dennis Williamson Dec 28 '09 at 18:15

3 Answers 3

Use the -r option of read.

From the ksh man page:

In raw mode, -r, the \ character is not treated specially.

share|improve this answer

Well, a quick and easy answer is to replace all the single slashes in your input file with double slashes. This will result in a single slash in the output. (eg: in vi, use %s/\\/\\\\/g

Reasoning: The backslash is an escape character. It's used to indicate "the next character should be treated literally", to avoid things like asterisks being taken as anything but an asterisk. So putting two of them in a row results in the first one indicating that the second should be used as just a slash.

Is this viable for you, or do you need to make changes to the script itself, without touching the data?

share|improve this answer

Use -r on the read:

cat test.input | while read -r 'line'
do
echo "$line"
done 
share|improve this answer
    
By using the -e option of echo, you could be throwing away the \ that you saved by using read -r. Say, for example, your model number was "abc\033". Just use echo without the -e. Yay! We can edit comments! –  Dennis Williamson Dec 28 '09 at 18:28
    
-r option works. Thanks a lot –  Ashu Dec 28 '09 at 18:31
    
Removed the -e as per Dennis comment. –  davey Dec 28 '09 at 18:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.