image
image

Go Back   macosx.com > Community > Bob's Place

Reply
 
Thread Tools
  #1  
Old November 15th, 2002, 03:12 PM
michaelsanford's Avatar
Psycholinguist
 
Join Date: Oct 2002
Location: Ottawa/Montrιal
Posts: 2,175
Thanks: 0
Thanked 1 Time in 1 Post
michaelsanford is on a distinguished road
Shell script to rip form variable names to a file?

I have a rather large for that I want to get the variable names from, so that I can quickly make a PHP page without having to recopy them all by hand.

I've gotten this far
cat ncf.html | grep -e '<input' | grep -e 'name='

But this just gives me the full lines of every instance of those variable names (i.e., each form element). I can't figure out how to make the script get the part after name= (so XXX, the variable name) and parse that into a file (with a simple redirection operator).

Any ideas?
__________________
michaelsanford.com • Blog • Twitter • Tumblr • LinkedIn
• iMac Aluminum 24" |
MacOS X 10.5-current | 3.06 GHz Intel Core Duo | 4 GB RAM | 1 TB HDD
• iBook G4 1.42 GHz | MacOS X 10.5-current | 1 GB RAM, 100 GB HDD
• AMD Athlon64 3500+ | Slackware 12 (2.6.21.5-smp) | 2 GB RAM, 2•120 GB RAID 1, 2•500 GB RAID 0
Reply With Quote
  #2  
Old November 15th, 2002, 07:09 PM
Prescriptivist
 
Join Date: Sep 2000
Location: Behind you! Duck!
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
onan is on a distinguished road
You started with:

cat ncf.html | grep -e '<input' | grep -e 'name='

Let me start off by pointing out a few details about what you have here already.

Firstly, you don't need the cat; just supply the source filename as the final argument to the first grep.

Secondly, you generally don't need to use -e. The first non-option thing you supply to grep is assumed to be the match pattern.

Lastly, you could choose to do this with a single grep, looking for both of these strings on a single line, rather than with two. Using the -E option allows you to use full regular expressions, not just simple strings.

So with all that, what you have so far would have:

grep -E '\<input.*name\=' ncf.html


(Note that that doesn't actually get you anything that you didn't already have, but it's slightly better form, and it never seems to hurt to develop good habits about such things.)


Okay, so now on to actually answering your question.

The next tool you want is probably sed. sed's most common usage is to do substitutions on text that it's fed; it also can speak regular expressions, and can obviously substitute nothing for strings, thus deleting them. A simple example of a common way to use sed is:

sed 's/search/replace/'

So if you want to delete everything up through name=", and then delete everything from " onward, you could do something like:

grep -E '\<input.*name\=' ncf.html | sed 's/.*name\=\"//' | sed 's/\".*//'


Sorry for rambling on at such length, and hope this helps you out.
__________________
The correct plural of forum is fora. _Please_ fix the painfully illiterate references to 'forums'.
Reply With Quote
  #3  
Old November 15th, 2002, 08:41 PM
michaelsanford's Avatar
Psycholinguist
 
Join Date: Oct 2002
Location: Ottawa/Montrιal
Posts: 2,175
Thanks: 0
Thanked 1 Time in 1 Post
michaelsanford is on a distinguished road
Awesome!

Thanks a lot!
__________________
michaelsanford.com • Blog • Twitter • Tumblr • LinkedIn
• iMac Aluminum 24" |
MacOS X 10.5-current | 3.06 GHz Intel Core Duo | 4 GB RAM | 1 TB HDD
• iBook G4 1.42 GHz | MacOS X 10.5-current | 1 GB RAM, 100 GB HDD
• AMD Athlon64 3500+ | Slackware 12 (2.6.21.5-smp) | 2 GB RAM, 2•120 GB RAID 1, 2•500 GB RAID 0
Reply With Quote
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[HOWTO] - Use niutil gatorparrots HOWTO & FAQs 6 December 10th, 2002 04:24 PM
I installed Fink under root and..... Hydroglow Unix & X11 5 November 27th, 2002 04:57 PM
AppleScript front end to a shell script? meancode Mac OS X System & Mac Software 0 March 17th, 2002 09:25 PM
changing multiple file names karavite Mac OS X System & Mac Software 5 January 6th, 2002 11:55 AM
vignette client on mac os x erim Software Programming & Web Scripting 8 July 13th, 2001 02:14 PM


All times are GMT -5. The time now is 04:47 AM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.