October 21st, 2003, 09:25 PM
-
sed command problem
hi all. pls help.
i am using the sed command to substitute several text patterns in a file. my problem is, i want to replace the pattern with a new line. i cant seem to make \n work. here's an example. the patterns to be replaced with a next line are "abc", "def" and "hij".
input file:
qwertyabcuiop asdfgdefhjkl hijzxcvbnm
output file shoule be like this:
qwerty
uiop asdfg
hjkl
zxcvbnm
thanks.
October 22nd, 2003, 06:19 AM
-
To my knowledge, \n doesn't work in sed. But you can spilt lines:
Code:
#!/bin/bash
echo "This is a test sentence" | \
sed -n 's/This is\(.*\)/This is\
\1/p'
=> This is\(.*\)
Searchpatern.
=> This is\
=> \1
Output patern. The \ after is is there for the linebreak, the \1 prints all that was captured with the \(.*\).