English 中文(简体)
Remove symbol in shell
原标题:
  • 时间:2009-11-22 02:14:18
  •  标签:
  • csh

I m new to shell scripting, right now using csh and let s say I have a line in my configuration file like this:

127.0.0.1:2222 127.0.0.2:3333 127.0.0.3:4444

All I want is to parse it for two variables in array :

2222 3333 4444

And another one

127.0.0.1 127.0.0.2 127.0.0.3
问题回答

Read C Shell Programming Considered Harmful .

How do you get the right line in the configuration file? I m about to assume there s just the one line in the file, but as long as you can identify the line, there are ways to do it.

set var1=`sed  s/[0-9.]*:([0-9]*)/1/g  config.file`
set var2=`sed  s/([0-9.]*):[0-9]*/1/g  config.file`

The first captures the port numbers after the dotted-decimal addresses. The second captures the addresses. In each case, the result is assigned to a string with spaces separating the values.


Some more manual bashing (on MacOS X 10.5.8 - man csh prints the tcsh page) suggests you might be after:

set arr1=(`sed  s/[0-9.]*:([0-9]*)/1/g  config.file`)
set arr2=(`sed  s/([0-9.]*):[0-9]*/1/g  config.file`)

With this notation (the parentheses around the back-quoted sed command), you create arrays, and can use:

echo $arr1[1]
echo $arr1[2]
echo $arr1[3]
echo $arr2[1]
echo $arr2[2]
echo $arr2[3]

(assuming the sample data). The part of the manual page where this is noted marks the syntax with (+) which might well mean extension to standard C shell , so whether this works for you may depend on your environment.





相关问题
ampersand at beginning of a line in csh

What does an ampersand at the beginning of a line do in csh? It seems to be ignored (with no error message), but why?

"exec source <script>" does not work in tcl

I m trying to call a script in Tcl with the command: exec source <script path> and I get the error couldn t execute "source": no such file or directory How can I call another script from tcl?...

Explain the deviousness of the Perl "preamble"

The Perl manual describes a totally devious construct that will work under any of csh, sh, or Perl, such as the following: eval (exit $?0) && eval exec perl -wS $0 ${1+"$@"} & ...

Syntax error in CShell Script

Write Script to read a positive integer number then it computes the following sequence: If the number is even, halve it If it is odd multiply it by 3 and add1 You should repeat this process until the ...

How to redirect stdout and stderr from csh script

For the following script install.csh: #!/bin/csh -f tar -zxf Python-3.1.1.tgz cd Python-3.1.1 ./configure make make install cd .. rm -rf Python-3.1.1 Intended use: ./install.csh |& tee ...

Remove symbol in shell

I m new to shell scripting, right now using csh and let s say I have a line in my configuration file like this: 127.0.0.1:2222 127.0.0.2:3333 127.0.0.3:4444 All I want is to parse it for two ...

热门标签