LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-27-2008, 03:26 AM   #1
viveksnv
Member
 
Registered: Feb 2008
Posts: 38

Rep: Reputation: 15
getting selected lines in a file


Hello all,

my file has
KEY 9D1D479E5AEE1C4219B3B56735266609
########GET http://top100-images.rambler.ru/top100/w1.gif
########STORE_OK IN_MEMORY SWAPOUT_NONE PING_DONE
########CACHABLE,DISPATCHED,NEGCACHED,VALIDATED
########04087859 LU:1204088178 LM:-1 EX:1204088478
########0 locks, 0 clients, 1 refs
########Swap Dir -1, File 0XFFFFFFFF
KEY D57DBE38D0D3F84960FE6526A289CC6D
########GET http://wiki.squid-cache.org/wiki/squ...css/common.css
########STORE_OK IN_MEMORY SWAPOUT_DONE PING_DONE
########CACHABLE,DISPATCHED,VALIDATED
########LV:1203939464 LU:1204085787 LM:1117795312 EX:-1


IMPORTANT:
(######## -> space. Not char

I try print only the
http://top100-images.rambler.ru/top100/w1.gif
http://wiki.squid-cache.org/wiki/squ...css/common.css

Please help me.
 
Old 02-27-2008, 03:29 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
what is the code you have?
 
Old 02-27-2008, 04:05 AM   #3
livetoday
Member
 
Registered: Jun 2006
Location: India
Distribution: RHEL,Suse,Fedora
Posts: 106

Rep: Reputation: 15
while playing with awk command to answer the query ...I found that the following command is giving the required output..

Code:
cat file |  awk '$1 = /http/ {print $2}'
http://top100-images.rambler.ru/top100/w1.gif
http://wiki.squid-cache.org/wiki/squ...css/common.css
But I don't know how ??

I am using GNU awk. But I don't know how awk is working in this scenario? Can anyone explain ?
 
Old 02-27-2008, 05:09 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by livetoday View Post
while playing with awk command to answer the query ...I found that the following command is giving the required output..

Code:
cat file |  awk '$1 = /http/ {print $2}'
http://top100-images.rambler.ru/top100/w1.gif
http://wiki.squid-cache.org/wiki/squ...css/common.css
But I don't know how ??

I am using GNU awk. But I don't know how awk is working in this scenario? Can anyone explain ?
first you can do away with the cat. Its useless in this scenario. Your usage of awk is a bit "unconventional". however it will still work in this case.
Quote:
Code:
$1 = /http/
the above means , look for pattern http, then if found, assign its value to $1. In awk, if a pattern is found, the return value would be 1. So $1=/http/ means assign 1 to $1. you can try this
Code:
# awk '$1=/KEY/{print $1,$2}' file
1 9D1D479E5AEE1C4219B3B56735266609
1 D57DBE38D0D3F84960FE6526A289CC6D
the "usual" way is this ...
Code:
# awk '/http/{print $2}' file
http://top100-images.rambler.ru/top100/w1.gif
http://wiki.squid-cache.org/wiki/squ...css/common.css

Last edited by ghostdog74; 02-27-2008 at 06:59 AM.
 
Old 02-28-2008, 06:52 AM   #5
livetoday
Member
 
Registered: Jun 2006
Location: India
Distribution: RHEL,Suse,Fedora
Posts: 106

Rep: Reputation: 15
Thanks a lot for this detailed explanation. This certainly clears my doubts however while reading through your answer a new query pops up.

Is is possible to print only that field in which awk has found the pattern.

Quote:
Originally Posted by ghostdog74 View Post
first you can do away with the cat. Its useless in this scenario. Your usage of awk is a bit "unconventional". however it will still work in this case.

the above means , look for pattern http, then if found, assign its value to $1. In awk, if a pattern is found, the return value would be 1. So $1=/http/ means assign 1 to $1. you can try this
Code:
# awk '$1=/KEY/{print $1,$2}' file
1 9D1D479E5AEE1C4219B3B56735266609
1 D57DBE38D0D3F84960FE6526A289CC6D
the "usual" way is this ...
Code:
# awk '/http/{print $2}' file
http://top100-images.rambler.ru/top100/w1.gif
http://wiki.squid-cache.org/wiki/squ...css/common.css
 
Old 02-28-2008, 07:06 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
sed -n '/http:/p' filename

Quote:
Originally Posted by ghostdog74
what is the code you have?
Why didn't we stop here??...
 
Old 02-28-2008, 07:14 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
you can use a for loop to go through the fields
Code:
awk '
{
  for(i=1;i<=NF;i++){
    if ($i ~ /pattern/) print $i
  }
}
'
you can also use match() with RSTART and RLENGTH as the variables set when the pattern is found. However they just show you where the pattern starts and end, not which field though.
 
Old 02-28-2008, 07:16 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by pixellany View Post
sed -n '/http:/p' filename

Why didn't we stop here??...
hmm...beats me
 
Old 02-28-2008, 09:55 PM   #9
livetoday
Member
 
Registered: Jun 2006
Location: India
Distribution: RHEL,Suse,Fedora
Posts: 106

Rep: Reputation: 15
Thanks again....

Yeah ! I also feel sed is easiser to use to get the desired output in this case.

Code:
sed -n 's/.*\(http:*\)/\1/p' filename

http://top100-images.rambler.ru/top100/w1.gif
http://wiki.squid-cache.org/wiki/squ...css/common.css

Quote:
Originally Posted by ghostdog74 View Post
you can use a for loop to go through the fields
Code:
awk '
{
  for(i=1;i<=NF;i++){
    if ($i ~ /pattern/) print $i
  }
}
'
you can also use match() with RSTART and RLENGTH as the variables set when the pattern is found. However they just show you where the pattern starts and end, not which field though.
 
Old 02-28-2008, 10:27 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by livetoday View Post
Thanks again....

Yeah ! I also feel sed is easiser to use to get the desired output in this case.
for this case, sure.

Quote:
Code:
sed -n 's/.*\(http:*\)/\1/p' filename

http://top100-images.rambler.ru/top100/w1.gif
http://wiki.squid-cache.org/wiki/squ...css/common.css
you might want to add an extra space after the http pattern so that you won't grab more than what you want.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hw to clear the selected lines from History? linuxxx123 Linux - Newbie 6 01-08-2009 03:56 PM
Random file lines directed to a new file. In script an error. In command line no err leventis Programming 1 09-28-2006 07:16 AM
how to unzip a zip file on a selected dir in linux ? sunlinux Linux - Software 1 06-12-2006 04:52 AM
Copy selected with specified file extensions dickb Linux - Newbie 4 06-25-2005 02:43 PM
How to copy a selected content from a .PDF file satimis Linux - General 3 07-03-2003 04:18 AM


All times are GMT -5. The time now is 10:44 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration