Shell c shell help needed.

Hi, I am working on writing a c shell script. It's this the 3rd script I would be writing. I am fairly new to c shell but not new to programming.

I need help to figure out how to use regular expressions in c shell. I am used to doing it in C++ and PHP. /.+[a-z]/ etc I need to know how it would work in c shell.

I know .+ means 1 or more matches of any character. I want to know if this is possible in c shell and is it the same as used in PHP / perl etc.

I need to grab an ip out of a file. The file will contain 2 ip addresses ip1 < ip2 :timestamps:
I want to grab ip2 and need to match < cuz the ip after this and is what I need to grab. However, don't want to grab the character <. I want to exclude it from returning with the matches made.
 
A: Using (t)csh for programming is generally considered a bad idea. Whether it is appropriate as an interactive shell is a question of some debate, for for scripts, the various sh and Korn derived shells are a better idea.

B: Why is this a shell question? Why don't you just use any of the standard utilities, like sed and awk? With a little bit of reading of the sed manual, I'm sure you can come up with a sed expression that takes "ip < ip2 :timestamps" and throws away everything after the ">" and before the ":". It's even easier in awk: Run the data into awk, perhaps changing the IFS (input field separator) to include ">" and ":", and then pick out the correct columns.

Using the shell itself to do heavy lifting is a very difficult way to make progress. Use appropriate small tools. Here is something I just hacked up in sed:
Code:
# echo "ip1 < ip2 :timestamps" | sed "s/.* < \(.*\) :.*/\1/"
ip2

See how that regular expression in sed works? Match anything, followed by space < space, then start saving the good stuff (opening parenthesis), then anything and stop saving, look for a blank and a colon, and then the rest. Having matched that, replace it with just the content of the first pair of parentheses (throwing away the first and last part), done. Don't know whether this exact expression will work for your task; some tweaking probably required.
 
Back
Top