简体   繁体   English

如何从文件的每一行中提取具有特殊字符的特定字符串

[英]how to extract particular string with special character from each line of a file

I have a file with a jungle of strings. 我有一个带有字符串丛林的文件。 Few lines read as follows: 几行内容如下:

 2*H[0, 3, y]*Log[1 - z] - 2*H[2, 3, y]*Log[1 - z] +
 6*H[2, 2, 0, y] + 6*H[2, 2, 2, y] - 48*Log[-Q2] + 12*zeta2*Log[-Q2] +
 (-107 + 12*N^2*(-1 + N^2))*z^2))*(H[0, 1, 0, y] + H[0, 1, 0, z] + ...

I want to find different unique combination of 我想找到不同的独特组合

H[*,*,*] or H[*,*,*,*]

coming in each line. 进入每一行。 Therefore I have to search for string containing special characters too. 因此,我也必须搜索包含特殊字符的字符串。 Is there any way to do in shell? 有什么办法可以做壳吗?

You can use this gnu awk command with custom RS : 您可以将此gnu awk命令与自定义RS

awk -v RS='H\\[[^]]*\\]' 'RT && !seen[RT]++{print RT}' file

H[0, 3, y]
H[2, 3, y]
H[2, 2, 0, y]
H[2, 2, 2, y]
H[0, 1, 0, y]
H[0, 1, 0, z]

Regex H\\\\[[^]]*\\\\] will set each instance of H[...] as record separator. 正则表达式H\\\\[[^]]*\\\\]H[...]每个实例设置为记录分隔符。

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

相关问题 我如何从每一行中提取一些特定的单词? - How do i extract some particular words from each line? 如何从包含特定字符串模式的行中提取第一个参数 - how to extract the first parameter from a line containing a particular string pattern 从带有特定正则表达式的巨大重复文件中提取特定行 - extract a specific line from a huge repetititve file with a particular regex 如何转义从命令行参数存储为变量的特殊字符 - How to escape a special character that is stored as variable from a command line argument 如何将变量中的字符串添加到BASH文件中的每一行 - How to add a string from variables to each line in a file in BASH 如何从文件中的每一行的字符串中选择第n个字符? - How to select the nth char from a string for each line in a file? 我如何使用sed -i命令替换从文件中特定行开始到另一行的字符出现? - How can i use sed -i command to replace the occurance of character starting from particular line in a file till another line? 如何使用linux命令删除文件字符串中的特殊字符 - How to remove a special character in a string in a file using linux commands 使用Bash从日志文件的每一行提取IP地址 - Using Bash to Extract IP Addresses From Each Line of Log File 如何从汇编中的字符串中计算字符和行数? - How to count character and line from a string in assembly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM