Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex in sed to keep current line but remove next line

Tags:

regex

bash

sed

This is sample data:

ServerA
Value1 fh824rfz
Plan CustomA
ServerB
Value3 9fgjzxlo
Plan CustomD
ServerC
Value10 339fgh0l
Plan CustomE

This is the regex works in vscode:

(Value[0-9]{1,2} [0-9a-z]{8}\n)(.*)

Expected output:

ServerA
Value1 fh824rfz
ServerB
Value3 9fgjzxlo
ServerC
Value10 339fgh0l

But I'm trying in sed with regexes like this but they don't work:

-E 's|(Value[0-9]{1,2} [0-9a-z]{8}\n)(.*)\n|\1|g'
-re 's|(Value[0-9]{1,2} [0-9a-z]{8}\n)(.*)\n|\1|g'
-zre 's|(Value[0-9]{1,2} [0-9a-z]{8}\n)(.*)\n|\1|g'

How can I do this? I think the issue is with \n because when I remove that, the sample works (but still it's not the expected output).

like image 632
Saeed Avatar asked Oct 30 '25 02:10

Saeed


1 Answers

Here's one way to do it (checked with GNU sed, syntax might vary for other implementations):

$ sed -E '/Value[0-9]{1,2} [0-9a-z]{8}$/{n; d}' ip.txt
ServerA
Value1 fh824rfz
ServerB
Value3 9fgjzxlo
ServerC
Value10 339fgh0l

n command prints the pattern space (if auto-print is not disabled by -n option), replaces it with the next line and d will delete it.

like image 65
Sundeep Avatar answered Oct 31 '25 17:10

Sundeep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!