Find between lines start and end: awk with bash variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find between lines start and end: awk with bash variables
# 1  
Old 09-04-2015
Find between lines start and end: awk with bash variables

I have a file as follows:
Code:
0
1056
85540
414329
774485
1208487
1657519
2102753
2561259
3037737
3458144
3993019
4417959
4809964
5261890
5798778
6254146

I want to find all lines between a specified start and end tag.

I assign these values using shell variables as so:
Code:
start=0
end=1056

I do similar assignments repetitively, but for brevity I am showing just one execution of a loop.

I use AWK as below:

Code:
awk -v s="$start" -v e="$end" '$0~s,$0~e' file

The expected output is:
Code:
0
1056

but the output that I get is:
Code:
0
1056
85540
414329
774485
1208487
1657519
2102753
2561259
3037737
3458144
3993019
4417959
4809964
5261890
5798778
6254146

.

The same statements work for other start and end values, but not for 0 and 1056.
Could someone point me to what is wrong here.
# 2  
Old 09-04-2015
Hello Jamie_123,

Following may help you in same.
Code:
 awk -vstart=1 -vend=6 '(NR>=start && NR<=end)'  Input_file

I have just given example for values of start and end variables, you can change it to according to your requirement.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-04-2015
Thanks for the response. However I am looking to match to the content of the line and not line numbers.

Last edited by jamie_123; 09-04-2015 at 06:24 AM..
# 4  
Old 09-04-2015
Quote:
Originally Posted by jamie_123
.
.
.
The same statements work for other start and end values, but not for 0 and 1056.
. . .
That's because there's many "0"s in them there numbers, so the range immediately starts again (and doesn't get stopped any more). Try to anchor the "0":
Code:
awk -v s="$start" -v e="$end" '$0~"^"s,$0~e' file
0
1056

---------- Post updated at 11:22 ---------- Previous update was at 11:19 ----------

Your file doesn't have 1056 lines. RavinderSingh13's proposal lists until EOF or line 1056, whatever it reaches first.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-04-2015
Thanks for the response RudiC. Could you throw me a pointer on the difference between the cap and the tilda. I thought the tilda sign is used to anchor.
# 6  
Old 09-04-2015
Hello Jamie,

Following may help you. Let's say we have following input file.
Code:
cat test121
TEST1211
TEST
RTEST

Now example of ^.
Code:
awk '/^R/' test121
RTEST

Means it will show all text lines which are starting with letter R as above.

Example for ~.
Code:
awk '($0 ~ /TEST/)' test121
TEST1211
TEST
RTEST

Means if any line contains string TEST but it may contain any other additional values to which is match but not exact match as above.

For an exact match we can do following.
Code:
awk '($0 == "TEST")' test121
TEST

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 09-04-2015
The ~ matches any occurence of the search pattern in the string, regardsless of its position. You can anchor the pattern at the start (with ^ ) or at the end (with $ ) of the string as I did with your sample (by just adding he ^ ).
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. UNIX for Beginners Questions & Answers

How do delete certain lines alone which are matching with start and end string values in file?

Hi, In my previous post ( How to print lines from a files with specific start and end patterns and pick only the last lines? ), i have got a help to get the last select statement from a file, now i need to remove/exclude the output from main file: Input File format: SELECT ABCD, DEFGH,... (2 Replies)
Discussion started by: nani2019
2 Replies

3. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Get the lines from logfile within start and end date

Hi guys, I am having the below logfile,date in yyyy-mm-dd 2013-08-02 *some content* 2013-08-02 *some content* 2013-08-02 *some content* 2013-08-03 *some content* 2013-08-05 *some content* from the above logfile i need to get the lines between the two timestamps,if i give... (5 Replies)
Discussion started by: mohanalakshmi
5 Replies

5. Shell Programming and Scripting

awk to count start and end keyword in a line

Hello fellow awkers and seders: need to figure out a way to ensure a software deployment has completed by checking its trace file in which I can store the deployment results as follows: echo $testvar ===== Summary - Deploy Result - Start ===== ===== Summary - Deploy Result - End =====... (1 Reply)
Discussion started by: ux4me
1 Replies

6. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

7. Shell Programming and Scripting

Fetching a set of lines with start and end

Hi Folks, Need help in fetching a group of lines with a start and end strings. Example is shown below. start#morning tea jog breakfast end start#afternoon lunch work chat end start#evening snacks tea chat (6 Replies)
Discussion started by: jayadanabalan
6 Replies

8. UNIX for Dummies Questions & Answers

Find Quarter Start and End Dates

Dear Members, Depending on the current date i should find out the start and end dates of the quarter. ex: Today date is 14-Nov-2011 then Quarter start date should be Oct 1 2011 and Quarter End date should be Dec 31 2011. How can i do this? Thanks Sandeep (1 Reply)
Discussion started by: sandeep_1105
1 Replies

9. Shell Programming and Scripting

read a string from its end to its start and stop when you find a specific character

How can I do this? Actually I have a file which contains a path e.g. /home/john/Music/hello.mp3 and I want to take only the filename (hello.mp3) So, I need to read the file from its end to its start till the character "/" Is this possible? Thanks, I am sure you'll not disappoint me here! Oh,... (9 Replies)
Discussion started by: hakermania
9 Replies

10. Shell Programming and Scripting

String as both start and end anchors in awk

Not sure if the title of this thread makes sense, but hopefully my explanation will. I'm using awk to print some stats from an apache accesslog. I would like to specify the regexp condition where only the two root pages of "index.html" and "/" are counted in my results. What I can't figure out... (3 Replies)
Discussion started by: picassolsus
3 Replies
Login or Register to Ask a Question