0

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 ?

bugnet17
  • 173
  • 1
  • 1
  • 12

2 Answers2

1

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
Amit Bhardwaj
  • 333
  • 1
  • 8
0

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)

Robert Kock
  • 5,795
  • 1
  • 12
  • 20