2013-07-09 77 views
0

我想知道爲什麼myfile.sed中的搜索條件不會過濾。我只想過濾第一列中的任何字母。這就是我想過濾行開始後的任何字母,接着" chracter並用"字符和,字符結尾,但我在後面的列中獲取數據:爲什麼我的sed搜索在字段中找不到alpha?

我使用

sed -f myfile.sed in-file

{ 
     /^"[0-9]+[^0-9]+[0-9]*",/p 
     /^"[0-9]+",""/p 
} 

文件內包含的數據的前幾場的myfile.sed外觀的內容我想過濾

"1866TL","1866TL","ME","SUBA","GRY","B","25","40","LAKEHILL AVE"

我感興趣的打印上面的行,因爲一個非數字 - TL - 出現在第一列。

爲了使這項工作,我試圖用[^0-9A-Za-z]代替"字符。

我已經嘗試過只搜索幾個數字後跟一個字符,但無法完成這項工作。

我在做什麼錯?

糾myfile.sed看起來像這樣從@choroba的回答後:

{ 
     /^"[0-9]\+[^0-9]\+[0-9]*",/p 
     /^"[0-9]\+",""/p 
} 

我得到其他領域我也沒打算讓。 如何在逗號後切斷搜索?

"940915","L33677","MA","CHEV","RED","R","25","54","GROVE ST", 
"1866TL","1866TL","ME","SUBA","GRY","B","25","40","LAKEHILL AVE", 
"966011","1942758","NH","AUDI","BLU","","25","13","MT. VERNON ST.", 
+0

的窠臼,你不是在尋找(數字或其他非字母字符),總是試圖根據你寫你的文本處理腳本正在尋找(字母字符),最終結果幾乎總是更清晰,更簡單,更容易維護。 –

回答

2

加上必須反斜線有特殊含義:

/^"[0-9]\+[^0-9]\+[0-9]*",/p 

如果你不想否定的字符類匹配逗號和雙引號,它們包括:

/^"[0-9]\+[^0-9",]\+[0-9]*",/p 
+0

謝謝。我得到太多了,但至少我想要的線。我編輯了OP並重新提出了這個問題。 – octopusgrabbus

+0

@octopusgrabbus:查看更新。 – choroba

+0

我更新了OP,使其更清晰。基本上,我只想在第一個逗號(,)字符之前有一個字母時纔打印。嘗試你的例子,它仍然獲得所有線路。 – octopusgrabbus

0

sed是用於簡單替換一條線的優秀工具,對於其他任何只使用awk的工具:

$ cat file 
"940915","L33677","MA","CHEV","RED","R","25","54","GROVE ST", 
"1866TL","1866TL","ME","SUBA","GRY","B","25","40","LAKEHILL AVE", 
"966011","1942758","NH","AUDI","BLU","","25","13","MT. VERNON ST.", 

$ awk -F, '$1 ~ /[[:alpha:]]/' file 
"1866TL","1866TL","ME","SUBA","GRY","B","25","40","LAKEHILL AVE", 

不能簡單得多,對吧?這裏

FWIW是sed的等效:

$ sed -n '/^[^,]*[[:alpha:]]/p' file 
"1866TL","1866TL","ME","SUBA","GRY","B","25","40","LAKEHILL AVE", 
相關問題