简体   繁体   English

如何从shell bash中的文本文件中提取模式

[英]How to extract patterns form a text files in shell bash

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. 如何从这个文件中提取tititatatete

Should be some sthing like that 应该是这样的一些sthing

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

and the output should be 输出应该是

titi
tata
tete

Do you really need sed ? 你真的需要sed吗? You could use cut : 你可以用cut

cut -d. -f2 filename

使用awk你可以做到:

awk -F. '{print $2}' file

awk/cut would be better choice for this problem. awk / cut是这个问题的更好选择。

here is the sed line and grep option: 这是sed行和grep选项:

sed -r 's/.*\.([^.]*)\..*/\1/'
grep -Po '\.\K[^.]*(?=\.)' 

awk and cut are best for this awkcut是最好的

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
sed -n 's/^[^.]*.\([^.]*\)..*/\1/p' myfile.txt

显示内部至少有2个点的线之间的第二个值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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