-1

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

study
  • 1
  • 5

1 Answers1

0

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.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • Hello, after I found that after using this method, echo "$ {now_list_title}" will change the number, as long as all the values after output this variable will be abnormal. – study Nov 09 '22 at 05:53
  • set -x echo "${now_list_url}||${_site_dns_com_cnt}" echo "${now_list_url}||${now_list_keyword}||${now_list_title}||Test character" ———————————————————————————————————— result ||Test character'om||keyword||goodkey - good2site ||Test characterrd||goodkey - good2site – study Nov 09 '22 at 05:59
  • @study That sounds like the input file is in DOS/Windows format, and has a carriage return at the end of each line (in addition to the newline that `read` expects). You can tell `read` to discard it with `while IFS=$IFS$'\r' read -r now_list_url now_list_keyword now_list_title; do`. See ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) for more information and options. – Gordon Davisson Nov 09 '22 at 06:08
  • In the current method, use "IFS = $ ifs $ '\ r'". get_text now_list_title But I use the GET_TEXT method to use the Now_list_title variable value. After the get_text method, the now_list_tital variable value will still change the line. – study Nov 09 '22 at 06:29
  • No, I added "IFS = $ iFS $ '\ r'" and I still change it. I will send you the result to see – study Nov 09 '22 at 06:39
  • echo "${now_list_url}||${now_list_keyword}||${now_list_title}||ce1" ———————————————————————————— result ||ce1.com||godkeyword||godkeyword - godtext – study Nov 09 '22 at 06:40
  • jnpxw.com godkeyword godkeyword - godtext _____ These are three corresponding variable values – study Nov 09 '22 at 06:45
  • That's definitely a carriage return getting in there. Is this running in bash, or some other shell? Other shells may not understand the `$'\r'`. Also, make sure the capitalization and spacing are correct; they're wrong in what you just said. Use back-quotes to quote code and literal strings in your comments so they won't be weirdly formatted. – Gordon Davisson Nov 09 '22 at 07:05
  • I feel file coding problem – study Nov 09 '22 at 07:27