Search a string in a file which is also present in another file in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search a string in a file which is also present in another file in UNIX
# 1  
Old 07-22-2016
Search a string in a file which is also present in another file in UNIX

Hi there,

I am new to Unix and had below requirement to finish my task.

I have file1.dat which has data as shown below.

Code:
case1.txt
case2.txt
case3.txt
case4.txt

file1.dat has only file names

I have folder which has above files mentioned in file1.dat

Code:
./all_files
case1.txt
case2.txt
case3.txt
case4.txt

data in case1.txt, case2.txt, case3.txt:

Code:
step1
step2
step3
step4

Now, I have to take particular step say step2 from each file, which is also present in file1.dat

Note: step2 is not line no:2 always.


Moderator's Comments:
Mod Comment Use code tags, thanks.
# 2  
Old 07-22-2016
Is this homework and if no, what have you tried?
# 3  
Old 07-22-2016
Hi zaxxon,

thanks for your tips on formatting.

I didn't tried as i am new to unix.

I am just familiar with the commands, but not the scripting.

Thanks for understanding.
# 4  
Old 07-22-2016
Unfortunately, you don't mention the shell you use. With e.g. bash, try
Code:
grep step2 $(<file1.dat)

.
# 5  
Old 07-22-2016
sorry RudiC, forgot to mention yes its bash shell.

file1.dat has only file names.

where as the files(case1.txt, case2.txt, case3.txt) exists in ./all_files folder
lets say file1.dat is also in same folder

ex: the step2 in case1.txt should display if the file name 'case1.txt' is present in file1.dat

Please help
# 6  
Old 07-22-2016
What's the result of the proposal in post#4 if run in directory all_files?
# 7  
Old 07-22-2016
the expected result will be..

step2 from all files (case1.txt, case2.txt, case3.txt), because the name(case1.txt, case2.txt, case3.txt) exists in file1.dat
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. UNIX for Beginners Questions & Answers

UNIX command to ignore replacing a search string if it is already present

Hello, I am searching the following string Folder^ in a file and replacing it with Folder^/ However if the file already contains Folder^/ I want to avoid replacing it with Folder^// To do this I have to do the following today: 1) echo "Folder^" | sed 's/Folder\^/Folder\^\//g' I get... (2 Replies)
Discussion started by: mehimadri12
2 Replies

3. Shell Programming and Scripting

UNIX Scripting help to input string and search a file to find

Hi Don, this is not homework question. I work for a Credit card company and my development goal this year is to learn Unix. I would love if others can help me get started, thanks. Hi everyone I am new to Unix and need help writing a script that can ask user for an input, then search that input... (2 Replies)
Discussion started by: 12ic11
2 Replies

4. Shell Programming and Scripting

UNIX Scripting help to input string and search a file to find

Hi everyone, I am new to Unix and need help writing a script that can ask user for an input, then search that input within a file I know will have to use the read and grep commands, anyone can give me somewhere to start would help Task: Write a script to display which volume pool a given... (1 Reply)
Discussion started by: 12ic11
1 Replies

5. UNIX for Dummies Questions & Answers

UNIX Scripting help to input string and search a file to find

Hi everyone, I am new to Unix and need help writing a script that can ask user for an input, then search that input within a file I know will have to use the read and grep commands, anyone can give me somewhere to start would help Task: Write a script to display... (1 Reply)
Discussion started by: 12ic11
1 Replies

6. Shell Programming and Scripting

UNIX shell script to search a string in a file

Hi folks, I am new for shell script, I hope somebody to help me to write shell script My requirement is below steps 1. I have apache access.log i.e located in /var/log/httpd/ Ex. 127.0.0.1 - - "GET... (14 Replies)
Discussion started by: Chenchireddy
14 Replies

7. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

8. Shell Programming and Scripting

Need unix commands to delete records from one file if the same record present in another file...

Need unix commands to delete records from one file if the same record present in another file... just like join ... if the record present in both files.. delete from first file or delete the particular record and write the unmatched records to new file.. tried with grep and while... (6 Replies)
Discussion started by: msathees
6 Replies

9. UNIX for Dummies Questions & Answers

Unix search a string in the text file

File name : Sample.txt Actually i would like to read <schema>Oracle<schema> string from input file and return only once database as my output. Please advise me. Moved to appropriate forum. (1 Reply)
Discussion started by: balajikalai
1 Replies

10. UNIX for Dummies Questions & Answers

search a word in a file present in tar file

i need to search a word in the tar file. if it is present just give me the file name please help me (1 Reply)
Discussion started by: junkbuster
1 Replies
Login or Register to Ask a Question