简体   繁体   中英

Loop through txt file comma-separated and use as variable

I have txt file separated by comma:

2012,wp_fronins.pdf
2013,test789.pdf
2014,ok09report.pdf

I'm trying to extract from the file each value and pass him to CURL command with a condition before. For example:

if $value1=2012 do 
curl "https://onlinesap.org/reports/$valu1/$value2

Any idea ?

Another way to achieve is to read the file directly and cut the rows to get the elements directly.

while read p; do
    value1=`echo $p | cut -d',' -f1`
    value2=`echo $p | cut -d',' -f2`
    if [ $value1 = "2012" ]; then
        curl "https://onlinesap.org/reports/$value1/$value2"
    fi
    # Add More conditional statements here for other value1
done < filename.txt

Since the name of the pdf file (value2) is unique, you may try something like this:

#!/bin/bash

FILENAME=myFile.txt

cat $FILENAME | awk -F',' '{print $2}' | while read value2; do
  value1=`grep -w "$value2" $FILENAME | awk -F',' '{print $1}'` # watch the back-tick
  if [ $value1 = "2012" ]; then
    curl https://onlinesap.org/reports/$value1/$value2
  fi
done

Please notice that the whole file is scanned a second time for each line found.
In other words, its complexity is O(n^2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM