String as both start and end anchors in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String as both start and end anchors in awk
# 1  
Old 08-20-2008
Question 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 is how, using regexp or otherwise, I can specify that I only want the forwardslash entries counted ONLY if the forwardslash is at BOTH the start and end of the line....in otherwords, it's the ONLY character on that line. Here's what I have that I thought would work but isn't:

Code:
awk '$7 ~ /^\/index.html|^$\// {print $7}' access_log |sort |uniq -c

I suppose I can't expect the machine to interpret my placing ^ and $ next to one another to mean "this string is at the beginning AND end of the line".

Any ideas? Thanks all.
# 2  
Old 08-20-2008
It'd be better if you post some sample of your data, in the meantime to retrieve / count the lines that start and end with a forward slash, you can try this,

Code:
awk '/^\/index.html/ && /\/$/ { print ... }' ...

# 3  
Old 08-20-2008
Rubin,

Thanks for the direction. Here are a few lines (field 7 only) of the accesslog I'm working with:

/ (count lines like this one)
/index.html (count lines like this one)
/
/
/portals/index.html (don't want lines like this one counted)
/family_medicine/index.html
/
/
/portals/pda/ (don't want lines like this one counted)
/portals/surgery/
/portals/pediatrics/


Where each line that contains "/" and "/index.html" are the only lines I'm interested in. Note: I'm only interested in the root level index.html not the cases like "/portals/index.html".

Using your code, I'm not finding any matches. When trying just one of the conditions, such as:
Code:
awk '$7 ~ /^\/index.html/ {print $7}' access_log-20080213 |sort |uniq -c

it works fine-- I'm showing a count of 5 hits on "/index.html". However, when I try your example of combining the two conditions, that's where I'm not getting any results at all. If I try ONLY your added condition (awk '$7 ~ /\/$/ {print $7}' ....), I get results including lines such as "/portals/pediatrics/" which I don't want.

The AND/OR logic is a touch confusing for me here because I definitely want the count incremented for BOTH conditions, but by using AND it's not going to take it that I mean that field 7 must contain BOTH "/" and "/index.html" per line will it? Obviously OR may not apply here because I don't want it choosing between the two conditions-- I want them both counted.

I hope this clarified and didn't complicate! thanks again.

Derek

Last edited by picassolsus; 08-20-2008 at 03:50 PM..
# 4  
Old 08-20-2008
One way ...
Code:
awk '$7 == "/index.html"  ||  $7 == "/" {print $7}' file

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 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

2. Shell Programming and Scripting

Search a String between start and end of a block in a file

Hi, I have a scenario where I want to display the output based on the pattern search between the start and end of a block in a file, we can have multiple start and end blocks in a file. Example give below, we need to search between the start block abc and end block def in a file, after that... (5 Replies)
Discussion started by: G.K.K
5 Replies

3. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

4. UNIX for Beginners Questions & Answers

How to use awk to print strings with anchors?

I have a list of countries and I'm trying to print the country names that start with "A" and end in "a". I can do the first one which gives me the correct output. awk '/^A/ {print $1}' countries.txt but when I try: awk '/^A a$/ {print $1}' countries.txt or even: awk... (2 Replies)
Discussion started by: steezuschrist96
2 Replies

5. Shell Programming and Scripting

Find between lines start and end: awk with bash variables

I have a file as follows: 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. (6 Replies)
Discussion started by: jamie_123
6 Replies

6. 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

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

8. 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

9. Shell Programming and Scripting

Appending string, variable to file at the start and string at end

Hi , I have below file with 13 columns. I need 2-13 columns seperated by comma and I want to append each row with a string "INSERT INTO xxx" in the begining as 1st column and then a variable "$node" and then $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13 and at the end another string " ; COMMIT;" ... (4 Replies)
Discussion started by: Vaddadi
4 Replies

10. Shell Programming and Scripting

Awk: Remove comma at the end of the string

Hi i had String like UID: ABC345QWE678GFK345SA90, LENGTH 32 when I used awk ' FS, {print $1}' prints ABC345QWE678GFK345SA90, how can i getrid of that coma at the end of the string. Thanks in advance.. (14 Replies)
Discussion started by: Reddy482
14 Replies
Login or Register to Ask a Question