Sponsored Content
Top Forums Shell Programming and Scripting ksh Script, Reading A File, Grepping A File Contents In Another File Post 303026507 by bakunin on Wednesday 28th of November 2018 05:25:28 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 03:56 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy