Bash: Replace array value with curl result

I have a text file with a name raw.txtwith something like the following:

T DOTTY CRONO 52/50 53/40 54/30 55/20 RESNO NETKI
U CYMON DENDU 51/50 52/40 53/30 54/20 DOGAL BEXET
V YQX KOBEV 50/50 51/40 52/30 53/20 MALOT GISTI
W VIXUN LOGSU 49/50 50/40 51/30 52/20 LIMRI XETBO
X YYT NOVEP 48/50 49/40 50/30 51/20 DINIM ELSOX
Y DOVEY 42/60 44/50 47/40 49/30 50/20 SOMAX ATSUR
Z SOORY 43/50 46/40 48/30 49/20 BEDRA NERTU
A DINIM 51/20 52/30 50/40 47/50 RONPO COLOR
B SOMAX 50/20 51/30 49/40 46/50 URTAK BANCS
C BEDRA 49/20 50/30 48/40 45/50 VODOR RAFIN
D ETIKI 48/15 48/20 49/30 47/40 44/50 BOBTU JAROM
E 46/40 43/50 42/60 DOVEY
F 45/40 42/50 41/60 JOBOC
G 43/40 41/50 40/60 SLATN

I read it into an array:

while read line; do
    set $line
    IFS=' ' read -a array <<< "$line"
done < raw.txt

I am trying to replace all occurrences [A-Z]{5}with a result curlwhere the correspondence [A-Z]{5}is served as a variable in the call curl.

The first match should be DOTTY. The call looks similar curl -s http://example.com/api_call/DOTTY, and the result looks like -55.5833 50.6333which should replace DOTTYin an array.

So far I could not correctly match the desired line and filed a match in curl.

Your help is greatly appreciated.

All the best, Chris

EDIT:

Decision

@Kevin @Floris . . ! .

#!/bin/bash
while read line; do
    set $line
    IFS=' ' read -a array <<< "$line"
    i=0
    for str in ${array[@]}; do
        if [[ "$str" =~ [A-Z]{5} ]]; then
            curl_tmp=$(curl -s http://example.com/api_call/$str)
            # cut off line break
            curl=${curl_tmp/$'\r'}
            # insert at given index
            declare array[$i]="$curl"
        fi
        let i++
    done
    # write to file
    for index in "${array[@]}"; do
        echo $index 
    done >> $WORK_DIR/nats.txt
done < raw.txt
+4
4

script, , , :

#!/bin/bash
while read line; do
        set $line
        IFS=' ' read -a array <<< "$line"
        for str in ${array[@]}; do
                if [[ "$str" =~ [A-Z]{5} ]]; then
                        echo curl "http://example.com/api_call/$str"
                fi
        done
done < raw.txt

EDIT: URL, URI. , , , do_something "$ (curl...)"

EDIT2: bash, , :

bash, , , - , .

- , , . shift . , tmp curl- , .

removed temporarily to avoid confusion

3: . . .

EDIT4:

#!/bin/bash
while read line; do
        set $line
        IFS=' ' read -a array <<< "$line"
        i=0
        # echo ${array[@]} below is just so you can see it before processing.  You can remove this
        echo "Array before processing: ${array[@]}"
        for str in ${array[@]}; do
                if [[ "$str" =~ [A-Z]{5} ]]; then
                        # replace the echo command below with your curl command
                        # ie - curl="$(curl http://example.com/api_call/$str)"
                        curl="$(echo 1234 -1234)"
                        if [[ "$flag" = "1" ]]; then
                                array=( ${adjustedArray[@]} )
                                push=$(( $push + 2 ));
                                let i++
                        else
                                push=1
                        fi
                        adjustedArray=( ${array[@]:0:$i} ${curl[@]} ${array[@]:$(( $i + $push)):${#array[@]}} )
                        #echo "DEBUG adjustedArray in loop: ${adjustedArray[@]}"
                        flag=1;
                fi
                let i++
        done
        unset flag
        echo "final: ${adjustedArray[@]}"
        # do further processing here
done < raw.txt

, , , bash, - . , , - .

, ,

ps - , , script , . Perl, php python

+2

cmatch:

#!/bin/bash

while read line
do
  echo $line
  a=`echo $line | egrep -o '\b[A-Z]{5}\b'`
  for v in $a
  do
   echo "doing curl to replace $v in $line"
   r=`curl -s http://example.com/api_call/$v`
   r1=`echo $r | xargs echo`
   line=`echo $line | sed 's/'$v'/'$r1'/'`
  done
done

chmod 755 cmatch
./cmatch < inputfile.txt > outputfile.txt

,

:

  • \b [A-Z]{5} , ABCDEFG ( ) .
  • egrep -o
  • ,
  • , curl
  • , curl

edit . script , ...

curl ( ), , script, newlines ( , ):

echo $r | xargs echo

echo . .

+2

:

sed?

sed "s/\([A-Z]\{5\}\)/$(echo curl http:\\/\\/example.com\\/api_call\\/\\1)/g" /tmp/raw.txt

, . 100%,

EDIT: , , , .

+1
#!/bin/bash


while read line;do
  set -- $line
  echo "second parm is $2"
  echo "do your curl here"
 done < afile.txt
0

All Articles