Bash loop to search file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash loop to search file
# 1  
Old 05-05-2015
Bash loop to search file

In the bash when the user inputs an id to search for the bash currently closes, and if a match is found outputs a new file (match.txt). Is it possible to have not close the bash but rather, on the screen "searching for match" and if a match is found "match found in line.." is displayed and written to match.txt else "no match found" is displayed on the screen and no file is written. Thank you Smilie.

The code below downloads a getCSV file then the user enters an id and that input is used to search the getCSV file. Currently, the last part is not working. I have added a loop and modified so other parts of the code with no luck.

Code:
#!/bin/bash

printf "establishing connection and downloading file, please wait"
cd 'C:\Users\cmccabe\Desktop\wget'
curl -# -o getCSV.txt http://xxx.xx.xxx.xxx/data/getCSV.csv
   
	   
 printf "Download complete, what is the id of the NGS patient: "; read id
	[ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && exit 1
  	
printf "searching, please wait" 
result=`grep -n "${id}" getCSV.txt`
while read -r line
 do
if [ -n "$result" ]; then
   echo "match found in line $result" >> match.txt
else
  echo "no match found"
 done
fi

# 2  
Old 05-06-2015
get rid of everything from while read -r line down and try something simple like :
Code:
if  grep $id getCSV.txt
then
   echo "match found in line $result" | tee -a  match.txt
else
  echo "no match found"
fi

# 3  
Old 05-07-2015
@cmccabe, Any specific reason to create a new thread ? If its similar problem, please continue here

You have received some of the nice replies and I guess few questions have been asked to you.
Creating new thread will lead to revisit the same problem and wasting of everyone's time.
Thanks.
# 4  
Old 05-07-2015
Moderator's Comments:
Mod Comment I have to agree with clx. The code shown in this thread has already been presented in the thread Bash to search file and the code presented here duplicates what was in post #5 in that thread. Questions asked concerning that post still apply. The syntax errors in the code have not been corrected.

This thread is closed.

Please answer the questions that were asked in that thread in that thread and we may be able to help you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run a loop that will search for a file to thousand machine and know who owns the file

Run a loop that will search for a file to thousand machine and know who owns the file $ for i in abc{01..02} > do > echo -n $i > ssh $i "sudo find / -name .ssh -exec ls -l {} \;|grep id" > done abc01-rw-------. 1 root root 1675 Nov 10 2018 id_rsa abc01-rw-------. 1 root root 1675 Nov 14... (6 Replies)
Discussion started by: invinzin21
6 Replies

2. Shell Programming and Scripting

Bash loop to result in one file

Is there a way to use a directory with multiple files (.bam) to create one new file? The below bash loop will create a new file (header.sam) for each of the bam files. However, I only need to use 1 of the bam file to make 1 header file. I attempted a code as well, but not sure if thats... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Bash to search file based off user input then create new file

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... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. Shell Programming and Scripting

Recursive search for string in file with Loop condition

Hi, Need some help... I want to execute sequence commands, like below test1.sh test2.sh ...etc test1.sh file will generate log file, we need to search for 'complete' string on test1.sh file, once that condition success and then it should go to test2.sh file, each .sh scripts will take... (5 Replies)
Discussion started by: rkrish123
5 Replies

5. Programming

Loop in bash file for mysql

Hi, I'm having problems with a script which some of you helped me with last week. It's a script to check the status of orders in an SQL database, and if the count(*) is zero, then the script stops. If it's non-zero, the script echos the result to a file and then in cron, I cat the file and mail... (3 Replies)
Discussion started by: davidm123SED
3 Replies

6. Shell Programming and Scripting

How to use Bash and awk to search in a file?

I have the following code as a file which a user can choose with an option to search in that file. The file name is not specified and can have any name and extension. Now a sample of this file is like this: 1,root,init,20,0.0,0.1,0:01.78 1008,root,migration/0,1,2.0,1.8,7:04.32... (4 Replies)
Discussion started by: bashily
4 Replies

7. Shell Programming and Scripting

File 1 Column 1 value search in File 2 records, then loop?

Summary: I planned on using Awk to grab a value from File 1 and search all records/fields in file 2. If there is a match in File 2, print the first column value of the record of the match of File2. Continue this search until the end of file 2. Once at the end of file 2, grab the next value in... (4 Replies)
Discussion started by: Incog
4 Replies

8. UNIX for Dummies Questions & Answers

How to search a file in bash terminal?

Hi all, I am new to unix. Anyone knows how to search a file in bash terminal, please kindly tell me. For example, I want to search file named "myfile" in the entire machine... Thanks a lot:) (2 Replies)
Discussion started by: andrewust
2 Replies

9. Shell Programming and Scripting

Search two file types within a for loop

I need to search for a csv and a dat file within a for loop then output them to a log table. I can do it individually but I want them together. See below code: check csv count=0 for file in $(ls *.csv) ; do count=`expr $count + 1` ... (4 Replies)
Discussion started by: Pablo_beezo
4 Replies

10. Shell Programming and Scripting

how to search a keyword within a file using a for loop

hi guys i have a problem here, im trying to stablish a relationship between a text file and an input user for example the script is going to prompt the user for some football team and what the script is going to do is return the colums in which that input is located so far this is what i have ... (6 Replies)
Discussion started by: lucho_1
6 Replies
Login or Register to Ask a Question