IN Between Data after matching the Pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting IN Between Data after matching the Pattern
# 1  
Old 02-23-2017
IN Between Data after matching the Pattern

HI ,
I WANT TO RETRIVE IN BETWEEN DATA FROM PARENTHESIS AND I AM GETTING ERRORS WHILE RUN THE AWK.I HAVE 2 FILES AND WANT TO PROCESS 1ST FILE PATTERN TO 2ND FILE AND WRITES INTO OUTPUT FILE.THIS TIME I AM PUTTING WHERE EXACTLY I AM GETTING ERRORS.SO PLEASE HELP.
Code:
PATTERN_FILE.TXT
--------------
ABCD
PQRS
XYZ

INPUT FILE.TXT
----------------

CRAETE TABLE ABCD
(
A,
B,
C
);
CREATE TABLE PQRS
(
P,
R
);

SO HERE IN CASE 1ST PATTERN(ABCD) READS AND LOOK INTO INPUT FILE.TXT AND IF FINDS THE MATCH IT SHOULD GET THE DATA BETWEEN PARENTHESIS
Code:
A,
B,
C

NEXT PATTERN (PQRS) AND IT MATCHES AND GETS THE RESULT
Code:
P,
R

NEXT XYZ COMES AND NO MATCH SO NOTHING RETURNED.
I WANT TO READ THIS THROUGH LOOP SO AS ONCE 1ST FILE WRITE INTO OUTPUT FILE I WILL DO SOME MODIFICATION BEFORE SEARCH THE NEXT PATTERN.

MY CODE
-------
Code:
for i in `cat pattern_file.txt`
do
awk '/'{print "$i"}'/{flag=1;next}/);/{flag=0}flag' INPUT_FILE.txt > output.txt
cat output.txt >> output_1.txt
echo "@@@@@@@@" >> output_1.txt
done

Moderator's Comments:
Mod Comment edit by bakunin: writing in all-caps is against our forum rules - which you have accepted at least three times now, because this you have managed to amass enough infractions to be set to read-only with your account "raju2016" and banned with your acccount "raj121".

I hereby give you time to think about your future usage of our forum: 20 points infraction. In the meantime your thread is closed because i am unwilling to see your lacking manners being rewarded by a solution.

Last edited by bakunin; 02-23-2017 at 02:20 PM..
# 2  
Old 02-23-2017
Moderator's Comments:
Mod Comment Please do not write in allcaps.


"for I in `cat file`" is a dangerous use of backticks. It does not do what you think it does. Do
Code:
while read LINE
do
...
done < inputfile

instead.

There's better ways to get variables into awk, such as awk -v VAR="value" or awk 'expression' VAR=value FILENAME, no need to embed it in the expression.

So:

Code:
while read PAT
do
        awk -v PAT="$PAT" 'PAT ~ $0 { P=1 } P ; /\);/ { P=0 }' inputfile
        echo "@@@@@@@@"
done < pattern_file.txt > output.txt

Moderator's Comments:
Mod Comment edit by bakunin: see comment in starting post, i hid your solution.

Last edited by bakunin; 02-23-2017 at 02:25 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

2. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

3. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

4. UNIX for Dummies Questions & Answers

Find pattern suffix matching pattern

Hi, I am trying to get a result out of this but fails please help. Have two files /tmp/1 & /tmp/hosts. /tmp/1 IP=123.456.789.01 WAS_HOSTNAME=abcdefgh.was.tb.dsdc /tmp/hosts 123.456.789.01 I want this result in /tmp/hosts if hostname is already there dont want duplicate entry. ... (5 Replies)
Discussion started by: rajeshwebspere
5 Replies

5. Shell Programming and Scripting

Script to read file and extract data by matching pattern

Hello, I have a file ( say file1) which has lines like below. xxxx:xxxx,yyyy,1234,efgh zzzz:zzzz,kkkk,pppp,1234,xxxx,uuuu,oooo dddd:dddd here the word before ":" ( ie: xxxx) is the file name and the string after : are also file names, but each file name separated by "," In case of... (20 Replies)
Discussion started by: pradeepmacha
20 Replies

6. Shell Programming and Scripting

Removing data with pattern matching

I have the following: HH:MM:SS I want to use either % or # sign to remove :SS can somebody please provide me an example. I know how to do this in awk, but awk is too much overhead for something this simple since I will be doing this in a loop a lot of times. Thanks in advance to all... (2 Replies)
Discussion started by: BeefStu
2 Replies

7. Shell Programming and Scripting

sed - matching pattern one but not pattern two

All, I have the following file: -------------------------------------- # # /etc/pam.d/common-password - password-related modules common to all services # # This file is included from other service-specific PAM config files, # and should contain a list of modules that define the services... (2 Replies)
Discussion started by: RobertBerrie
2 Replies

8. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

9. Shell Programming and Scripting

help needed .. Unable to write the data to new file after matching the pattern

Hi, i am pretty new to Unix environment ..... Can i get some help from any of you guyz on writing Unix script. my requirement is like reading a csv file, finding a specific pattern in the lines and repalce the string with new string and write it to another file. My file is file ABC123.dat... (3 Replies)
Discussion started by: prashant_jsw
3 Replies

10. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies
Login or Register to Ask a Question