简体   繁体   中英

Get string between characters in Linux

I have text file, which has data like this:

asd.www.aaa.com
abc.abc.co 
look at me
asd.www.bbb.com
bzc.bzc.co
asd.www.ddd.com
hello world
www.eee.com
xx.yy.z

I want strings which is surrounded by "asd.www.[i want this string].com" .

So my output will be like:

aaa
bbb
ddd

try:

grep -Po '^asd\.www\.\K[^.]*(?=\.com)' file

if asd could be in middle of the string, remove the first ^ .

there could be other corner cases, like the greedy matching etc. it depends on your source input.

I suggested cut originally but I misread your question. So I'm going to post an alternative with awk instead. You are looking for the third column of your text input where there are a total of four columns.

less file.txt | awk -F '.' '{ if ($4 != "") print $3 }'

It splits your string on . and only prints out column $3 if column $4 is blank. This should yield the following given your example text:

aaa
bbb
ddd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM