0

我的文件的内容:file.conf 是

sm key mappings
sm_key_7_value_0="/www/cgi-bin/scripts/app_web_mood_control.sh Global masteron;echo hello"
sm_key_7_value_1="/www/cgi-bin/scripts/app_web_mood_control.sh Global masteroff"
sm_key_8_value_0="/www/cgi-bin/scripts/app_web_mood_control.sh Global green"
sm_key_8_value_1="/www/cgi-bin/scripts/app_web_mood_control.sh Global away"
sm_key_9_value_0="/www/cgi-bin/scripts/app_usr_core_sendPulse.sh CR_D601 C1_OPN"
sm_key_9_value_1="/www/cgi-bin/scripts/app_usr_core_sendPulse.sh CR_D601 C1_CLS"
sm_key_A_value_0="/www/cgi-bin/scripts/app_oasis_irControl.sh IR_D03 AC_ON_"
sm_key_A_value_1="/www/cgi-bin/scripts/app_oasis_irControl.sh IR_D03 AC_OFF"
sm_key_B_value_0="/www/cgi-bin/scripts/app_usr_core_sendInstruction.sh CR_D601 OP_01N"
sm_key_B_value_1="/www/cgi-bin/scripts/app_usr_core_sendInstruction.sh CR_D601 OP_01F"
sm_key_C_value_0="/www/cgi-bin/scripts/app_usr_core_sendInstruction.sh CR_D601 OP_02N"
sm_key_C_value_1="echo 123 && $(sleep 5) && $(echo hello)"

我用过grep

 grep "/www/cgi-bin/scripts/app_web_mood_control.sh" file.conf 

grep 的输出是

sm_key_7_value_0="/www/cgi-bin/scripts/app_web_mood_control.sh Global masteron;echo hello"
sm_key_7_value_1="/www/cgi-bin/scripts/app_web_mood_control.sh Global masteroff"
sm_key_8_value_0="/www/cgi-bin/scripts/app_web_mood_control.sh Global green"
sm_key_8_value_1="/www/cgi-bin/scripts/app_web_mood_control.sh Global away"

在 grep 的这个 o/p 中,我需要访问 global masteron Global masteroff Global green Global away它并将其放入一个变量中。目的是我想访问 control.sh 之后的任何内容并将其存储在变量中如何实现这一点。

4

4 回答 4

1

让你开始的东西:

$ awk -F'[ ;"]' -f g.awk your_input
Global masteron
Global masteroff
Global green
Global away

在哪里

$ cat g.awk
/Global/ {
    print $3, $4
}

多变的:

$ a=$(awk -F'[ ;"]' -f g.awk your_input)

$ echo $a
Global masteron Global masteroff Global green Global away
于 2013-09-13T12:04:50.103 回答
0

\K在中使用grep

readarray -t found < <( grep -P -o '/www/cgi-bin/scripts/app_web_mood_control.sh \K[^;"]*' file.conf)

for curFound in "${found[@]}"; do
    echo "$curFound"
done

输出:

全球masteron
全球masteroff
全球green
全球远

于 2013-09-13T13:18:18.337 回答
0
var=$(grep -Po '(?<=/www/cgi-bin/scripts/app_web_mood_control\.sh )[^;"]*' file.conf)

测试:

kent$  a=$(grep -Po '(?<=/www/cgi-bin/scripts/app_web_mood_control\.sh )[^;"]*' f)

kent$  echo $a
Global masteron
Global masteroff
Global green
Global away
于 2013-09-13T12:04:16.430 回答
0

您可以使用readarraysed

#!/bin/bash

FILE='file.conf'

# Get values.
readarray -t MATCHES < <(sed -n '/app_web_mood_control.sh/{ s|.*app_web_mood_control.sh ||; s|;.*$||; s|\"||; p; }' "$FILE")

# Demonstrate the values.
for A in "${MATCHES[@]}"; do
    echo "$A"
done

示例输出:

Global masteron
Global masteroff
Global green
Global away

保存在数组中的所有值都MATCHES从索引 0 开始,因此您可以通过${MATCHES[0]}${MATCHES[1]}等访问它们。

于 2013-09-13T12:07:13.503 回答