Extract lines that match a pattern

Hi all,

I got a file that contains the following content, Actually it is a part of the file content,

Installing XYZ XYZA Image, API 18, revision 2
  Unzipping XYZ XYZA Image, API 18, revision 2 (1%)
  Unzipping XYZ XYZA Image, API 18, revision 2 (96%)
  Unzipping XYZ XYZA Image, API 18, revision 2 (99%)
    Installed XYZ XYZA Image, API 18, revision 2

This part of the file repeats through out the file some "n" times.
The problem in front of me is i need to extract the bold line that repeats n times in the file and put them into another file.

I tried this piece of code but in vain.

grep -o "Installed*" filename > destfile

This actually matches the pattern and puts the lines after the required line.

Please let me know where i am wrong.

Thanks.


Use
grep -w 'Installed' test.txt
    Installed XYZ XYZA Image, API 18, revision 2

1 Like
-o, --only-matching
              Print only the matched (non-empty) parts of a matching line, with each  such  part  on  a  separate output line.

Below would resolve your issue.

grep "Installed" filename > Newfilename
1 Like

Hi,
Mirwasim and pravin,

I rectifies my mistake. Both of your code works fine. Thanks for that.
Can you also let me how to solve this.??

The above grep command sends the output as required in a single.
this is how the output looks like,

Installed XYZ XYZA Image, API 18, revision 2 Installed XYZ XYZA Image, API 18, revision 2 Installed XYZ XYZA Image, API 18, revision 2 Installed XYZ XYZA Image, API 18, revision 2 

Now i need the output to be in different lines along with line numbers like this,

  1. Installed XYZ XYZA Image, API 18, revision 2
  2. Installed XYZ XYZA Image, API 18, revision 2
  3. Installed XYZ XYZA Image, API 18, revision 2
  4. Installed XYZ XYZA Image, API 18, revision 2
    and so on.

Please help me in achieving this, I tried doing this but in vain as i am a newbie to shell scripting.

Thanks.

In your example input, is that actually 5 lines, or 1 line which has line-wrapped? Because neither of those grep commands should concatenate output lines like that.

Alternatively, is the output directly from grep, or are you doing something else with it first?

Carlo,

Yes neither of the commands concatenate output lines as i said.
I am trying to cat the output of the grep command inside mail delivery system,
Thats the reason the lines are concatenating.
Can you please suggest what i need to do???

The piece of code just after grep would be something like this,

grep "Installed" filename > Newfilename
cat -  <<EOF | sendmail -oi -t
From: ${MAILFROM}
To: ${MAILTO}
Subject: 
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
IMPORTANCE: High
MIME-Version: 1.0
$html
EOF

Where the variable html contains the output of cat Newfilename.

I hope i am not making things complicated.

Thaks.

Hi, try with:

cat -  <<EOF | sendmail -oi -t
From: ${MAILFROM}
To: ${MAILTO}
Subject: 
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
IMPORTANCE: High
MIME-Version: 1.0
"$html"
EOF

Regards.

this is also one possible way to pass variable inside EOF

$ sed "s/%HTML%/$html/g"<<'EOF'
xyz=%HTML%
EOF