如何从unix或solaris中的文件中删除包含字符串的行

发布于 2024-10-21 19:24:45 字数 383 浏览 9 评论 0原文

我的文件如下,

test1    
test2    
test3    
test4    
test5

我想删除包含 test2 的行,因此我使用了如下命令,但不是仅删除“test2”行,而是删除了整个文件,并且 fize-size 变为零。

bash# cat /aaa/bbb/ccc/myfile | sed -e '/test2/ d'  > /aaa/bbb/ccc/myfile     
bash# ls -l total 0    
-rw-r--r--   1 root     root           0  3月 11 17:41 myfile

有人能建议一下,命令有什么问题吗?

I have myfile as follows

test1    
test2    
test3    
test4    
test5

I want to delete line containing test2, so I used command as follows, but instead of removing only 'test2' line its removed whole file and fize-size became zero.

bash# cat /aaa/bbb/ccc/myfile | sed -e '/test2/ d'  > /aaa/bbb/ccc/myfile     
bash# ls -l total 0    
-rw-r--r--   1 root     root           0  3月 11 17:41 myfile

can anybody suggest , whats wrong in command?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

晨敛清荷 2024-10-28 19:24:45

除非您有 GNU sed(带有“-i”选项),并且您使用的是 Solaris,所以您可能没有,否则您别无选择,只能写入临时文件:

sed -e '.....' infile > infile.tmp
mv infile.tmp infile

更新:使用 ed

printf "%s\n" 'g/test2/d' w q | ed infile

unless you have GNU sed (with the "-i" option), and you're on Solaris so you probably don't, you have no choice but to write to temp file:

sed -e '.....' infile > infile.tmp
mv infile.tmp infile

update: edit the file in-place with ed

printf "%s\n" 'g/test2/d' w q | ed infile
許願樹丅啲祈禱 2024-10-28 19:24:45

我不知道 perl 是否是 Solaris 上的标准。如果是这种情况,您可以使用:

perl -ni -e 'if(!/test2/){print;}' myfile

I don't know if perl is standard on Solaris. If this is the case, you can use:

perl -ni -e 'if(!/test2/){print;}' myfile
秋意浓 2024-10-28 19:24:45

不幸的是,输出重定向立即清空输出文件。因此,您必须使用不同的文件作为输出文件,例如:

sed '/test2/d' /aaa/bbb/ccc/myfile > /aaa/bbb/ccc/myfile2

或者您可以执行类似的操作:

sed '/test2/d' /aaa/bbb/ccc/myfile | tee /aaa/bbb/ccc/myfile

但由于缓冲,这不是很可靠。如果输出程序 (tee) 在 sed 完成读取之前写入文件,这将导致数据损坏。
也许您还可以尝试使用程序 buffermbuffer 作为 tee 的替代品,您可以在其中指定缓冲区大小。但我在快速试验中并没有取得可靠的成功。

Unfortunately the output redirection immediately empties the output file. Therefore you have to use a different file as output file, eg:

sed '/test2/d' /aaa/bbb/ccc/myfile > /aaa/bbb/ccc/myfile2

Or you could do e.g. something like that:

sed '/test2/d' /aaa/bbb/ccc/myfile | tee /aaa/bbb/ccc/myfile

But due to buffering this is not very reliable. If the output program (tee) writes to the file before sed has finished reading, this will lead to corrupt data.
Maybe you could also experiment with the programs buffer or mbuffer as substitute for tee there you can specify buffer sizes. But I didn't have reliable success on a fast trial.

醉态萌生 2024-10-28 19:24:45

尝试

grep -v test2 test.txt  > temp
mv temp test.txt 

Try

grep -v test2 test.txt  > temp
mv temp test.txt 
很酷又爱笑 2024-10-28 19:24:45

您可以简单地使用旧版:

echo "/test2
d
w
q" | ed /aaa/bbb/ccc/myfile

You can simply use the good old ed:

echo "/test2
d
w
q" | ed /aaa/bbb/ccc/myfile
始终不够 2024-10-28 19:24:45

您还可以使用 grep

> grep -v test2 test.txt 
test1    
test3    
test4    
test5

请注意,与 sed 一样,您不应该覆盖正在读取的文件,因此您可以像这样调用它:

grep -v test2 test.txt >测试.输出

You can also use grep:

> grep -v test2 test.txt 
test1    
test3    
test4    
test5

Be aware that as with sed, you shouldn't overwrite the file you are reading from, so you can invoke it like this:

grep -v test2 test.txt > test.out

毁梦 2024-10-28 19:24:45

Solaris,假设您使用的不是过时的版本,应该至少已经带有 bash 3。所以只使用 bash

while read -r line
do
  case "$line" in
     *"test2"*) continue;;
     *) echo "$line";;   
  esac
done < file > temp && mv temp file

Solaris, assuming you are not on an archaic version, should already come with at least bash 3. So using just bash

while read -r line
do
  case "$line" in
     *"test2"*) continue;;
     *) echo "$line";;   
  esac
done < file > temp && mv temp file
誰認得朕 2024-10-28 19:24:45

下面的代码对我来说工作得很好......

touch myfile2
cp /aaa/bbb/ccc/myfile myfile2
sed '/test2/d' myfile2 > /aaa/bbb/ccc/myfile

the following code is workign fine for me...

touch myfile2
cp /aaa/bbb/ccc/myfile myfile2
sed '/test2/d' myfile2 > /aaa/bbb/ccc/myfile
不喜欢何必死缠烂打 2024-10-28 19:24:45

您可以在 AWK 中将整个文件读入数组:

awk '!/test2/ {a[++c] = $0} END {for (i=1; i<=c; i++) print a[i] > FILENAME}' /aaa/bbb/ccc/myfile

由于重定向不是由 shell 完成的,因此不会执行早期截断。

You can read the whole file into an array in AWK:

awk '!/test2/ {a[++c] = $0} END {for (i=1; i<=c; i++) print a[i] > FILENAME}' /aaa/bbb/ccc/myfile

Since redirection is not done by the shell, early truncation is not performed.

不奢求什么 2024-10-28 19:24:45
$touch myfile

$printf "test1\ntest2\ntest3\ntest4\ntest5\n">myfile

$cat myfile 

test1
test2
test3
test4
test5

$sed '2d' myfile

test1
test3
test4
test5

所以使用:

$sed '2d' myfile
$touch myfile

$printf "test1\ntest2\ntest3\ntest4\ntest5\n">myfile

$cat myfile 

test1
test2
test3
test4
test5

$sed '2d' myfile

test1
test3
test4
test5

So use:

$sed '2d' myfile
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文