2

I'm new to expect scripting, trying to implement below command (that works otherwise) in an expect script

$ grep -i cps config.cfg | grep -v "//cps" | grep -v "cps := 4.1" | \
    grep -v "cps := 2.2" | grep -v "cps := 5.1" | grep -v "cps := 7.4"| \
    awk '{ SUM += $3} END { print SUM}'

The command summarizes the total cps call per seconds in a config file without counting with certain values like 4.1, 2.2, 5.1 and 7.4.

Because of the long command I was thinking about declaring the command in parts but I got a syntax error when executing the script:

set cpscmd1 "grep -i cps traffic.cfg"
set cpscmd2 "| grep -v "//cps" | grep -v "cps := 4.1" | grep -v "cps := 2.2"
set cpscmd3 "| grep -v "cps := 5.1" | grep -v "cps := 7.4"| \
    awk '{ SUM += $3} END { print SUM}''"
...

exp_send "$cpscmd1 $cpscmd2 $cpscmd3 ";exp_send\r

Could you please give me some tips about how to do this in a proper way?

1

You don't say what exact syntax error you get, but it looks like you primarily have an issue with escaping of quotation marks. For example, with cpscmd2 having all those double-quotes, what should be the start and end quotes, and which quotes should be considered part of the command itself? The way to pass quotes as part of the command is to escape them with \:

set cpscmd2 "| grep -v \"//cps\" | grep -v \"cps := 4.1\" | grep -v \"cps := 2.2\" "

(You also were missing the close-quote at the end.) Alternatively, since you are grepping for literal strings, you could use single-quotes for those, making it a bit more readable:

set cpscmd2 "| grep -v '//cps' | grep -v 'cps := 4.1' | grep -v 'cps := 2.2' "

You will also need to escape the $ sign in cpscmd3. Your final \r should be quoted as well.

But I would recommend letting the awk command handle the values you don't want counted, by specifically omitting them from the SUM. This would then end up as:

set cpscmd1 "grep -i 'cps' traffic.cfg | grep -v '//cps' "
set cpscmd2 "| awk '\$3 !~ /4.1|2.2|5.1|7.4/ { SUM += \$3} END { print SUM}' "
...

exp_send "$cpscmd1 $cpscmd2 \r"
  • Hi Lars, thanks a lot for taking your time to answer and explain what was wrong! Those changes worked like a charm ! – Julius Oct 31 '14 at 8:15
  • Whould you like to use one command only? awk '/cps/ && !/\/\/cps|4.1|2.2|5.1|7.4/{ SUM += \$3} END { print SUM}' traffic.cfg – Costas Oct 31 '14 at 9:52
  • Hi Costas, thanks for your replay. It works when executing the command itself directly on Linux: linux1:~/load/$ awk '/cps/ && !/\/\/cps|4.1|2.2|5.1|7.4/{ SUM += \$3} END { print SUM}' traffic.cfg 32.4982 – Julius Oct 31 '14 at 10:42
  • But there is syntax error when running in a script: set cpscmd1 "awk '/cps/ && !/\/\/cps|4.1|2.2|5.1|7.4/{ SUM += \$3} END { print SUM}'" exp_send "$cpscmd1 \r" Result: linux1:~/load/$ awk '/cps/ && !///cps|4.1|2.2|5.1|7.4/{ SUM += $3} END { print SUM}' traffic.cfg awk: /cps/ && !///cps|4.1|2.2|5.1|7.4/ { SUM += $3} END { print SUM} awk: ^ syntax error linux1:~/load/$ – Julius Oct 31 '14 at 10:43
  • More escapes needed there, for the new backslashes -- double them up: awk '/cps/ && !/\\/\\/cps|4.1|2.2|5.1|7.4/{ SUM += \$3} END { print SUM}' traffic.cfg Note the slight meaning change here, that awk will avoid a line with "5.1" anywhere, not just third argument -- but that should be fine with your input, I presume. – Lars Rohrbach Oct 31 '14 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.