Cut line from searched file if grep find neighbor columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut line from searched file if grep find neighbor columns
# 1  
Old 12-06-2018
Cut line from searched file if grep find neighbor columns

Hello All,
While searching for the question, I found some answers but my implementation is not giving expected output.
I have two files; one is sourcefile, other is named template.
What I want to do is to search each line in template, when found all columns, cut the matching line from source file.
Print order is important. I have tested another script but it gives the output in alphabetical order which I do not desire.
If column1 and column2 in template file are not neighbor in sourcefile, I wish the script to skip that line. Max column nr is: 5

sourcefile
Code:
Monday Tuesday Wednesday
Monday Friday 998877
Tuesday Friday 112233
Tuesday Friday 112233 11223344
Monday 998877 012345678
Tuesday Friday
Thursty

template
Code:
Monday 998877
Tuesday Friday
Thursday Friday
Saturday
Sunday
Monday Tuesday Thursday Friday

Expected output with below order:
Code:
Tuesday Friday 112233
Tuesday Friday 112233 11223344
Monday 998877 012345678
Tuesday Friday

Tested script:
Code:
while read -r COL1 COL2 COL3 COL4 COL5
do
if grep -q "$COL1\|$COL2\|$COL3\|$COL4\|$COL5" sourcefile; then
    grep -Ff sourcefile template
fi
done<template


I'd like to know how to manage this process.

Thanks
Boris

Last edited by baris35; 12-06-2018 at 04:33 PM.. Reason: paste error on expected output
# 2  
Old 12-06-2018
how exactly did you get Monday Tuesday Thursday Friday in your desired output?
# 3  
Old 12-06-2018
Hello,
My fault. I am gonna update the update now.
Thanks for the warning

Boris
# 4  
Old 12-06-2018
grep -F -f template sourcefile
This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 12-06-2018
Dear Vgersh99,
Thank you so much!

Kind regards
Boris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep: Retrieve two strings from one file to find them anyone on line in another file

I am having trouble matching *two* strings from one file anywhere in a line of a second file, and could use some help getting this figured out. My preference would be to use grep for this because I would like to take advantage of its -A option. The latter is due to the fact that I would like both... (2 Replies)
Discussion started by: jvoot
2 Replies

2. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

3. Shell Programming and Scripting

How do i find the first number in each line and insert dummy string into the missing columns?

Hi, I have one input file with the following content: MY_inpfile.txt Aname1 Cname1 Cname2 1808 5 Aname2 Cname1 1802 47 Bname1 ? 1819 22 Bname2 Cname1 1784 11 Bname3 1817 9 Zname1 Cname1 1805 59 Zname2 Cname1 Cname2 Cname3 1797 27 Every line in my input file have a 4 digit... (5 Replies)
Discussion started by: Szaffy
5 Replies

4. Shell Programming and Scripting

cut the variable from the line and use it to find the file and read the content of that file

Hi, I am working on one script..I am having files in the below format file 1 (each line is separated with : delimeter) SPLASH:SPLASH:SVN CIB/MCH:MCH:SVN Now I want from file 1 that most left part of the first line will store in... (6 Replies)
Discussion started by: rohit22hamirpur
6 Replies

5. Slackware

How should I cut this line using cut and grep?

not sure how to do it. wan't to delete it using cut and grep ince i would use it in the shell. but how must the command be? grep "64.233.181.103 wwwGoogle.com" /etc/hosts | cut -d the delimeter is just a space. can you help meplease. :D (1 Reply)
Discussion started by: garfish
1 Replies

6. Shell Programming and Scripting

how to get lines prior to the line being searched

Hi, Can anbody please let me know how i can retrieve lines above the line being searched in a file. I am looking for an error message from a file, if I see that message I want the lines above that message along with this line. how do we do this. Please do let me know An example which i have... (2 Replies)
Discussion started by: arunrao_oradba
2 Replies

7. Shell Programming and Scripting

how to grep a file and cut a corresponding value from the line

i have to grep a file which has contents like /Source/value/valuefile/readme.txt DefaultVersion:=1.7 I have to grep this file with "/Source/value/valuefile/readme.txt" and cut the corresponding value "1.7" from the same line i tried grep and cut but not working. (1 Reply)
Discussion started by: codeman007
1 Replies

8. Shell Programming and Scripting

Using grep - check the permissions of the file searched

What I need to do is: I need to use the grep command to search for pattern in directory and sub-directories. And also I need to show the permission of file been seached by the grep command. Could any one please suggest me? ----------------- $> cat file1.txt A -----------------... (8 Replies)
Discussion started by: Johny001
8 Replies

9. UNIX for Dummies Questions & Answers

Grep or other ways to output line above and/or below searched line

Hi all, Would like to know how I could search for a string 'xyz' but have the output show the line plus the line above and/or below all lines found. eg. search for xyz from file containing: abc 12345 asdf xyz asdfds wwwww kjkjkj ppppp kkkxyz eeee zzzzz and the output to... (2 Replies)
Discussion started by: sammac
2 Replies

10. Shell Programming and Scripting

grep & cut the file

I need to grep and cut the some file in the folder and output to some file. the sample file looks like below -rw-r--r-- 1 gui gui 28050789 Jun 25 18:38 mymkzpiii.txt -rw-r--r-- 1 gui gui 127983856 Jun 25 18:39 phmnlpiii.txt i need the output like below ... (3 Replies)
Discussion started by: aboorkuma
3 Replies
Login or Register to Ask a Question