我怎样才能让grep在bash中提取字符串的长度?
问题描述:
我希望能够提取单词列表中的特定长度的单词,并将其提取到另一个列表中。我怎样才能让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
我在想什么?
答
你需要有双引号,这样的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
请出示所需的输出。 –