Question

I have a file containing:

aaa.bbbb0.xxxx0=toto
aaa.bbbb0.xxxx1=toto
aaa.bbbb0.xxxx2=titi
eee.ffff.zzzz=kkkk

I want to extract the lines containing the regular expression"aaa\.bbbb0\.xxxx[0-6]\+=toto" and then extract the 3rd element in the path. So the output should looks in this way

xxxx
yyyy

I can get this output using grep and then cut in this way:

cat myfile.txt | grep "aaa\.bbbb0\.xxxx[0-6]\+=toto" | cut -d'=' -f1 | cut -d'.' -f3

How I can change the above grep and cut by only one sed command or awk command?

Was it helpful?

Solution

answer to previous question:

awk -F'[.=]' '$4 ~ /^[[:digit:]]+$/ {print $3}' file

answer to current question:

$ awk -F'[.=]' '/aaa\.bbbb0\.xxxx[0-6]+=toto/ {print $3}' file
xxxx0
xxxx1

OTHER TIPS

awk -F[.=] '/=mmmm/{print $3}' input.txt

To match the data of your first edit:

awk -F[=] '$2 ~ /^[0-9]+(\.[0-9]+)?$/{split($1,a,".");print a[3]}' input.txt

This will match floating point numbers too

And for second edit

awk -F= '/aaa\.bbbb0\.xxxx[0-6]+=toto/{split($1,a,".");print a[3]}' input.txt

Using grep -P option to enable PCRE you can get the output by using Positive Lookahead and Lookbehind.

$ cat file
aaa.bbbb.xxxx=4521
ccc.dddd.yyyy=7842
eee.ffff.zzzz=kkkk

$ grep -oP '(?<=\.)(\w+)(?==\d+$)' file
xxxx
yyyy 

Another awk variant:

$ awk -F. '{split($3,a,/=/); if (a[2]~/^[[:digit:]]+$/) print a[1]}' file
xxxx
yyyy

Based on updated question:

$ awk -F. '/aaa\.bbbb0\.xxxx[0-6]+=toto/{split($3,a,/=/); print a[1]}' file
xxxx0
xxxx1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top