Home > Net >  bash shell script transmission variable value space processing, how to space data it
bash shell script transmission variable value space processing, how to space data it

Time:11-08

bash shell script transmission variable value space processing, how to space data it

**#FileList. list.txt Delivery list belowt**
  $1          $2           $3
xxxx1.com_1 Hello_2 Hello_3 - Hello
xxxx2.com_1 Hello_2 Hello_3 - Hello


get_list() {
now_list=${1} #Pass list

while read -r line; do #Circular reading
now_list_url=$(echo "${line}"|awk -F ' ' '{print $1}') #url variable1
now_list_keyword=$(echo "${line}"|awk -F ' ' '{print $2}') #keyword variable2
now_list_title=$(echo "${line}"|awk -F ' ' '{print $3}') #title variable3

#print
echo "url:${now_list_url}"
result:xxxx1.com_1

echo "keyword:${now_list_keyword}"
result:Hello_2

echo "title:${now_list_title}"

result:Hello_3    #Due to the empty grid of the transmission variable 3
result:Hello_3 - Hello    #And want to be correct
done < "${now_list}"
}

#Run
get_list list.txt`

Transfer variable 3 due to space errors:Hello_3 I want correct transmission variables 3 due to space and finally the correct result:Hello_3 - Hello

#And here is because the transmission variable is incomplete,The result I finally need is the value of the value of the output complete variable 3 in the value of the complete variable3 = "Hello_3 - hello" Because of work needs, a lot of list processing, I will save the variable value in the text How should I deal with it

thanks

CodePudding user response:

If I understand what you're trying to do, you should let read split the fields, instead of using awk:

while read -r now_list_url now_list_keyword now_list_title; do

    echo "url:${now_list_url}"
    # result: xxxx1.com_1

    echo "keyword:${now_list_keyword}"
    # result: Hello_2

    echo "title:${now_list_title}"
    # result: Hello_3 - Hello

done < "${now_list}"

Since three variables were given to read, it'll try to split the line into three fields. When there are more than three space-separated things in the line, all the extra gets put in the last variable. See BashFAQ #1: "How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?" for more information.

  • Related