Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash to search file based off user input then create new file
# 1  
Old 04-30-2015
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 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 Smilie.

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


Last edited by cmccabe; 04-30-2015 at 02:47 PM.. Reason: modified code
# 2  
Old 05-01-2015
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:

Code:
result=`grep "${id}" getCSV.txt`
if [ -n "$result" ]; then
  echo "$result yes" >> match.txt
else
  echo "no"
fi

# 3  
Old 05-01-2015
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 Smilie.

Maybe:
Code:
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

# 4  
Old 05-01-2015
Using the -n option to grep will do the trick:

Code:
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

# 5  
Old 05-01-2015
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 Smilie.

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

# 6  
Old 05-01-2015
Breaking down the result line again will work:
Code:
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:

Code:
grep -n "${id}" getCSV.txt|while read $line; do
  echo "match found in line $line"|sed 's/:.*//'
  echo "$line"|sed 's/[^:]*://' >> match.txt
done


Previous Thread | Next Thread
Test Your Knowledge in Computers #1011
Difficulty: Easy
The number 128 in base 10 equals 10000000 in base 2.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automaticaly create function based off user input

I am trying to create a bash script that will create new function by using the user input. The below will create the necessary files in the correct format, however when it comes to the # create function I am at a loss. If the name entered was NEWNAME and the genes were GENE1,GENE2 then two files... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk command to search based on 5 user input fields

Field1=”” Field2=”” Field3=”” Field4=”” Field5=”” USER INPUT UP TO 5 FIELDS awk -F , '{ if ( $3 == Field1 && $6 == Field2 && $8 == Field3 && $9 == Field4 && $10 == Field5) print $0 }' /tmp/rodney.outD INPUT FILE (Rodney.outD): ... (3 Replies)
Discussion started by: rmerrird
3 Replies

3. Shell Programming and Scripting

How to create file and file content based existing information?

Hi Gurus, I am SQL developer and new unix user. I need to create some file and file content based on information in two files. I have one file contains basic information below file1 and another exception file file2. the rule is if "zone' and "cd" in file1 exists in file2, then file name is... (13 Replies)
Discussion started by: Torhong
13 Replies

4. Shell Programming and Scripting

Search pattern in a file taking input from another file

Hi, Below is my requirement File1: svasjsdhvassdvasdhhgvasddhvasdhasdjhvasdjsahvasdjvdasjdvvsadjhv vdjvsdjasvdasdjbasdjbasdjhasbdasjhdbjheasbdasjdsajhbjasbjasbhddjb svfsdhgvfdshgvfsdhfvsdadhfvsajhvasjdhvsajhdvsadjvhasjhdvjhsadjahs File2: sdh hgv I need a command such that... (8 Replies)
Discussion started by: imrandec85
8 Replies

5. Shell Programming and Scripting

bash script to find date based on search string for continuesly updating file

Hi All, I am very new to UNIX and I have tried this for a longtime now and unable to crack it.... There is a file that is continuously updating. I need to search for the string and find the date @ which it updated every day..... eg: String is "work started" The log entry is as below: ... (1 Reply)
Discussion started by: Nithz
1 Replies

6. Shell Programming and Scripting

Search on date range of file based on user input

Hello I would like to ask for help with a script to search a directory that contains many log files and based on a users input after being prompted, they enter a date range down to the hour which searches the files that contain that range. I dont know how to go about this. I am hoping that the... (5 Replies)
Discussion started by: lostincashe
5 Replies

7. Shell Programming and Scripting

Append file based upon user input-- solved

Ok, I have a script with a commandline option that allows the user to add a custom function to the script file. I have tried everything in my limited knowledge of sed to get this to work and keep coming up short. I need sed to search for a line starting with a pattern, I've got that part so far,... (0 Replies)
Discussion started by: DC Slick
0 Replies

8. Shell Programming and Scripting

Grepping a file based on input from a second file

how to grep a file based on another input file File1 ashu 1 ninetwo hari qwer 6 givefour jan fghj 8 noeight mar vbmi 7 noput feb -- --- File2 noput noeight --- -- Taking the input of grep as File2, a search need to be made in File1 giving File3 as output: (7 Replies)
Discussion started by: er_ashu
7 Replies

9. Shell Programming and Scripting

Read a file and search a value in another file create third file using AWK

Hi, I have two files with the format shown below. I need to read first field(value before comma) from file 1 and search for a record in file 2 that has the same value in the field "KEY=" and write the complete record of file 2 with corresponding field 2 of the first file in to result file. ... (11 Replies)
Discussion started by: King Kalyan
11 Replies

10. Shell Programming and Scripting

search file, change existing value based on input (awk help)

I have a file (status.file) of the form: valueA 3450 valueB -20 valueC -340 valueD 48 I am tailing a data.file, and need to search and modify a value in status.file...the tail is: tail -f data.file | awk '{ print $3, ($NF - $(NF-1)) }' which will produce lines that look like this: ... (3 Replies)
Discussion started by: nortonloaf
3 Replies

Featured Tech Videos