返回其他文件中字符串后的其余行

返回其他文件中字符串后的其余行

main.sh文件:

function checkSettings() {
  setting="$1 : " #| sed -e "s/\b\(.\)/\u\1/g"
  # awk -F '${setting}' '{print $2}' settings.tropx
  # sed 's/.*${setting} //' settings.tropx
  # sed -nr '/MOM:/ s/.*MOM:([^"]+).*/\1/p' settings.tropx
}
checkSettings "Debug Mode"
sleep 100

这三个选项都无法从这个文件中返回我想要的内容

settings.tropx文件:

Debug Mode : (OFF) | ON
Animations : (ALL) | SOME | NONE
Default Scripts : (SHOW) | HIDE
MOM : Yes

我希望函数能给出类似的结果,(ALL) | SOME | NONE如果我传递参数Animations

答案1

你可以使用awk

#!/bin/bash

function checkSettings() {
   awk -F ' *: *' -v arg="$1" '$1 == arg {print $2}' settings.tropx
}

checkSettings 'Debug Mode'

答案2

使用grepcut(读作man grep cut):

function checksettings() {
  grep "^$1 :" settings.tropx | \
    cut "-d:" -f2- | \
      cut "-d " -f2-
}
# use like this ($1 is the string)
rightside="$(checksettings "$1")"
# Beware! What happens if you pass "Aminations" (a typo)?

相关内容