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: adminpassword minumim length: 8user-type: authpassword minumim length: 8I get: 8min 8thWhen I expected:admin 8auth 8It looks like the second saved match is printed from the beggining of the line and replaces what was previously printed.Any idea?
multiline sed substitution overwrites previous output
sed;regular expression
Your command works for me:$ sed -r '/user-type/{N;s/user-type: (.*)\n.*gth: .*([0-9]+).*$/\1 \2/}' file.txtadmin 8auth 8If, 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 8thOne 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 8auth 8Or:$ sed -r '/user-type/{N;s/user-type: ([^\r]*).?\n.*gth: .*([0-9]+).*$/\1 \2/}' <(sed 's/$/\r/' <file.txt)admin 8auth 8