1

I am trying to do a sed substitution over two lines like this:

sed -r '/user-type/{N;s/user-type: (.*)\n.*gth: .*([0-9]+).*$/\1 \2/}'

I got this syntax from an answer here, which explains I first match the first line of the pattern and then add the next line to the pattern space with N to make the substitution. This has worked for me on previous occasions, but this time the weirdest thing happens.

For input:

user-type: admin
password minumim length: 8
user-type: auth
password minumim length: 8

I get:

 8min
 8th

When I expected:

admin 8
auth 8

It looks like the second saved match is printed from the beggining of the line and replaces what was previously printed.

Any idea?

1

Your command works for me:

$ sed -r '/user-type/{N;s/user-type: (.*)\n.*gth: .*([0-9]+).*$/\1 \2/}' file.txt
admin 8
auth 8

If, however, if convert the input to DOS format, I get the output that you see:

$ sed -r '/user-type/{N;s/user-type: (.*)\n.*gth: .*([0-9]+).*$/\1 \2/}' <(sed 's/$/\r/' <file.txt)
 8min
 8th

One solution is to convert your input file to unix-style line-endings.

Another solution is to adjust the sed command to be tolerant of \r characters:

$ sed -r '/user-type/{N;s/user-type: (.*)\n.*gth: .*([0-9]+).*$/\1 \2/; s/\r//}' <(sed 's/$/\r/' <file.txt)
admin 8
auth 8

Or:

$ sed -r '/user-type/{N;s/user-type: ([^\r]*).?\n.*gth: .*([0-9]+).*$/\1 \2/}' <(sed 's/$/\r/' <file.txt)
admin 8
auth 8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.