Extract lines that match a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract lines that match a pattern
# 1  
Old 10-04-2013
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,

Code:
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.

Code:
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.
# 2  
Old 10-04-2013
Quote:
Originally Posted by Kashyap
Hi all,

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

Code:
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.

Code:
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.

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

This User Gave Thanks to mirwasim For This Post:
# 3  
Old 10-04-2013
Code:
-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.

Code:
grep "Installed" filename > Newfilename

This User Gave Thanks to pravin27 For This Post:
# 4  
Old 10-04-2013
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,
Code:
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.
# 5  
Old 10-04-2013
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?

Last edited by CarloM; 10-04-2013 at 10:37 AM..
# 6  
Old 10-04-2013
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,
Code:
grep "Installed" filename > Newfilename

Code:
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.
# 7  
Old 10-04-2013
Hi, try with:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with pattern match and Extract

Hi All, I am having a file like below . Basically when SB comes in the text with B. I have to take the word till SB. When there only B I should take take till B. Tried for cut it by demilter but not able to build the logic SB- CD B_RESTO SB_RESTO CRYSTALS BOILERS -->There SB and B so I... (6 Replies)
Discussion started by: arunkumar_mca
6 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

Printing next 6 lines from of pattern match

Hi, i have a big file having many opcodes. if (opcode="01110000000100000000" ) then --fadd result.opcode := "01110000000100000000"; result.s0 := '1'; result.s1 := '1'; result.s2 := '0'; result.inst := '0'; result.scalar := '1';... (7 Replies)
Discussion started by: twistedpair
7 Replies

4. Shell Programming and Scripting

Print lines that do not match the pattern

I need to print the lines that do not match a pattern. I tried using grep -v and sed -n '/pattern/!p', but both of them are not working as I am passing the pattern as variable and it can be null some times. Example ........ abcd...... .........abcd...... .........abcd......... (4 Replies)
Discussion started by: sunny1234
4 Replies

5. Shell Programming and Scripting

Pattern Match & Extract from a string

Hi, I have long string in 2nd field, as shown below: REF1 | CLESCLJSCSHSCSMSCSNSCSRSCUDSCUFSCU7SCV1SCWPSCXGPDBACAPA0DHDPDMESED6 REF2 | SBR4PCBFPCDRSCSCG3SCHEBSCKNSCKPSCLLSCMCZXTNPCVFPCV6P4KL0DMDSDSASEWG I have a group of fixed patterns which can occur in these long strings & only... (11 Replies)
Discussion started by: karumudi7
11 Replies

6. Shell Programming and Scripting

counting lines that match pattern

I have a file of 1.3 millions lines. some are with the same word twice on the line, some line have two diffrent words. each line has two words, one in brackets. example: foo (foo) bar (bar) thae (awvd) beladf (vswvw) I am sure this can be done with one line of... (6 Replies)
Discussion started by: robsonde
6 Replies

7. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

8. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

9. Shell Programming and Scripting

Extract data from records that match pattern

Hi Guys, I have a file as follows: a b c 1 2 3 4 pp gg gh hh 1 2 fm 3 4 g h i j k l m 1 2 3 4 d e f g h j i k l 1 2 3 f 3 4 r t y u i o p d p re 1 2 3 f 4 t y w e q w r a s p a 1 2 3 4 I am trying to extract all the 2's from each row. 2 is just an example... (6 Replies)
Discussion started by: npatwardhan
6 Replies

10. Shell Programming and Scripting

Match first pattern first then extract second pattern match

My input file: <accession>Q91G55</accession> <name>043L_IIV6</name> <protein> <recommendedName> <location> <position position="294"/> </location> <fullName>Uncharacterized protein 043L</fullName> <accession>P18556</accession> <name>1106L_ASFB7</name> <protein> <recommendedName>... (5 Replies)
Discussion started by: patrick87
5 Replies
Login or Register to Ask a Question