Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

My file containing the following lines,

$BQ  
{ VOL       @home    }  
database    daba  
relation    tcdeatid  
opendb  
clear    
.lruno := 72         <-- (This line lruno. := 72 has 10 times in my file1)
.infno := 1  
.tid.noel := 101       
writedb       
clear  
.lruno := 72  
.infno := 205  
.tid.noel := 191      
writedb       
clear    
$EOF

In file no.2, I want only this part mentioned below from file1.

$BQ  
{ VOL       @home    }  
database    daba  
relation    tcdeatid  
opendb  
clear
.lruno := 72           
.infno := 1  
.tid.noel := 101  
.lruno := 72           
.infno := 205  
.tid.noel := 191
.lruno := 72           
.infno := 0  
.tid.noel := 1111  
.lruno := 72           
.infno := 56  
.tid.noel := 231
.lruno := 72           
.infno := 45  
.tid.noel := 61  
.lruno := 72           
.infno := 23  
.tid.noel := 901
.lruno := 72           
.infno := 123  
.tid.noel := 1611  
.lruno := 72           
.infno := 786  
.tid.noel := 81
.lruno := 72           
.infno := 55  
.tid.noel := 1  
.lruno := 72           
.infno := 253  
.tid.noel := 121
writedb       
clear  
$EOF

I want this chunk of lines (lruno. := 72) in a another file using shell commands from file1. I want the command which will read file and count how many times has lruno. := 72 in file1 and write it down the total to another file.

share|improve this question

closed as unclear what you're asking by roaima, Anthon, psusi, chaos, Michael Homer Jun 11 at 20:54

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

    
    
What are your criteria for selecting the lines you want to keep or delete? –  roaima Jun 11 at 19:27
    
I dont want to delete any lines from file... I just want to print particular lines which containing lruno := 72 in my file2 (It has 10 times in my file1) from file1. I dont written the whole in file1 but my the output file2 I have shown in my question. –  pmaipmui Jun 12 at 3:29

2 Answers 2

up vote 1 down vote accepted

Using sed

By selecting the lines starting with .lruno := 72 and continuing to the next clear, this produces the output you request:

$ sed -n '/.lruno := 72/,/clear/p' file
.lruno := 72
.infno := 1
.tid.noel := 101
.tid.info := 64
.tid.setnr := 1225
.typeidm := 1
.sourcetable := 2
writedb
clear
.lruno := 72
.infno := 205
.tid.noel := 101
.tid.info := 76
.tid.setnr := 1225
.typeidm := 1
.sourcetable := 2
writedb
clear

How it works: -n tells sed not to print unless we ask it to. /.lruno := 72/,/clear/p tells it to print all ranges that start with .lruno := 72 and end with clear.

Using awk

The same approach works using awk:

awk '/.lruno := 72/,/clear/' file

Just as in sed, /.lruno := 72/,/clear/ selects groups of lines starting with .lruno := 72 and ending with clear. Since no action is specified for those selected lines, awk performs its default action which is to print them.

share|improve this answer
    
Great.. That's exactly what I was looking for... @john1024 –  pmaipmui Jun 12 at 19:07

Using sed:

< inputfile sed '1,6d; $d' > outputfile
  • < inputfile: redirects the content of inputfile to sed's stdin
  • > outputfile: redirects the content of sed's stdout to outputfile
  • 1,6d: deletes all lines from the 1st to the 6th inclusive
  • $d: deletes the last line

Sample output:

~/tmp$ cat inputfile
$BQ
{ VOL @home }
database daba
relation tcdeatid
opendb
clear
.lruno := 72
.infno := 1
.tid.noel := 101
.tid.info := 64
.tid.setnr := 1225
.typeidm := 1
.sourcetable := 2
writedb
clear
.lruno := 72
.infno := 205
.tid.noel := 101
.tid.info := 76
.tid.setnr := 1225
.typeidm := 1
.sourcetable := 2
writedb
clear
.lruno := 18
.infno := 2
.tid.noel := 100
.tid.info := 34
.tid.setnr := 125
.typeidm := 9
.sourcetable := 2
writedb
clear
.lruno := 18
.infno := 25
.tid.noel := 1101
.tid.info := 71
.tid.setnr := 1425
.typeidm := 1
.sourcetable := 3
writedb
clear
$EOF
~/tmp$ < inputfile sed '1,6d; $d'
.lruno := 72
.infno := 1
.tid.noel := 101
.tid.info := 64
.tid.setnr := 1225
.typeidm := 1
.sourcetable := 2
writedb
clear
.lruno := 72
.infno := 205
.tid.noel := 101
.tid.info := 76
.tid.setnr := 1225
.typeidm := 1
.sourcetable := 2
writedb
clear
.lruno := 18
.infno := 2
.tid.noel := 100
.tid.info := 34
.tid.setnr := 125
.typeidm := 9
.sourcetable := 2
writedb
clear
.lruno := 18
.infno := 25
.tid.noel := 1101
.tid.info := 71
.tid.setnr := 1425
.typeidm := 1
.sourcetable := 3
writedb
clear
~/tmp$ 
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.