1

我试图让以下代码从用户那里读取变量;要搜索的文件、搜索字符串、输出所需的空格和要输出的字段数量。

第一个问题是 AWK 命令。如果我输入一个有效的空格,例如“”(单个空格)或“\t”,我会收到未终止的字符串和语法错误,只有在我请求输出多个字段时才会发生这种错误(否则不会添加空格上)。

其次,使用搜索字符串时,GREP 似乎有点挑剔。为了使用整个字符串,我必须在变量的开头和结尾添加双引号。

#!/bin/bash
#****************************************************
#Name:          reportCreator.sh
#Purpose:       Create reports from log files
#Author:        
#Date Written:      11/01/2013
#Last Updated:      11/01/2013
#****************************************************

clear

#Determine what to search for
printf "Please enter input file name(s): "
read inputFile
printf "Please enter your search query: "
read searchQuery
printf "Please enter the whitespace character: "
IFS= read whitespace
printf "Please enter the amount of fields to be displayed: "
read fieldAmount

#Add quotation marks to whitespace and searchQuery
whitespace=\""$whitespace"\"
searchQuery=\""$searchQuery"\"

#Declare variables
declare -i counter=0
declare -a fields[$fieldAmount]
declare -a fieldInsert[$fieldAmount]

#While loop for entering fields
while [[ "$counter" -ne "$fieldAmount" ]]
do
        #Ask for field numbers
        printf "Please enter number for required field $((counter+1)): "
        read fields[$counter]
        ((counter++))
done

#Create function to add '$' before every field and the whitespace characters
function fieldFunction 
{
    for (( counter=0; counter <= ($fieldAmount-1); counter++ ))
    do
        fieldInsert[$fieldAmount]="$""${fields[$counter]}"
        if (( counter!=($fieldAmount-1) ))
        then
            printf "${fieldInsert[*]}$whitespace"
        else
            printf "${fieldInsert[*]}"
        fi  
    done
}
printf "%b\n"

tac $inputFile | grep "$searchQuery" | less #| awk '{print $(fieldFunction)}'

exit 0

任何帮助,将不胜感激。

4

2 回答 2

1
  1. Grep 不理解引号,因此删除将它们添加到 $searchQuery 的行。
  2. 对 awk 使用双引号而不是单引号,因此 $(fieldFunction) 会扩展。

解决这个问题(以及取消注释 awk,当然),它可以工作:

user@host 15:00 ~ $ cat script
#!/bin/bash
#****************************************************
#Name:          reportCreator.sh
#Purpose:       Create reports from log files
#Author:        
#Date Written:      11/01/2013
#Last Updated:      11/01/2013
#****************************************************

clear

#Determine what to search for
printf "Please enter input file name(s): "
read inputFile
printf "Please enter your search query: "
read searchQuery
printf "Please enter the whitespace character: "
IFS= read whitespace
printf "Please enter the amount of fields to be displayed: "
read fieldAmount

#Add quotation marks to whitespace and searchQuery
whitespace=\""$whitespace"\"

#Declare variables
declare -i counter=0
declare -a fields[$fieldAmount]
declare -a fieldInsert[$fieldAmount]

#While loop for entering fields
while [[ "$counter" -ne "$fieldAmount" ]]
do
        #Ask for field numbers
        printf "Please enter number for required field $((counter+1)): "
        read fields[$counter]
        ((counter++))
done

#Create function to add '$' before every field and the whitespace characters
function fieldFunction
{
    for (( counter=0; counter <= ($fieldAmount-1); counter++ ))
    do
        fieldInsert[$fieldAmount]="$""${fields[$counter]}"
        if (( counter!=($fieldAmount-1) ))
        then
            printf "${fieldInsert[*]}$whitespace"
        else
            printf "${fieldInsert[*]}"
        fi
    done
}
printf "%b\n"

tac $inputFile | grep "$searchQuery" | awk "{print $(fieldFunction)}"

exit 0
user@host 15:01 ~ $ cat file
foo two three four
foo two2 three2 four2
bar two three four
user@host 15:01 ~ $ bash script
Please enter input file name(s): file
Please enter your search query: foo
Please enter the whitespace character:
Please enter the amount of fields to be displayed: 2
Please enter number for required field 1: 4
Please enter number for required field 2: 2

four2   two2
four    two
user@host 15:01 ~ $
于 2013-01-11T23:03:28.733 回答
0

我们来看看这段代码:

#Create function to add '$' before every field and the whitespace characters
function fieldFunction
{
    for (( counter=0; counter <= ($fieldAmount-1); counter++ ))
    do
        fieldInsert[$fieldAmount]="$""${fields[$counter]}"
        if (( counter!=($fieldAmount-1) ))
        then
            printf "${fieldInsert[*]}$whitespace"
        else
            printf "${fieldInsert[*]}"
        fi
    done
}
printf "%b\n"

tac $inputFile | grep "$searchQuery" | awk "{print $(fieldFunction)}"

它可以更简单、更健壮地重写为(未经测试):

tac "$inputFile" |
awk -v fieldsStr="${fields[*]}" -v searchQuery="$searchQuery" -v OFS="$whitespace" '
   BEGIN{ numFields = split(fieldsStr,fieldsArr) }
   $0 ~ searchQuery {
      for ( i=1; i <= numFields; i++ )
         printf "%s%s", (i==1?"":OFS), $(fieldsArr[i])
      print "\b"
   }
'

我不知道你为什么需要“tac”来打开文件,但我想你有你的理由,所以我把它留在了里面。

于 2013-01-12T15:27:28.243 回答