y.bregey
y.bregey

Reputation: 1499

Put strings from text to different variables in bash

There is a textfile in which the 1st line is link, the 2nd is username, and the 3rd is gallery:

http://sitename.com/galleries/1
1-username
gallery 1
http://sitename.com/galleries/2
2-username
gallery 2
http://sitename.com/galleries/3
1-username
gallery 3
http://sitename.com/galleries/4
2-username
gallery 4
http://sitename.com/galleries/5
3-username
gallery 5

For each link need to curl it, grep some image's links and download them using wget to folder named like gallery inside the folder named like username. For example, need to have such folder's structure:

\1-username\gallery 1
\2-username\gallery 2
\1-username\gallery 3
\2-username\gallery 4
\3-username\gallery 5

frame of code (used 2 backquotes for sed output variable):

| curl $link | egrep 'PATTERN' | for image in ``sed -e "s/PATTERN/\\1/"`` do wget $image -P "\\$username\\$gallery";done

How to set different variables for strings in cylce while reading such textfile?

Upvotes: 2

Views: 56

Answers (2)

ShaneQful
ShaneQful

Reputation: 2236

You can use the mod operator and a counter to read the file like so in order to get the right variables e.g.

C=0
while read line; do
    if (( (C % 3) == 0 )); then 
        link=$line
    elif (( (C % 3) == 1 )); then
        username=$line
    else
        gallery=$line
        #Do what you want with the varible here
        echo "$link - $username - $gallery"
    fi
    C=$((C+1)) #Counter
done < file.txt

Explanation:

  • Set counter to zero
  • Loop through each line in the file & If the counter mod 3 is 0, set link variable to the current line
  • if the counter mod 3 is 1, set username variable to the current line
  • Other wise(ie. if the counter mod 3 is 2), set the gallery variable to the current line and execute your code with the three variables.
  • Increment the counter for the next iteration.

I'm just echoing the variables, the output of which is below but you can run your code there instead.

http://sitename.com/galleries/1 - 1-username - gallery 1
http://sitename.com/galleries/2 - 2-username - gallery 2
http://sitename.com/galleries/3 - 1-username - gallery 3
http://sitename.com/galleries/4 - 2-username - gallery 4
http://sitename.com/galleries/5 - 3-username - gallery 5

Upvotes: 1

anubhava
anubhava

Reputation: 785196

Using awk:

awk '/http/{p=NR;next} NR==p+1{u=$0;next} NR==p+2{print "\\" u "\\" $0; p=""}' file
\1-username\gallery 1
\2-username\gallery 2
\1-username\gallery 3
\2-username\gallery 4
\3-username\gallery 5

Upvotes: 2

Related Questions