2017-08-14 54 views
0

我希望能夠提取單詞列表中的特定長度的單詞,並將其提取到另一個列表中。我怎樣才能讓grep在bash中提取字符串的長度?

如果我運行:

grep -oP '\b(\w{7})\b' infile >> outfile 

eveything運行正常和詞得到提取,但是當我在bash腳本運行它,沒有東西輸出。如果我把雙引號,因爲它是造成被讀取的長度數,我仍然得到語法error.so劇本是這樣的:

read -p "what is in:" in 
read -p "what is out:" out 
read -p "what is char num:" char 

grep -oP '\b(\w{$char})\b' $in >> $out 

我在想什麼?

+0

請出示所需的輸出。 –

回答

0

你需要有雙引號,這樣的bash字符串中擴展變量

嘗試

grep -oP "\b(\w{$char})\b" 

看到它在行動:

輸入文件

$ cat file1 
dockerkill container 
docker anothercontainer 
dockerput contain 

腳本:

$ cat script.sh 
char="6" 
grep -oP "\b(\w{$char})\b" file1 

輸出

$ ./script.sh 
docker