我有一个文本文件,其中写入了以下模式:
TIME[32.468ms] -(3)-............."TEXT I WANT TO KEEP"
我想丢弃包含的每行的第一部分
TIME[32.468ms] -(3)-.............
为了测试正则表达式,我尝试了以下操作:
cat myfile.txt | egrep "^TIME\[.*\]\s\s\-\(3\)\-\.+"
这正确地识别了我想要的行。现在,删除我尝试过的模式:
cat myfile.txt | sed s/"^TIME\[.*\]\s\s\-\(3\)\-\.+"//
但它似乎只是在执行cat
,因为它显示了完整文件的内容并且没有发生替换。
我做错了什么?
操作系统:CentOS 7
最佳答案
对于显示的示例,请尝试执行以下 grep
命令。使用 GNU grep
编写和测试。
grep -oP '^TIME\[\d+\.\d+ms\]\s+-\(\d+\)-\.+\K.*' Input_file
说明:为上述代码添加详细说明。
^TIME\[ ##Matching string TIME from starting of value here.
\d+\.\d+ms\] ##Matching digits(1 or more occurrences) followed by dot digits(1 or more occurrences) followed by ms ] here.
\s+-\(\d+\)-\.+ ##Matching spaces91 or more occurrences) followed by - digits(1 or more occurrences) - and 1 or more dots.
\K ##Using \K option of GNU grep to make sure previous match is found in line but don't consider it in printing, print next matched regex part only.
.* ##to match till end of the value.
第二个解决方案:在此处添加 awk
程序。
awk 'match($0,/^TIME\[[0-9]+\.[0-9]+ms\][[:space:]]+-\([0-9]+\)-\.+/){print substr($0,RSTART+RLENGTH)}' Input_file
说明:使用awk
的match
函数,来匹配正则表达式^TIME\[[ 0-9]+\.[0-9]+ms\][[:space:]]+-\([0-9]+\)-\.+
这将捕获我们实际的文本想要从行中删除。然后打印除了 OP 实际需要的匹配文本之外的其余文本。
关于regex - 如何使用 grep/sed/awk 从文本文件开头删除模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68206907/