Display match or no match and write a text file to a directory

The below bash connects to a site, downloads a file, searches that file based of user input - could be multiple (all that seems to work). What I am not able to figure out is how to display on the screen match found or no match found" and write a file to a directory (C:\Users\cmccabe\Desktop\wget) with the match in it. If there is no match then no file needs to be created just displayed. Thank you :).

#!/bin/bash

# url download
printf "establishing connection and downloading file, please wait"
cd 'C:\Users\cmccabe\Desktop\wget'
curl -# -o getCSV.txt http://xxx.xx.xxx.xx/file/name.csv

# enter user-defined id
printf "please enter id(s), use a comma between multiple: "; IFS="," read -a id

     # check to make sure something entered
	[ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
	
# search download using entered id
echo "searching, please wait" 
result=`grep -n "${id}" getCSV.txt`
        for ((i=0; i<${#id[@]}; i++))
              do printf ${id[$i] & ${result[$i]}} >> "C:\Users\cmccabe\Desktop\wget\match.txt"
        done
   printf "match found in line $result"
else
   printf "No match found! " && sleep 2
   fi

# additional
printf "Are there additonal patients?  Y/N "; read match_choice

    case "$match_choice" in
        [yY]) id="${id}"; echo "what is the id of the NGS patient: "; read id
              [ -z "id" ] && printf "\n No ID supplied. Leaving match function." && 
			      sleep 2 && exit
		esac 

What exactly do you expect

  • grep -n "${id}" getCSV.txt (note: id is an array)
  • printf ${id[$i] & ${result[$i]}} (note: result is not an array)
    to do?

That printf 's format specifier is missing...

Where is the if for the existing fi ?

1 Like

Looks like I have a bit of work to do, what would you do? I am making progress learning programming, but am still a scientist who wants to learn. Thank you :).

Hi, cmcabe

Here's an example of what it could be. It is done on the fly and not tested or guarantee to do anything useful for you.

#!/bin/bash

#### FUNCTIONS ####

# modifications to the functionality of the program
# can be done here without changing the flow of the script

ask (){
    local IFS=','
    echo "A search id must be entered"
    echo "Multiple ids can be entered if separated by comma"
    echo "Please ENTER the id(s) now: "
    read -a id
    if [[ -z $id ]]; then
        echo "Missing search ID"
        echo "Goodbye!"
        exit 1
    fi
}

search (){
    p="no"
    # search every entry given by the user
    for i in ${id[@]}; do
        grep -n "$i" "$1" >> "$2"

        # update user at stdout
        if [[ $? -eq 0 ]]; then
            echo "ID: $i found"
            p="yes"
        else
            echo "ID: $i not found"
        fi
    done
}

clean_up (){
    # Let's not leave an empty file if not match was found
    if [[ ! -s "$1" ]]; then
        rm -f "$1"
    fi
}

bye () {
    if [[ $p == "yes" ]]; then
        echo "Please, check file $1 for results"
    fi
}

check () {
   if [[ ! -s $1 ]]; then
       echo "$1 does not exist or it is empty"
       echo "Goodbye!"
       exit 1
   fi 
}

### END OF FUNCTIONS ####

### CONSTANTS ####

# manual configuration can be done here
# without modifying the program

ROOTDIR='C:\Users\cmccabe\Desktop\wget'
MATCH="${ROOTDIR}\\match.txt"
CSV="${ROOTDIR}\\getCSV.txt"
URL="http://xxx.xx.xxx.xx/file/name.csv"

### END OF CONSTANS ####


echo "Establishing connection and downloading file..., please wait!"
 
curl "-#" "$URL" > "$CSV"
check "$CSV"
ask
search "$CSV" "$MATCH"
clean_up "$MATCH"
bye "$MATCH"
1 Like

What you don't seem to recognize is that you have a mixture of processing an array of id values along with getting a single scalar result value. And, after getting an array of id values and getting a single result value you are then using a for loop to process the values in the id array without getting result values associated with each element of the id array. That just can't work.

I don't follow the logic of what your script is trying to do, and the inconsistent indentation in your script doesn't help. This is totally untested, but might provide an idea for a way to move forward on your project. Note that (unlike Aia's suggestion and your code), this gathers one ID at a time rather than an array of IDs. Otherwise, I think this code follows the intended flow of your code more closely than Aia's suggestion. But, I may have misread the intent of your code:

#!/bin/bash
csv="getCSV.txt"
logf="C:\Users\cmccabe\Desktop\wget\match.txt"

# url download
printf "establishing connection and downloading file, please wait"

cd 'C:\Users\cmccabe\Desktop\wget'
curl -# -o "$csv" http://xxx.xx.xxx.xx/file/name.csv

# enter user-defined id
while read -r -p'please enter id (empty line or ctl-d when done): ' id
do	# check to make sure something entered
	if [ -z "$id" ]
	then 	printf '\nNo ID supplied.  '
		break
	fi

	# search download using entered id
	echo "searching, please wait" 
	result=$(grep -n "${id}" "$csv")
	if [ -z "$result" ]
	then	echo 'No match found!'
		sleep 2
		continue
	fi
	printf 'match found in line %s\n' "$result"
	printf '%s & %s\n' "$id" "$result" >> "$logf"
	# sleep 2 # Do you want to sleep here too?
done
echo 'Leaving match function.'
sleep 2
exit
1 Like