Fetching a line matching a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetching a line matching a pattern
# 1  
Old 06-30-2015
Fetching a line matching a pattern

Hi Gurus,

I have a file as follows (Sample shown below but the list is very huge)

Code:
SCHEDULE WS1#JS1
RUNCYCLE1
:
WS1#JOB1
WS1#JOB2
FOLLOWS JOB1
END

SCHEDULE WS2#JS1
RUNCYCLE2
:
WS1#JOB3
WS1#JOB1
FOLLOWS JOB3
WS2#JOB1
FOLLOWS JOB1
END

Now i have another file as below

Code:
WS1#JOB1
WS2#JOB1
WS3#JOB4

Now i need to fetch the schedule names from the first file for the jobs seen in the second file. So now the output would be as below

Code:
WS1#JOB1 WS2#JS1
WS1#JOB1 WS2#JS1
WS2#JOB2 WS2#JS1
WS3#JOB4 Not Available in file1

Kindly help me out with this.

Last edited by jayadanabalan; 06-30-2015 at 08:55 AM.. Reason: Missed a line in file 2
# 2  
Old 06-30-2015
I don't think your desired output is compliant with your input data, and it has duplicate values in it. So it was difficult to implement sth fitting to your specification.
Anyhow, try
Code:
awk '
NR==FNR         {T[$1]
                 next
                }
/SCHEDULE/      {SCHEDNAM=$2}

$1 in T         {print $1, SCHEDNAM
                 delete T[$1]
                }
END             {for (t in T) print t, "not in file1"
                }
' file2 file1
WS1#JOB1 WS1#JS1
WS2#JOB1 WS2#JS1
WS3#JOB4 not in file1

# 3  
Old 07-01-2015
Hi Rudic,

Thanks for the reply.

The script is working with the example that i had given, but when i try this with a big file, the script doesnt seem to work. I am getting not in file1 for all the data in file2.
# 4  
Old 07-01-2015
So - the big file doesn't seem to be structurally identical to your samples.
# 5  
Old 07-01-2015
This is a part of my big file

Code:
#Mon-Sun (incl hol)  
 
SCHEDULE A01G3GBOAPP1A#J01AQMSPBATCH01  
ON RUNCYCLE RULE1 "FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR,SA,SU" 
UNTIL 1200 +1 DAYS  
PRIORITY 0 
: 
A01G3GBOAPP1A#J01AQMSPSR2PWEB 
 PRIORITY 0 
 
A01G3GBOAPP1A#J01AQMSPSURPWEB 
 PRIORITY 0 
 
A01G3GBOAPP1A#J01AQMSPCUSTSUR 
 PRIORITY 0 
END 
 
#Mon-Sun (incl hol)  
 
SCHEDULE A01G3GBOAPP1A#J01AQMSPBATCH02  
ON RUNCYCLE RULE1 "FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR,SA,SU" 
UNTIL 1200 +2 DAYS  
PRIORITY 0 
: 
A01G3GBOAPP1A#J01AQMSPEMAILIN 
 PRIORITY 0 
END

Now if i search for "A01G3GBOAPP1A#J01AQMSPCUSTSUR", i must get output with "A01G3GBOAPP1A#J01AQMSPBATCH01"

Code:
A01G3GBOAPP1A#J01AQMSPCUSTSUR A01G3GBOAPP1A#J01AQMSPBATCH01

Likewise "A01G3GBOAPP1A#J01AQMSPCUSTSUR" can be in multiple schedules as well.

Last edited by jayadanabalan; 07-01-2015 at 03:22 AM.. Reason: Missed a data
# 6  
Old 07-01-2015
And what's in "another file as below" (cf. post#1)?
# 7  
Old 07-01-2015
The other file contains data like "A01G3GBOAPP1A#J01AQMSPCUSTSU" and there are more than 23000 records. Example seen below.

Code:
A01G3GDB1A#AEACCTBALMSTR
A01G3GDB1A#AEACCTBALMSTR_N
A01G3GDB1A#AEBALANCESHEETFACT
A01G3GDB1A#AEBALANCESHEETFACT_N
A01G3GDB1A#AECO1PREFSHARES


Last edited by jayadanabalan; 07-01-2015 at 03:50 AM.. Reason: extra space came in
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing spaces from line matching a pattern

Hi, I want to remove the spaces from all the lines matching a particular pattern from my file. For instance in file abc.txt I have following data. Header,This is the header 111,this is 1st record 222, this is 2nd record 333, this is 3rd record Footer,3 records found Footer,111222333 ... (5 Replies)
Discussion started by: decci_7
5 Replies

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

3. Shell Programming and Scripting

matching pattern to end of line

I have the following code and want to get only the comments. Ideally I would like to replace the characters to the left of the first '!' in the line with blanks. real, dimension(:), allocatable :: ft ! stores a single trace real, dimension(:), allocatable :: tr ! stores a single... (8 Replies)
Discussion started by: kristinu
8 Replies

4. Shell Programming and Scripting

Fetching string after matching pattern from last

I have a file a file having entries are like @ram@sham@sita @krishan@kumar @deep@kumar@hello@sham in this file all line are having different no of pattern-@. need to fetch the substring after the last pattern. like sita kumar sham thanks in advance (3 Replies)
Discussion started by: saluja.deepak
3 Replies

5. Shell Programming and Scripting

How to use sed to modify a line above or below matching pattern?

I couldn't figure out how to use sed or any other shell to do the following. Can anyone help? Thanks. If seeing a string (e.g., TODAY) in the line, replace a string in the line above (e.g, replace "Raining" with "Sunny") and replace a string in the line below (e.g., replace "Reading" with... (7 Replies)
Discussion started by: sprinner
7 Replies

6. Shell Programming and Scripting

Shell Scripting:Fetching content from each line with respect to pattern.

one.txt ONS.820.log:V 20Oct2010:GP ^ ^ ONS.123.log:V 21Oct2010:GP ^ ^ ONS.820.log:V 30Oct2010:GP ^ ^ want to make new file from existing one with addition. 20Oct2010 User KV001 has name tk003 with buffer- 338-1 21Oct2010 User KV003 has name tk002 with buffer- 338-2 30Oct2010 User KV002... (5 Replies)
Discussion started by: saluja.deepak
5 Replies

7. Shell Programming and Scripting

Extracting a string matching a pattern from a line

Hi All, I am pretty new to pattern matching and extraction using shell scripting. Could anyone please help me in extracting the word matching a pattern from a line in bash. Input Sample (can vary between any of the 3 samples below): 1) Adaptec SCSI RAID 5445 2) Adaptec SCSI 5445S RAID 3)... (8 Replies)
Discussion started by: jharish
8 Replies

8. Shell Programming and Scripting

Fetching just the matching pattern

Hi Everybody, I would like you to help me with the following problem. Given is a path to a file like this: /root/DIR/subdir/file.dat Having this path I would like to grep/sed or whatever the directory string that matches the following pattern ''. I just need the matching directory... (5 Replies)
Discussion started by: hhoosscchhii
5 Replies

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

10. UNIX for Dummies Questions & Answers

exclude columns with a matching line pattern

Hi , I have 5 columns total and am wanting to search lines in columns 3-5 and basically grep -v patterns that match 'BBB_0123' 'BVG_0895' 'BSD_0987' Does anyone know how to do this? I tried combining grep -v with grep -e but, it didn't work. Thanks! (5 Replies)
Discussion started by: greptastic
5 Replies
Login or Register to Ask a Question