pattern match in each line and capture it question


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users pattern match in each line and capture it question
# 1  
Old 03-27-2008
pattern match in each line and capture it question

I need a clarification on one of the scripts that i have written, I new to file handling and i need help:

I am trying to find a particular pattern in a file "****** PBX" in set of 5 files named:
xy.cc3
xv.cc3
xx.cc3
xr.cc3
xd.cc3

in a directory. If i find the files starting with these line i need to capture the full line and put it in a log file.

Can anyone help me please

Thanks,
Sandeep
# 2  
Old 03-27-2008
try

grep '^...... PBX' xy.cc3 xv.cc3 xx.cc3 xr.cc3 xd.cc3 >logfile

This will prefix each "extracted-line" with the corresponding file-name

(The '^' character represents the start-of-the-line and each dot ('.') represents one-arbitrary-character, in regular-expresions used in line-identification in commands like grep, egrep, awk, sed, etc.)
# 3  
Old 03-27-2008
thanks it works
# 4  
Old 03-28-2008
I need one more info , I have written the script below with your inputs
---------------------------------------------------------------------
##!/usr/bin/sh -x
##Initialising variables

FROM_DIR=/usr/local/Sandeep_test/santest; export FROM_DIR

PBX_TXT="****** PBX"; export PBX_TXT

cd $FROM_DIR

for file in $FROM_DIR/*.cc3

do

grep '^...... PBX' $file > file_log

cut -c8-40 file_log

done
exit 0
--------------------------------------------------------

The output of the the above script is
Output:

PBX TYPE:san PBX-id: 555
PBX TYPE:san2 PBX-id: 556
PBX TYPE:san3 PBX-id: 557

now i need one more info:How do i catch the above result so that san and 555 are put it in a file ( example: file 1) like the below format:

san,555
san2,556
san3,557

and then comapare this line by line with an another file(example:file 2) that is as below:
san,555
san2,556
san3,558

now after the compare of the file 1 and file 2 it should say that san3,557 is not correct as this is not in file 2

Please help will be very gratefull

Thanks,
Sandeep
# 5  
Old 03-28-2008
Add the following to the cut -c8-40 file-log:

Code:
| sed 's/ PBX-id: /,/' >file-1

and then use either diff or comm (w. file-1 file-2 as its two arguments) in order to see the difference.
# 6  
Old 03-28-2008
cut -c8-40 | sed 's/ PBX-id: /,/' >file-log


added the above and it did not give any result. please help
# 7  
Old 03-28-2008
sorry but this how the output looks from my script. i mistakingly sent the wrong yesterday:

PBX TYPE:IDC4 PBX-id: L14
PBX TYPE:CC3 PBX-id: LC4
PBX TYPE:CC3 PBX-id: LC8

aloso can u please explain the code, when u send it ........will be grateful.


Thanks,
sandeep
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

4. Shell Programming and Scripting

Print only next pattern in a line after a pattern match

I have 2013-06-11 23:55:14 1Umexd-0004cm-IG <= user@domain.com I need sed/awk operation on this, so that it should print the very next pattern only after the the pattern mach <= ie only print user@domain.com (7 Replies)
Discussion started by: anil510
7 Replies

5. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

6. Shell Programming and Scripting

pattern match and replace another pattern in same line

I have a pattern username:x:32005:32006::/usr/local/user:/bin/bash I need to match the line containing username and replace /bin/bash with /usr/local/my/bin/noshell So it becomes username:x:32005:32006::/usr/local/user:/usr/local/my/bin/noshell (7 Replies)
Discussion started by: anilcliff
7 Replies

7. Shell Programming and Scripting

Print Line if next line Match a pattern

Hi All, Does anyone know how to print 1H1A....... in peal script print line ^1H1A....... if next line equal 5R0RECEIPT.... Thank for help:D Cat st.txt 1H1A-IN-11-5410-0009420|1010047766|dsds|1|N|IN|IN|000000|1||N|<<<line match 5R0RECEIPT| 5R0RECEIPT|... (2 Replies)
Discussion started by: kittiwas
2 Replies

8. Shell Programming and Scripting

pattern match question

I have a pattern match problem I could use your help with I have a file in the following format (names.txt) jae,doe john,doe jay,doe I need to loop through the file using FOR and check the names in names.txt against another file (information.txt). information.txt is in the following... (2 Replies)
Discussion started by: chaos40
2 Replies

9. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies

10. Shell Programming and Scripting

match a pattern, print it and the next line

I have a file nbu_faq.txt (Question/answer) which looks like this What I am trying to do is write out each question in a file1.txt and than the question/answer in a file2.txt like this file1.txt Q: What is nbu? Q: What is blablabla...? Q: Why ....? file2.txt Q: What is nbu? A:... (4 Replies)
Discussion started by: nymus7
4 Replies
Login or Register to Ask a Question