j******n 发帖数: 271 | 1 Given a text file, replace all "cat" with "dog" on all lines where there
is no "mouse". Use only traditional unix tools that come with system
installation, including sed, awk, but excluding perl, ruby, python. | c****w 发帖数: 79 | 2 nobody cares?
here is an ugly one,
assume the text file is a, then
$ nl -w10 -n rz a | grep mouse > a0
$ nl -w10 -n rz a | grep -v mouse > a1
$ cat a1 | sed 's/cat/dog/g' > a11
$ cat a11 a0 | sort | sed 's/[0-9]*\t//' > b
then file b has the result
【在 j******n 的大作中提到】 : Given a text file, replace all "cat" with "dog" on all lines where there : is no "mouse". Use only traditional unix tools that come with system : installation, including sed, awk, but excluding perl, ruby, python.
| c****w 发帖数: 79 | 3 there were errors, so here's a better ugly one, hehe
$ nl -n rz a | grep mouse > a0
$ nl -n rz a | grep -v mouse | sed 's/cat/dog/g'> a1
$ cat a1 a0 | sort | sed 's/^[0-9]*\t//' > b
【在 c****w 的大作中提到】 : nobody cares? : here is an ugly one, : assume the text file is a, then : $ nl -w10 -n rz a | grep mouse > a0 : $ nl -w10 -n rz a | grep -v mouse > a1 : $ cat a1 | sed 's/cat/dog/g' > a11 : $ cat a11 a0 | sort | sed 's/[0-9]*\t//' > b : then file b has the result
| k*******d 发帖数: 1340 | 4 原来现在街上编程的要求这么高了啊,要求这么高的Unix,看来只有CS的能行了 | j******n 发帖数: 271 | 5 That is a good one. Actually there is a way without using temporary files,
even without pipes.
【在 c****w 的大作中提到】 : there were errors, so here's a better ugly one, hehe : $ nl -n rz a | grep mouse > a0 : $ nl -n rz a | grep -v mouse | sed 's/cat/dog/g'> a1 : $ cat a1 a0 | sort | sed 's/^[0-9]*\t//' > b
| c****w 发帖数: 79 | 6 Thanks to the little pressure
sed '/mouse/ !{ s/cat/dog/g }' a
【在 j******n 的大作中提到】 : That is a good one. Actually there is a way without using temporary files, : even without pipes.
| j******n 发帖数: 271 | 7 Wow cool. More elegant than the one I cooked up. Thanks! | J*****n 发帖数: 4859 | 8
能解释一下其中的语法么?
【在 c****w 的大作中提到】 : Thanks to the little pressure : sed '/mouse/ !{ s/cat/dog/g }' a
|
|