In the below bash
a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file.
For example, the bash
is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search download.txt and the line is written to match.txt. The code runs but no output result. Eventually, I will search for specific text in the line that id was found in, but I figured getting the search based of user input was a to work is a good start. Thank you :).
#!/bin/bash
cd 'C:\Users\cmccabe\Desktop\wget'
wget -O getCSV.txt http://xxx.xx.xxx.xxx/data/getCSV.csv
{
printf "\n\n"
printf "What is the id of the NGS patient: "; read id
[ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
[ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && return
}
input=$id
while read -r line
do
case $line in
*${id}*)
echo $line " yes" >> match.txt
;;
*)
echo "no"
;;
esac
done
Your read loop doesn't seem to read from the downloaded file. Anyway I wouldn't try to emulate grep
with shell commands. Instead the loop could be replaced by the use of
grep "${id}" getCSV.txt
. For example like:
result=`grep "${id}" getCSV.txt`
if [ -n "$result" ]; then
echo "$result yes" >> match.txt
else
echo "no"
fi
After the user inputs the id and hits enter to start the search the program exits and if a match is found there is a new file match.txt. I am not sure how to keep the program open and display a message "searching, please wait" and when a match is found display the line #. Thank you :).
Maybe:
result=`grep "${id}" getCSV.txt`
if [ -n "$line" ]; then
printf "searching, please wait"
echo "match found in line$line" >> match.txt
else
echo "no match found"
fi
Using the -n
option to grep will do the trick:
printf "searching, please wait"
result=`grep -n "${id}" getCSV.txt`
if [ -n "$result" ]; then
echo "match found in line $result" >> match.txt
else
echo "no match found"
fi
When the bash
menu is opened the getCSV file is downloaded and then the menu closes. Below is the code as is and what I am trying to do is when the menu is opened the file is downloaded, the user is prompted for the id, and once that is entered it is searched for in the file. While it is being searched for on the screen "searching, please wait" is displayed and when a match is found on the screen "match found in lijne..." is displayed and a file match.txt is written. Thank you for your help :).
#!/bin/bash
cd 'C:\Users\cmccabe\Desktop\wget'
wget -O getCSV.txt http://xxx.xx.xxx.xxx/data/getCSV.csv
search() {
printf "\n\n"
printf "What is the id of the NGS patient: "; read id
[ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
[ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && return
printf "searching, please wait"
result=`grep -n "${id}" getCSV.txt`
if [ -n "$result" ]; then
echo "match found in line $result" >> match.txt
else
echo "no match found"
fi
}
Breaking down the result line again will work:
result=`grep -n "${id}" getCSV.txt`
if [ -n "$result" ]; then
echo "match found in line $result"|sed 's/:.*//'
echo "$result"|sed 's/[^:]*://' >> match.txt
else
echo "no match found"
fi
This will stop working, if more than one result line is found. If that is possible, it's probably best to remove the -n
from grep, just write the result lines into the file and omit the line number from the found message. Or do something like:
grep -n "${id}" getCSV.txt|while read $line; do
echo "match found in line $line"|sed 's/:.*//'
echo "$line"|sed 's/[^:]*://' >> match.txt
done