in reply to match word on line - match word on another line - print lines

you could also try this

cat YourFile|perl -pe'$/="Title:";chomp;$_=/invalid url/?"Title:".$_:" +"'

A quick explanation as this does look a bit line noise like. Perl is started with the options p and e. e gives the code to execute on the command line, p puts a while loop around the code with a continue block that just prints $_.

A very useful trick is to deparse a script to see better what is going on, lets do that now to make it more readable and so you can see the while and print added by the -p

pre> perl -MO=Deparse -pe'$/="Title:";chomp;$_=/invalid url/?"Title:". +$_:""' -e syntax OK LINE: while (defined($_ = <ARGV>)) { $/ = 'Title:'; chomp $_; $_ = /invalid url/ ? 'Title:' . $_ : '' } continue { print $_; }
$/ is the input record seperator and by setting this to "Title:" I get perl to read one whole block at a time (up to and including the next occurance of Title: or EOF).

The chomp removes the record seperator (Title:) from the end of the record, trailing titles would be ugly.

The last part sets $_ depending on whether the block we are examining contains the string "invalid url". The Test?value1:value2 construct returns the first value if the test is true and the second if it fails. In this case we either fix up $_ for printing by prefixing "Title:" or we set it to an empty string so nothing is printed.

Cheers,
R.