在2个标记之间提取文本

时间:2018-03-20 14:16:36

标签: bash awk sed grep

输入

random adsf 845 asdsf.$Ecdda-string0.rand me39 84abd    
dee rand a[s% 845 a1sEdsf.$cdNda-string1.rand me39 84abd

输出 - 保持da-.之间的所有内容

string0    
string1

到目前为止我尝试了什么

sed -e 's/.*da-\(.*\)./\1/' file 
grep -o -P '(?<=da-).*(?=.)' file
grep -o -P '(?<=da-).*(?=\.)' file

4 个答案:

答案 0 :(得分:0)

关注awk可能对您有帮助。

awk 'match($0,/da-[^.]*/){print substr($0,RSTART+3,RLENGTH-3)}'  Input_file

说明:

awk '
match($0,/da-[^.]*/){                 ##Using match out of the box keyword for matching regex da- to till first occurence of DOT.
  print substr($0,RSTART+3,RLENGTH-3)}##Printing the substring whose start values of RSTART+3 till RLENGTH-3 , where RSTART and RLENGTH are the variables of out of the box which will be set once a regex is having TRUE value.
'  Input_file                         ##mentioning Input_file here.

答案 1 :(得分:0)

你的sed很接近。在匹配结束时需要\..*

$ cat test.txt
random adsf 845 asdsf.$Ecdda-string0.rand me39 84abd
dee rand a[s% 845 a1sEdsf.$cdNda-string1.rand me39 84abd

$ sed 's#.*da-\([^.]*\)\..*#\1#g' test.txt
string0
string1

答案 2 :(得分:0)

grep看后面..

$ grep -oP '(?<=da-)[^.]+' file

string0
string1

答案 3 :(得分:0)

$ awk -F'[-.]' '{print $3}' file

string0
string1

使用提供的FS打印第三个字段。

相关问题