Gripsiden Gripsiden - 3 months ago 9
Perl Question

Perl or Bash Script - Prefix Lines in text file with text from earlier in file

I have a file like:

!Denver

Line 1
Line 2
Line 3

!New York

Line 1
Line 2


I want to do the following - Basically pull the text with prefix ! ( like !Denver ) and append the text , less "!" to the next lines until another ! comes along with new text for proceeding lines

Denver.Line 1
Denver.Line 2
Denver.Line 3
New York.Line 1
New York.Line 2


This is part of a bigger script - but am hoping to complete the rest myself.

I found the following which i am reviewing:



So I may get an answer myself shortly.

Thanks!

Answer
 perl -ne 'if (s/^!//) { chomp; $p = $_ } elsif (/\S/) { print "$p.$_" }'  < data
Comments