LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 10-02-2007, 05:32 PM   #1
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Rep: Reputation: 57
awk: get lines starting from String1 and finish String2


Hi, I'd like an easy way for getting lines starting from String1 and finish String2 with awk

Code:
cat myfile | awk '  {

bla ... 

 split($0,vk,string1) ; print vk[2] 

... bla 
 } '
I can but it ll be quite complicate... I am sure there is easy way since awk is very powerful
 
Old 10-02-2007, 06:15 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If you want to include the entire lines in the range, simply precede the command with /<pattern1>/,/<pattern2>/.

example:
Code:
awk '/gdm/,/lp/{ print $0 }' /etc/passwd
gdm:x:105:108:Gnome Display Manager daemon:/var/lib/gdm:/bin/false
haldaemon:x:101:102:User for haldaemon:/var/run/hal:/bin/false
icecream:x:102:104:Icecream Daemon:/var/cache/icecream:/bin/false
lp:x:4:7:Printing daemon:/var/spool/lpd:/bin/bash
You can also use sed:
Code:
sed -n '/gdm/,/lp/p' /etc/passwd
 
Old 10-02-2007, 10:31 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
shorter version of awk
Code:
awk '/pattern1/,/pattern2/' file

Last edited by ghostdog74; 10-02-2007 at 10:32 PM.
 
Old 10-04-2007, 01:05 AM   #4
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Original Poster
Rep: Reputation: 57
Thanks
actually , I should have explained more
I do:

Code:
cat index.htm | grep http:// | awk '  blabla'
and Id like that teh awk do return
Code:
http://linklinklink
:
from
Code:
blablaaref="http://linklinklink"blablabal
so that I can have an output:
Code:
http://linklinklink
http://linklinklink
http://linklinklink
http://linklinklink
..
 
Old 10-04-2007, 03:22 AM   #5
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Post a sample from your file.
 
Old 10-04-2007, 09:12 AM   #6
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
You need to give us an actual sample. But from what I'm understanding you want something like this.
Code:
$ cat file
blablaaref="http://linklinklink"blablabal

$ sed -n 's/.*blablaaref="\(.*\)"blablabal.*/\1/p' file
http://linklinklink
sed -n 's/.*pattern1\(.*\)pattern2.*/\1/p' file

this won't work if you have multiple urls on one line..
so yea, post a sample...
 
Old 10-05-2007, 07:35 AM   #7
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Original Poster
Rep: Reputation: 57
Quote:
Originally Posted by angrybanana View Post
You need to give us an actual sample. But from what I'm understanding you want something like this.
Code:
$ cat file
blablaaref="http://linklinklink"blablabal

$ sed -n 's/.*blablaaref="\(.*\)"blablabal.*/\1/p' file
http://linklinklink
sed -n 's/.*pattern1\(.*\)pattern2.*/\1/p' file

this won't work if you have multiple urls on one line..
so yea, post a sample...
Code:
$ sed -n 's/.*blablaaref="\(.*\)"blablabal.*/\1/p' file
http://linklinklink
Exactly !!

By chance would you have that with awk, I am awk fan now. It's so great tool, C way and vb way ...

thanks !!
 
Old 10-05-2007, 07:58 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
# awk -F '"' '{print $2}' file
http://linklinklink
 
Old 10-28-2007, 04:56 PM   #9
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Original Poster
Rep: Reputation: 57
If I do this, I do not get all the files to download, then I ll get to look for each nunber to download separately ...lot of work

and this : "[","]"

Code:
 elinks mysite.htm  | grep  ZIP | awk '  "[","]"         {     print $0      }  '
is not working

sorry, I am learning
but awk is super cool !: http://www.grymoire.com/Unix/Awk.html
 
Old 10-29-2007, 08:16 AM   #10
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Not sure what you want exactly. Can you give an example of the input data and what you want for an output.
 
  


Reply



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
Delete lines using awk kkjegan Programming 13 09-11-2007 07:36 PM
Shell: string1 += string2. How to do this? jhwilliams Linux - Software 6 06-19-2007 01:49 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
Search and Replace a string1 with string2 in many files TroelsSmit Linux - Newbie 5 12-16-2004 01:04 PM
awk text that is on several lines homey Programming 2 10-31-2004 09:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:57 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration