0

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?

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148

3 Answers3

2

Use the -r option of read.

From the ksh man page:

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

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
0

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?

Christopher Karel
  • 6,442
  • 1
  • 26
  • 34
0

Use -r on the read:

cat test.input | while read -r 'line'
do
echo "$line"
done 
gm3dmo
  • 9,632
  • 1
  • 40
  • 35
  • 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
  • Removed the -e as per Dennis comment. – gm3dmo Dec 28 '09 at 18:49