简体   繁体   English

如何从文件中的匹配行中提取子字符串?

[英]How can I extract a substring from a matching line in a file?

I use: 我用:

cat display.txt | sed -e 's/...\(.*\).../\1/g' 

to get string between("..."). 得到之间的字符串(“ ...”)。 But, it gives me this result which is full content of the file. 但是,它给了我这个结果,它是文件的全部内容。 How can I extract just "hhgggeeee" from this? 如何从中提取"hhgggeeee"

= New Index: 98:4F:EE:06:0B:03 (BR/EDR,UART,hci0)               
[hci0] 0.595705 > ACL Data RX: Handle 12 flags 0x02 dlen 19                    
[hci0] 11.420307 Channel: 64 len 15 [PSM 0 mode 0] {chan 0}     
1b ef 17 68 68 67 67 67 65 65 65 65 0d 0a 8f 
...hhgggeeee... > HCI Event: Mode Change (0x14) plen 6 [hci0] 11.923905
Status: Success (0x00) Handle: 12 Mode: Active (0x00)
Interval: 0.000 msec (0x0000)

You can use a perl one liner for this: 您可以为此使用perl一种衬垫:

perl -lne 'print $1 if /^\.\.\.(.*)\.\.\./' display.txt

For your file, it outputs: 对于您的文件,它输出:

hhgggeeee

You can use a sed command. 您可以使用sed命令。 Try the below command. 请尝试以下命令。

cat dis.txt|sed -n 's/\.\.\.\([^\.]\+\)\.\.\..*/\1/gp' 

Or use can use the grep command also. 或者使用也可以使用grep命令。

cat dis.txt|grep -o '\.\.\.\([^\.]\+\)\.\.\.'

The both are gives the same output. 两者都给出相同的输出。 The output is 输出是

hhgggeeee
$ sed -n 's/.*\.\{3\}\(.*\)\.\{3\}.*/\1/p' file
hhgggeeee

暂无
暂无

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

相关问题 如何在Linux的一行中提取子字符串 - How can I extract a substring in a line on linux 如何在与命令行中的另一个模式匹配的目录中“查找”匹配模式的文件? - How can I `find` files matching pattern within a directory matching another pattern from the command line? 如何从格式不可预测的文件中提取单行/多行正则表达式匹配项并将每个项放在一行中放入输出文件? - How to extract single-/multiline regex-matching items from an unpredictably formatted file and put each one in a single line into output file? 我如何仅从TCPDUMP文件中提取目标端口 - How can i extract only the destination ports from a TCPDUMP file 如何从匹配行之后删除文件中的所有行? - How do I delete all lines in a file starting from after a matching line? 如何从Linux命令行上的变量中提取不带扩展名的文件名(并添加计数器)? - How can I extract the filename without extension (and add a counter) from a variable on the Linux command line? 从ls中提取子字符串 - Extract substring from ls 如何通过匹配上一行将sed替换为文本 - How can i replace text with sed by matching previous line 如何从一行中提取多个非贪婪的正则表达式匹配部分? - How to extract multiple non-greedy regex-matching parts from a line? 如何使用sed或awk在开始子字符串和结束子字符串的基础上提取行部分 - how to extract line portion on the basis of start substring and end substring using sed or awk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM