views:

101

answers:

4

So I have sed running with the following argument fine if I copy and paste this into an open shell:

cat test.txt | sed '/[,0-9]\{0,\}[0-9]\{1,\}[acd][0-9]\{1,\}[,0-9]\{0,\}/{N
s/[,0-9]\{0,\}[0-9]\{1,\}[acd][0-9]\{1,\}[,0-9]\{0,\}\n\-\-\-//}'

The problem is that when I try to move this into a korn shell script, the korn shell throws errors because of what I think is that new line character. Can anyone give me a hand with this? FYI: the regular expression is supposed to be a multiple line replacement. Thank you!

A: 

can you try to put your regex in a file and call sed with the option -f ?

cat test.txt | sed -f file.sed
Pierre
No, that didn't work. I get the same errors. Thanks for the suggestion though!
Shane
A: 

Can you try to replace the new line character with `echo -e \\r`

iniju
A: 

This: \{0,\} can be replaced by this: *

This: \{1,\} can be replaced by this: \+

It's not necessary to escape hyphens.

The newline can be replaced by -e (or by a semicolon)

The cat can be replaced by using the filename as an argument to sed

The result:

sed -e '/[,0-9]*[0-9]\+[acd][0-9]\+[,0-9]*/{N' -e 's/[,0-9]*[0-9]\+[acd][0-9]\+[,0-9]*\n---//}' test.txt

or

sed '/[,0-9]*[0-9]\+[acd][0-9]\+[,0-9]*/{N;s/[,0-9]*[0-9]\+[acd][0-9]\+[,0-9]*\n---//}' test.txt

(untested)

Dennis Williamson
Thank you - that worked perfectly! What is the significance of the semicolon there? is this part of sed?
Shane
@Shane: The semicolon is a command separator that's used in `sed` (and several other languages). `N` is one command and `s///` is another. You could do something like this trivial example: `echo 'ab' | sed 's/./X/; s/./a/2; s/X/b/'` to chain multiple commands together (it swaps the "a" and "b", by the way - but this is probably better: `sed 's/\(.\)\(.\)/\2\1/'`).
Dennis Williamson
(.)(.)I like that one better too!
Shane
That should have been `sed -r 's/(.)(.)/\2\1/'` or `sed 's/\\(.\\)\\(.\\)/\2\1/'`
Dennis Williamson
A: 

The Korn Shell - unlike the C Shell - has no problem with newlines in strings. The newline is very unlikely to be your problem, therefore. The same comments apply to Bourne and POSIX shells, and to Bash. I've copied your example and run it on Linux under both Bash and Korn shell without any problem.

If you use C Shell for your work, are you sure you're running 'ksh ./script' and not './script'?

Otherwise, there is some other problem - an unbalanced quote somewhere, perhaps.

Check out the '-v' and '-n' options as well as the '-x' option to the Korn Shell. That may tell you more about where the problem is.

Jonathan Leffler