How to extract patterns form a text files in shell

2020-04-17 04:33发布

I have a text file that contains:

toto.titi.any=val1
toto.tata.any=val2
toto.tete.any=val2

How to extract titi , tata and tete from this file.

Should be some sthing like that

$ cat myfile.txt | sed '......' 

and the output should be

titi
tata
tete

5条回答
老娘就宠你
2楼-- · 2020-04-17 05:09

Do you really need sed? You could use cut:

cut -d. -f2 filename
查看更多
够拽才男人
3楼-- · 2020-04-17 05:13

awk/cut would be better choice for this problem.

here is the sed line and grep option:

sed -r 's/.*\.([^.]*)\..*/\1/'
grep -Po '\.\K[^.]*(?=\.)' 
查看更多
三岁会撩人
4楼-- · 2020-04-17 05:21
sed -n 's/^[^.]*.\([^.]*\)..*/\1/p' myfile.txt

display second value between dot from line having at least 2 dot inside

查看更多
The star\"
5楼-- · 2020-04-17 05:24

awk and cut are best for this

you can also read the file line by line, and print out the portion needed.

$ IFS=.
$ while read first interested others ; do echo $interested; done < file
titi
tata
tete
查看更多
We Are One
6楼-- · 2020-04-17 05:28

With awk you can do:

awk -F. '{print $2}' file
查看更多
登录 后发表回答