script to grab lines between two values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to grab lines between two values
# 1  
Old 09-18-2008
script to grab lines between two values

hi guys

say I have a file that contains

Code:
hello
world
this 
is
AAAAA
message
to
try
BBBBBB
and 
see
if
anyone
AAAAA
knows
the
answer
BBBBBB
thanks

Now I would like a script so that it will grab AAAAA and BBBBBB and everything in between and write to a new file

i.e.

I would like in my file


Code:
AAAAA
message
to
try
BBBBBB

AAAAA
knows
the
answer
BBBBBB

thanks
# 2  
Old 09-18-2008
is this what you wanted.. just check..
Quote:
awk '/AAAAA/,/BBBBBB/' infile > outfile
# 3  
Old 09-18-2008
Quote:
Originally Posted by vidyadhar85
is this what you wanted.. just check..
that is exactly what I wanted vidya, many thanks
# 4  
Old 09-19-2008
Code:
awk '{
 if($0=="AAAAA")
       a=1
 if(a==1)
       print
 if($0=="BBBBBB")
 {
       print ""
      a=0
 }
}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk remove/grab lines from file with pattern from other file

Sorry for the weird title but i have the following problem. We have several files which have between 10000 and about 500000 lines in them. From these files we want to remove lines which contain a pattern which is located in another file (around 20000 lines, all EAN codes). We also want to get... (28 Replies)
Discussion started by: SDohmen
28 Replies

2. Shell Programming and Scripting

Q: grab email username from script

Hi all, I want to set up a script to email a person to confirm another program is running but i wish to avoid having to customise the script for each individiual. I am ok with what i need to do except i need to get the first and last name of the user to construct an email address to send to.... (6 Replies)
Discussion started by: KlintJ
6 Replies

3. Shell Programming and Scripting

awk to grab the most found lines

awk 'NR>=5034&&/Max/{++c}c==3{o=$0 RS $0 RS $0; print o; c=0}' logfile i would like to modify the above awk command to instead show lines that are the most frequent: for instance, given a log file containing: Warning MaxClient is having issues - please MaxClients java error...yams... (4 Replies)
Discussion started by: SkySmart
4 Replies

4. Shell Programming and Scripting

grab shell script error

How do I capture an error for any command I use. For e.g if i try to zip a file and the file is not there. The regualr $? -gt 0 only tells its un-successful but won't tell me that the file is not there when I run through a script. How can I capture this error? for e.g.. zip $x.zip $x... (1 Reply)
Discussion started by: dsravan
1 Replies

5. Shell Programming and Scripting

AWK script - extracting min and max values from selected lines

Hi guys! I'm new to scripting and I need to write a script in awk. Here is example of file on which I'm working ATOM 4688 HG1 PRO A 322 18.080 59.680 137.020 1.00 0.00 ATOM 4689 HG2 PRO A 322 18.850 61.220 137.010 1.00 0.00 ATOM 4690 CD ... (18 Replies)
Discussion started by: grincz
18 Replies

6. Shell Programming and Scripting

Help with script to read lines from file and count values

Hi, I need some help with a script I'm trying to write. I have a log file containing references to a number of different webservices. I wish to write a script that will list the webservices with a count as to how many times they appear in the log. An example of the log file content: ... (2 Replies)
Discussion started by: gman2010
2 Replies

7. Shell Programming and Scripting

grab PID of a process and kill it in a script

#!/bin/sh who echo "\r" echo Enter the terminal ID of the user in use: echo "\r" read TERM_ID echo "\r" ps -t $TERM_ID | grep sh echo "\r" echo Enter the process number to end: echo "\r" read PID echo "\r" kill -9 $PID What this code does is ultimately grab the PID of a users sh... (6 Replies)
Discussion started by: psytropic
6 Replies

8. Shell Programming and Scripting

Help!! Need script to read files and add values by lines...

Hi All, I really need your help. I am a begginner in shell script and I believe this is a very simple issue. I have in my directory, n-files, like 1.dhm, 2.dhm, 3.dhm. These files have 1 column with 1 value per line, like: 1.dhm ------ 10 20 30 40 50 2.dhm ------ 30 50 20 (3 Replies)
Discussion started by: dhuertas
3 Replies

9. Shell Programming and Scripting

grab next consecutive line or lines

Hello i'm writting a krn shell that will find the letter F and I would like to grab everything from F till 0:00. Is there a sed or awk command that i could use. Thank you. File is looks like this. 11/12 xxx xxxx xxx F xxxx xxxx some info entered here some info entered here xxxx... (1 Reply)
Discussion started by: wisher115
1 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question