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.
You had the example
This is different from
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
This User Gave Thanks to MadeInGermany For This Post:
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:
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:
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:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)