2014-10-09 20 views

回答

0

也許這樣的事情:

#!/bin/bash 

exec 3>output 

while read -r line; do 
    nwords=`echo $line | wc -w`; 
    if [ ! "$nwords" -gt 1 ]; then 
     echo "$line" >&3; 
    fi 

done < input 

exec 3>&- 

然後,你將有那些沒有餘萬字的輸出文件中的所有行,你可以刪除比輸入文件和MV輸出輸入。

0

我用這個,使用SED:我的txt文件是file.txt的:

one 
one two 
one two tree for 
one two 

sed -i.bck '/\(.\+\)\(\s\+\)\(.*\)/d' file.txt 
cat file.txt 
one 
+0

我不知道它是否應該在這種情況下不起作用: one two \ n (空格)兩個 當開頭有空格時 – RaFD 2014-10-09 09:39:05

+0

@RFF它在開頭處用空格 – c4f4t0r 2014-10-09 10:12:09

0

如果你的字是由「空間」字符分隔,你可以做到這一點的只有一條線路,與sed。讓「./myfile.txt」是你的文件名,然後:

#!/bin/bash 

sed -i "/[^ ][ ]/d" ./myfile.txt 

正則表達式替換由非空格字符開頭的所有空格字符,是這樣的話,當你有兩個詞。