SED to remove a Line with REGEX Pattern

时光总嘲笑我的痴心妄想 提交于 2020-12-30 06:59:47

问题


i've got a hundreds of files with thousands of lines, which i need to delete some lines that follows a pattern,so i went to SED with regex .The struct of files is something like this

A,12121212121212,foo,bar,lorem
C,32JL,JL
C,32JL,JL
C,32JL,JL
C,32JL,JL
A,21212121212121,foo,bar,lorem
C,32JL,JL
C,32JL,JL
C,32JL,JL
A,9999,88888,77777

I need to delete All the lines that starts with "A" and ends with "lorem"

Expected output-

C,32JL,JL
C,32JL,JL
C,32JL,JL
C,32JL,JL
C,32JL,JL
C,32JL,JL
C,32JL,JL
A,9999,88888,77777

I've made the Regex :

^(A).*(lorem)

And it match in my text editor (Sublime,UltraEdit)

In the UNIX shell

sed '/^(A).*(lorem)/d' file.txt

But somehow it doesn't work,it shows the whole file, and i can't figure out why.

Can someone help me please?


回答1:


$ sed '/^A.*lorem$/d' file.txt
  • ^A: starts with an A
  • .*: stuff in the middle
  • lorem$: ends with lorem



回答2:


The others gave you correct solutions but didn't explain why your regex didn't work. The () surely were useless, but if you had used the regex with other tools/languages, you might very well have had the expected result.

It didn't work with sed because it will by default use POSIX's basic regular expressions, where the characters for grouping are \( and \), while ( and ) will match literal characters. There were no such brackets in your input text, so it didn't match.

Your regular expression would have worked if you had used GNU's sed -r or BSD's sed -E, the flag switching to POSIX's extended regular expressions where ( and ) are used to group and \( \) match the literal brackets.

In conclusion, the following commands will do the same thing :

  • sed '/^A.*lorem$/d' file.txt
  • sed -r '/^(A).*(lorem)$/d' file.txt (with GNU sed)
  • sed -E '/^(A).*(lorem)$/d' file.txt (with BSD sed and modern GNU sed)
  • sed '/^\(A\).*\(lorem\)$/d' file.txt



回答3:


Remove the brackets.

Using your code, the appropriate one-liner becomes-

sed '/^A.*lorem/d' file.txt

If you want to be more rigourous, you can look at James's answer which more correctly terminates the regex as-

sed '/^A.*lorem$/d' file.txt

Both will work.



来源:https://stackoverflow.com/questions/40241433/sed-to-remove-a-line-with-regex-pattern

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!