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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Display match or no match and write a text file to a directory
# 1  
Old 01-08-2016
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 Smilie.

Code:
#!/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


Last edited by cmccabe; 01-08-2016 at 04:17 PM.. Reason: added details
# 2  
Old 01-09-2016
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 ?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 01-09-2016
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 Smilie.
# 4  
Old 01-10-2016
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.


Code:
#!/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"


Last edited by Aia; 01-10-2016 at 03:28 AM..
This User Gave Thanks to Aia For This Post:
# 5  
Old 01-10-2016
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:
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

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Insert text after match in XML file

Having a little trouble getting this to work just right. I have xml files that i want to split some data. I have 2 <name> tags within the file I would like to take only the first tag and split the data. tag example. From this. TAB<Name>smith, john</Name> to TAB<Name>smith,... (8 Replies)
Discussion started by: whegra
8 Replies

3. Shell Programming and Scripting

Bash to add portion of text to files in directory using numerical match

In the below bash I am trying to rename eachof the 3 text files in /home/cmccabe/Desktop/percent by matching the numerical portion of each file to lines 3,4, or 5 in /home/cmccabe/Desktop/analysis.txt. There will always be a match between the files. When a match is found each text file in... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Shell Programming and Scripting

Match pattern1 in file, match pattern2, substitute value1 in line

not getting anywhere with this an xml file contains multiple clients set up with same tags, different values. I need to parse the file for client foo, and change the value of tag "64bit" from false to true. cat clients.xml <Client type"FIX"> <ClientName>foo</ClientName>... (3 Replies)
Discussion started by: jack.bauer
3 Replies

6. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

7. Shell Programming and Scripting

fuzzy sequence match in a text file

Hi Forum: I have struggle with it and decide to use my eye ball to accomplish this. Basically I am looking for sequence of date inside a file. If one of the sequence repeat 2-3 time or skip once; it's still consider a match. input text file: Sep 6 A Sep 6 A Sep 10 A Sep 7 B Sep 8... (7 Replies)
Discussion started by: chirish
7 Replies

8. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

9. Shell Programming and Scripting

Need Help - match file name and copy to Directory

I am trying to sort the following files from folder Bag to Apple, Cat Food, Dog Food. I can get all of the files I want into a new folder, but not sure of the best approch to get them to their final directory My Files ========== apple.1234.ext apple.1235.ext cat food 101.ext Cat Food... (2 Replies)
Discussion started by: mtschroeder
2 Replies

10. Shell Programming and Scripting

match text from two files and write to a third file

Hi all I have two files X.txt and Y.txt. Both file contains same number of sentences. The content of X.txt is The filter described above may be combined. and the content of Y.txt is The filter describ+ed above may be combin+ed. Some of the words are separated with "+"... (2 Replies)
Discussion started by: my_Perl
2 Replies
Login or Register to Ask a Question