Extract the whole set if a pattern matches

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Extract the whole set if a pattern matches
# 1  
Old 02-21-2017
Extract the whole set if a pattern matches

Hi,

I have to extract the whole set if a pattern matches.i have a file called input.txt

input.txt
------------
Code:
 CREATE TABLE ABC
(
A,
B,
C
);
CREATE TABLE XYZ
(
X,
Y,
Z,
P,
Q
);

Now i have another file called table.txt which has an entry

Table.txt
----------
Code:
 XYZ

so now if XYZ matches the string in Input.txt then i need to grab it into a different file called output.txt.As XYZ maches the pattern which is in input.txt and need to push to output.txt

output.txt
----------
Code:
CREATE TABLE XYZ
(
X,
Y,
Z,
P,
Q
);

for better match we can include the whole string like CREATE TABLE XYZ





Moderator's Comments:
Mod Comment Please use CODE tags correctly as required by forum rules! Enclose code, data, error msgs and the like, NOT the entire text!

Last edited by RudiC; 02-21-2017 at 06:34 AM.. Reason: Changed CODE tags.
# 2  
Old 02-21-2017
Try (untested)
Code:
awk 'NR==FNR {T[$1]; next} {for (t in T) if ($0 ~ t) print}' RS=";" ORS=";" table.txt input.txt


Last edited by RudiC; 02-21-2017 at 07:23 AM..
# 3  
Old 02-21-2017
Code:
Hi Rudi,The code is working but i need to pass the pattern which is in Table.txt in  loop.So in the Table.txt file may conatin multiple patterns.so i need to process those one by one and  write into output file.So pattern will go to input file and match the pattern and write into output file.

Exp:
Table.txt

XYZ
ABC

oUTPUT.TXT
------------------
CREATE TABLE ABC
(
A,
B,
C
);
CREATE TABLE XYZ
(
X,
Y,
Z,
P,
Q
);

# 4  
Old 02-21-2017
Code:
awk 'NR==FNR {T[$0]; next} {for (t in T) if ($0 ~ t) printf "%s;\n", $0}' table.txt RS=";"  input.txt

# 5  
Old 02-21-2017
@Rudi:This is not working.i want to pass one record of Table.txt and search for that pattern and get the whole set from Input.txt and store into output.txt.

So in 1st instance i want to pass XYZ and look for that match in input.txt and then i need to do some transformationand later store into output.txt.

Same way the next record from table.txt is ABC and i need to pass that to Input .txt and do transformation and append that to output.txt.

i think we need to pass those with while loop/ for loop and get processed.
# 6  
Old 02-21-2017
Well, it IS working, to the specs in posts#1 and #3. It gives exactly the desired / posted output.
If you need sth. else, start over and post a decent specification with requirements, input, and output samples.
# 7  
Old 02-21-2017
Quote:
Originally Posted by raju2016
@Rudi:This is not working.
And, as always, in addition to RudiC's already raised points: WHAT IS NOT WORKING, FOR CHRISSAKES??

If you are ill and visit a doctor: do you tell him your exact symptoms or just "it hurts"?

Show us what you did, what your input was, what your output was and how it differed from the expected output. Otherwise this is going to be a trade of "it works", "no, it doesn't", "yes, it does", "no, it doesn't", ... ad libitum ad nauseam.

Please take note: if you can't be bothered to tell us exactly what you want and how our suggestions fail (in other words: invest effort into your question) we won't be bothered to invest any effort in answering these question of yours. This here is give and take - and right now you are taking without giving.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get a 1st line which matches the particular pattern?

Hi all, I have file on which I do grep on "/tmp/data" then I get 5 lines as dir Path: /tmp/data/20162343134 Starting to listen on ports logging: -- Moving results files from local storage: /tmp/resultsFiles/20162343134/*.gz to NFS: /data/temp/20162343134/outgoing from above got to get... (7 Replies)
Discussion started by: girijajoshi
7 Replies

2. Shell Programming and Scripting

Extract all the sentences from a text file that matches a pattern list

Hi I have a big text file. I want to extract all the sentences that matches at least 70% (seventy percent) of the words from each sentence based on a word list called A. Say the format of the text file is as given below: This is the first sentence which consists of fifteen words... (4 Replies)
Discussion started by: my_Perl
4 Replies

3. Shell Programming and Scripting

Blocks of text in a file - extract when matches...

I sat down yesterday to write this script and have just realised that my methodology is broken........ In essense I have..... ----------------------------------------------------------------- (This line really is in the file) Service ID: 12345 ... (7 Replies)
Discussion started by: Bashingaway
7 Replies

4. Shell Programming and Scripting

awk with range but matches pattern

To match range, the command is: awk '/BEGIN/,/END/' but what I want is the range is printed only if there is additional pattern that matches in the range itself? maybe like this: awk '/BEGIN/,/END/ if only in that range there is /pattern/' Thanks (8 Replies)
Discussion started by: zorrox
8 Replies

5. Shell Programming and Scripting

How Not to Delete Words that matches a PATTERN

Hi, I have a test file name test.txt with its contents string 21345 qwee strinn strriin striin i need to delete all the words except the word STRING I used the command cat test.txt | sed 's/^..*$/**/g' but the output entries still contain strinn strriin striin. Plz Help me out.... (5 Replies)
Discussion started by: Ananth12
5 Replies

6. Shell Programming and Scripting

Extract columns where header matches a given string

Hi, I'm having trouble pulling out columns where the headers match a file of key ID's I'm interested in and was looking for some help. file1.txt I Name 34 56 84 350 790 1215 1919 7606 9420 file2.txt I Name 1 1 2 2 3 3 ... 34 34... 56 56... 84 84... 350 350... M 1 A A A A... (20 Replies)
Discussion started by: flotsam
20 Replies

7. Shell Programming and Scripting

Remove if the above line matches pattern

but keep if does not I have a file: --> my.out foo: bar foo: moo blarg i am on vacation foo: goose foo: lucy foo: moose foo: stucky groover@monkey.org foo: bozo grimace@gonzo.net dear sir - blargo blargo foo: goon foo: sloppy foo: saudi gimme gimme gimme (3 Replies)
Discussion started by: spacegoose
3 Replies

8. Shell Programming and Scripting

get value that matches file name pattern

Hi I have files with names that contain the date in several formats as, YYYYMMDD, DD-MM-YY,DD.MM.YY or similar combinations. I know if a file fits in one pattern or other, but i donīt know how to extract the substring contained in the file that matches the pattern. For example, i know that ... (1 Reply)
Discussion started by: pjrm
1 Replies

9. Shell Programming and Scripting

awk to count pattern matches

i have an awk statement which i am using to count the number of occurences of the number ,5, in the file: awk '/,5,/ {count++}' TRY.txt | awk 'END { printf(" Total parts: %d",count)}' i know there is a total of 10 matches..what is wrong here? thanks (16 Replies)
Discussion started by: npatwardhan
16 Replies

10. Shell Programming and Scripting

Extract if pattern matches

Hi All, I have an input below. I tried to use the awk below but it seems that it ;s not working. Can anybody help ? My concept here is to find the 2nd field of the last occurrence of such pattern " ** XXX ccc ccc cc cc ccc 2007 " . In this case, the 2nd field is " XXX ". With this "XXX" term... (20 Replies)
Discussion started by: Raynon
20 Replies
Login or Register to Ask a Question