2014-01-06 17 views
0
i've got this file pattern 

#### data ### 
#### roles ### 
11 test test22 
12 hash hash2 
13 hash3 hash77 
14 hash5 ahss 
14 hash5 ahss 
14 hash2 adfasf 
14 hash7 asfdf 

#### data ### 
#### addding ### 
11 test test22 
12 hash hash2 
13 hash3 hash77 
14 hash5 ahss 
14 hash5 ahss 
14 hash2 adfasf 
14 hash7 asfdf 

我只想複製線開始wuth 14並排除所有其他線路輸出文件,輸出文件應該是相同的結構,像慶典搜索文件的每一行字符串排除一些線和CPY休息用相同的結構

#### data ### 
#### roles ### 

14 hash5 ahss 
14 hash5 ahss 
14 hash2 adfasf 
14 hash7 asfdf 

#### data ### 
#### addding ### 

14 hash5 ahss 
14 hash5 ahss 
14 hash2 adfasf 
14 hash7 asfdf 

我用grep的,但它不會複製該文件 ####數據### ####角色###的頭

anytips用while循環或做如果

+0

你真的決心要找到一些可以用bash循環代替grep來完成的東西,不是嗎? – Barmar

+0

使用'while'循環或'if'做主要提示是**不要**。 –

回答

1
grep -E -e '^(#|$|14)' data.file 

這複製了以#開頭的行,空行和以14開頭的行和一個空格。

grep -E相當於egrep

egrep -e '^(#|$|14)' data.file 

你也可以用sedawk(或Perl或Python)做到這一點,但這些超過必要的複雜。

1

您可以使用grep -E,因爲它允許匹配的替代品:

grep -E '^#|^14 |^$' infile > outfile 
0

在bash腳本,在data.txt中保存數據並調用腳本

#!/bin/bash 
while read line 
do 
    ret=0; 
    echo $line | grep -q "^[0-9]\+\s\+[a-zA-Z0-9]\+\s\+.[a-zA-Z0-9]\+" 
    ret=$? 
    if [ $ret -eq 0 ]; then 
    echo $line | grep "^14 " 
    else 
    echo $line; 
    fi; 
done < data.txt 
相關問題