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

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

thanks it works

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

Add the following to the cut -c8-40 file-log:

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

cut -c8-40 | sed 's/ PBX-id: /,/' >file-log

added the above and it did not give any result. please help

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

can someone help me out with this please?...

First, you could read "man cut" to find out how the cut command is working and how to change it according to you data.

Second, just change the 8-40 to 10-40!!

Third!! your original command was:

cut -c8-40 file_log

so you should have added the sed at the end as:

cut -c8-40 file_log | sed ....

Not after 40!?!?!

THnaks for the reply.

The output of the script is

PBX TYPE:CC3 ,NSH

note the space between CC3 and ,NSH

I would want this to be :

CC3,NSH

My script is like this. Please reveiw.

##!/usr/bin/sh -x
##Initialising variables

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

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

echo "----------------------------LIST OF INCORRECT PBX/SWTICH TYPE FOUND--------------------------"

echo "----------------------------LIST The CORRECT PBX/SWTICH TYPE---------------------------------"
cd $FROM_DIR

for file in $FROM_DIR/LEE*.cc3

do

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

cut -c10-40| sed 's/ PBX-id: /,/' file_log

done
exit 0

This is NOT the script that you're running!!!

The "cut" has STILL no File-Parameter and is piped to the "sed" which has the File-Parameter.

You should have missed ONE OF THE BLANKS in "/ PBX-id: /,/" !!!

##!/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/LEE*.cc3

do

grep '^...... PBX' LEECOUNTY.cc3 > file_log

cut -c8-40 > file_log1

sed 's/ PBX-id: /,/' file_log1 > file_log2

done
exit 0

This is the script and when i cat file_log2 the output is:

PBX TYPE:IDC4 ,L14
PBX TYPE:IDC3 ,L12
PBX TYPE:IDC5 ,L13

Note there is lot of space between IDC4 and ,L14

Desired output would be:
IDC4,L14
IDC3,L12
IDC5,L13

OK apparantly you have more than ONE BLANK around the text!!

Please use:

sed 's/ *PBX-id: */,/' file_log1 > file_log2

Note the '*' after each blank.

Thanks so much.i really appreciate it. was help up for so many days.

Just one last thing , now the result is

PBX TYPE:IDC4,L14
PBX TYPE:IDC3,L12
PBX TYPE:IDC5,L13

What can i do to remove PBX TYPE: so that i have only

IDC4,L14
IDC3,L12
IDC5,L13
i can do a cut of this result again but can't we specify the elimination of the result in sed itself?

Thnx again:)

Regards,
Sandeep

Change "-c8-40" to "-c17-40" !!!

Don't you ever want to know how UNIX-Commands work?

Thanks again, I will do the change. I am so soryy to have troubled you so much. I am trying to and i have written some scripts as well. Those were using very easy commands and they look very amatuer. I am doing some googling and studying but since i have to spend most of my time with office work i am doing this outside my office timmings on interest. Also there is no one guiding as well. Thanks and with help of people like you one day i hope i can give solutions to others like you are doing

Thanks again

Regards,
Sandeep