1
 <tr>$
                                  <td nowrap valign="top" class="table_1row"><a name="d071301" id="d071301"></a>13-Jul-2011</td>$

i would like to match tr>$ <td nowrap valign, but when i try

grep -c "tr>\n<td nowrap valign" test.html

then i also tried

grep -c "tr>\n\s*<td nowrap valign" test.html

both of them find nothing. What error here?

One more question, can i add a character ^M into a html file by using sed???thanks

hk_student
  • 37
  • 5

2 Answers2

3

grep only does single line searches. Another source

To recap on some comments, sed does multiline searches and you can address the carriage return in sed syntax with \r

Community
  • 1
  • 1
Andy
  • 44,610
  • 13
  • 70
  • 69
  • please spend a few second. i would like to delete the line which has the sixth
      occurance. i google the way of doing it and sed -i '/^
        $/ d/6' file .However, it turn out a sed: -e expression #1, char 9: extra characters after command error?
    – hk_student Jul 20 '11 at 04:18
0

To add your control character, add the actual character: Type Ctrl+V, Ctrl+M.

sed will treat the control character just like a normal character.

Manny D
  • 20,310
  • 2
  • 29
  • 31
  • î would like to use sed to add so , is it sed -i 's/somthing/Ctrl+V/g' file ?? – hk_student Jul 20 '11 at 04:02
  • No, you actually type (on your keyboard) Ctrl+V, and then Ctrl+M. This is how you produce control characters in bash. They'll show up as ^M on screen but will be one character long. Your sed would look something like this: `sed -i 's/something/^M/g' file`. – Manny D Jul 20 '11 at 04:05
  • i can just using ^M in sed? but will it confused with actual text ^M? – hk_student Jul 20 '11 at 04:09
  • Nope, sed would interpret that as two characters: a carat (^) and a capital M. If you use the control character, however, sed will see that as a single character. – Manny D Jul 20 '11 at 04:10
  • I could very well be mistaken, but isn't ^M representing a carriage return, which you can just use `\r`? – Andy Jul 20 '11 at 04:13
  • please spend a few second i would like to delete the line which has the sixth
      occurance i google the way of doing it and sed -i '/^
        $/ d/6' file However, it turn out a sed: -e expression #1, char 9: extra characters after command error?
    – hk_student Jul 20 '11 at 04:16
  • @Andy, you're correct. `^M` is equivalent to `\r`, which is probably what @hk should be using since it's more clear. – Manny D Jul 20 '11 at 04:19
  • You should open another question up about this and accept an answer (if you feel it is acceptable) for this one – Andy Jul 20 '11 at 04:19
  • alright but in the above case which one would match ? grep -c "tr>\n\n\s* – hk_student Jul 20 '11 at 04:22
  • neither, grep isn't going to look at more than one line, so when you put a new line in the middle of your regex, it is never going to match. – Andy Jul 20 '11 at 04:28