ksh Script, Reading A File, Grepping A File Contents In Another File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh Script, Reading A File, Grepping A File Contents In Another File
# 1  
Old 11-28-2018
ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped.
First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes.

I have a text file (file_source) of terms, each line could have one, two or more words in this file.
I want to use each line from file_source as a key to grep for in a different file, (file_target)
I want to run a script that reads file_source, and uses the words from each line, to search/grep in file_target.
IE: grep "one first word" file_target, or just grep "second" file_target, etc.

In my first attempted script I have run a=`cat file_source`, then in a do loop, tried to grep "$a" file_another.
But instead of looking for all the words in one line, the routine I've created looks for each word individually from the line it's reading.
So instead of grep "a b c" file, I got grep a file, grep b file, grep c file.
Which is great... but sometimes, I really need to know that a, b and c occurred as a phrase.

I've tried multiple ideas, from cat, to read, to while to screaming. (OK, screaming is not a function, just a reaction.)
I can get a while read routine to read the whole line of file_source, but then trying to use the line to grep for in file_target fails wonderfully, saying something to the effect of source not found or what not.

I've run the script in the directory of the file I want to look at, calling out the file, and also run it pointing to the full path/file, to no avail.

If anyone has quick suggestions, outlines, ideas, or examples, WITHOUT burning up too much of your time, I'd appreciate it.
Thank you.

Bruce
# 2  
Old 11-28-2018
You had the example
Code:
grep "one first word" file_target

This is different from
Code:
grep one first word file_target

where it searches the word "one" in the files "first", "word", "file_target".
So ensure you have the quotes, even if you have a variable line from a while read line
Code:
grep "$line" file_target

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 11-28-2018
Quote:
Originally Posted by Brusimm
In my first attempted script I have run a=`cat file_source`, then in a do loop, tried to grep "$a" file_another.
This is not advisable for several reasons: first - as you noticed - field splitting will occur and may bite you in the behind. Second, there is a much easier way to do this, see below.

Quote:
Originally Posted by Brusimm
I've tried multiple ideas, from cat, to read, to while to screaming. (OK, screaming is not a function, just a reaction.)
I can get a while read routine to read the whole line of file_source, but then trying to use the line to grep for in file_target fails wonderfully, saying something to the effect of source not found or what not.
Not seeing your script i can only speculate but probably the problem was quoting (or, rather, the lack thereof). You probably did:

Code:
while read line ; do
     grep $line /some/file
done < /some/file_with_phrases

Notice that you always ALWAYS have to protect your variables - you don't go outside naked, they shouldn't be made to go outside unquoted. Do it like:

Code:
while read line ; do
     grep "$line" /some/file
done < /some/file_with_phrases

But there is a much better way and it doesn't even involve a script - TADAAAAHHHH:

Quote:
Originally Posted by Brusimm
If anyone has quick suggestions, outlines, ideas, or examples, WITHOUT burning up too much of your time, I'd appreciate it.
Well, sometimes, when the time is right, i can make a little room in my busy schedule which is filled with coming up with witty formulations for heightening the suspense and avoiding to lead to a premature climax of this most interesting topic ......*)

...to come up with a single command that does it all: grep!

Do it like this:

Code:
grep -f /some/file_with/phrases /file/to/grep/in

and grep will read the file_with_phrases, line by line, then do a search in the other file for that line. Sounds like this is what you wanted, no? To find out more about how to use grep i suggest to peruse the man page. If you still have questions, you'll be welcome.

I hope this helps.

bakunin
__________
*) You might not believe it but i can prolong that for a nearly indefinite time.
# 4  
Old 11-28-2018
I was trying to be careful... but will double check MadeInGermay.... Thanks.

------ Post updated at 03:36 PM ------

hey Bakunin...
A - Are you sure I don't run around out there nak.... oh never mind.
And yes, it worked... the suspense got me.

ARE YOU KIDDING ME? Crap on a crapsicle stick! Grep -f....

Thank you for your time, oh wise and humorous one.

Bruce
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Emergency UNIX and Linux Support

sed replace file contents by reading from another file

Hello, My input file1 is like this by tab-delimited chr1 mm10_knownGene stop_codon 3216022 3216024 0.000000 - . gene_id "uc007aeu.1"; transcript_id "uc007aeu.1"; chr1 mm10_knownGene CDS 3216025 3216968 0.000000 - 2 gene_id "uc007aeu.1"; transcript_id "uc007aeu.1"; ... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. Shell Programming and Scripting

Reading the contents of the file and splitting using ksh

We're using a ksh script for installing one product. I've another config file, I'd need to read this configuration file from my main script Content of the Configuration file:... (2 Replies)
Discussion started by: bittu129
2 Replies

4. UNIX for Dummies Questions & Answers

Looping/Reading file contents not working

Hi, I am doing something basic, but I am missing something. Im trying to read the contents of a file and taking those values and connecting to a database. However, it only connect to one (or reads in) value and then exists. Here is what it looks like: listname.txt db1 db2 db3 Script:... (15 Replies)
Discussion started by: DBnixUser
15 Replies

5. Shell Programming and Scripting

Reading file contents until a keyword

Hi Guys, I need to read a file until I find a blank line. and in the next iteration I want to continue reading from the line I find a keyword. For ex: my file looks like PDS_JOB_ALIAS CRITERIA_ITEM_TYPE PDS_JOB_CRITERIA_ITEM CRITERIA_ITEM_TYPE First I want to read the file... (2 Replies)
Discussion started by: infintenumbers
2 Replies

6. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

7. Shell Programming and Scripting

Grepping a file contents into another file

I have a file named as ucid.txt It has multiple rows of "id". I need to search and grep each line of it from a file named as pw_logs.txt and put the results into another file. Please help ! Thanks. (8 Replies)
Discussion started by: gopikrish81
8 Replies

8. Shell Programming and Scripting

Reading and printing one by one contents of a file

I have a file which has following contents: localhost_IP_SIP_1233026552455.xml localhost_IP_SIP_1233026552460.xml localhost_IP_SIP_1233026552467.xml localhost_IP_SIP_1233026552759.xml localhost_IP_SIP_1233026552969.xml localhost_IP_SIP_1233026552975.xml ... (2 Replies)
Discussion started by: Aditya.Gurgaon
2 Replies

9. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies

10. Shell Programming and Scripting

Reading specific contents from a file and appending it to another file

Hi, I need to write a shell script (ksh) to read contents starting at a specific location from one file and append the contents at specific location in another file. Please find below the contents of the source file that I need to read the contents from, File 1 -----# more... (5 Replies)
Discussion started by: dnicky
5 Replies
Login or Register to Ask a Question