Grep for string

I can't come to think of any other way than just grep, but hopefully someone can give me a hint...

In a script I am working on, I need to get which scsi channel a disk is connected to.
I know it's devicename, which is sdc.

With this knowledge, I run hdparm -I /dev/sdc and grep for "Model number:" ,
through awk, I get it's model number, and run the command scsiadd -p and gep for the model number.

I get the line with the model number,
what I need though, it the line just right above.
I need to find out what scsi channel the disk is connected to so that I can runt scsiadd -r 3 0 0 0 (in this case)

Host: scsi3 Channel: 00 Id: 00 Lun: 00
Vendor: ATA      Model: ST3120026AS      Rev: 8.05
Type:   Direct-Access                    ANSI  SCSI revision: 05

Now is just the question, through the way I am grepping for model number, is there anyway I can get the line above?
The extraction of the numbers I'll do with more greps and awk's (unless someone has another idea).

I hope I am making myself understood enough.. I'm getting frustrated and are not entirely sure I describe my problem in a good enough way :smiley:

sed -n '/Model:/N; /Channel:/p'

Just keep the previous line in awk:

awk '{ if($0 ~ /Model:/) { print ll; } ll=$0 }'

As citaylor says - it could look like (if I got it right):

awk '/^Host:/ {sub(/scsi/,"",$2); a=$2; b=$4; c=$6; d=$8} /ST3120026AS/ {printf("scsiadd -r %i %i %i %i\n", a,b,c,d)}' infile
scsiadd -r 3 0 0 0

And maybe use system() to execute it or execute it with shell's $().

Kudos to verdepollo for giving you what you asked for. Here's what I think you need:

MODEL=$(hdparm -I /dev/sdc | <some code here>$)
scsiadd -p |  
awk '/^Host: scsi/ { scsiid=substr($2,5,1) " " $4 " " $6 " " $8 }
       /Model: '"$MODEL"/' { print scsiid; found=1; exit(0); }
       END { exit(1-found); }' 

---------- Post updated at 05:55 PM ---------- Previous update was at 05:52 PM ----------

Zaxxon and I posted about the same time. We take the same general approach but vary in our actual tactics. The exit() condition in mine allows you to test the return code of the command to see if it worked, ie, to put in a script, pipeline or if statement and fail appropriately.

Sometimes a little truss/tusc/strace, strings on commands, or man page reading, can reveal where the information is stored in flat file land!

Important: What Operating System and version are you running?
What hardware do you have?

Posting hardware enquiry comands out of context means nothing without knowledge of the command syntax and the hardware.

What is the output from:

ls -lad /dev/whatever_your_device_is_called

This should produce a unique identity mappable to the SCSI ID given your knowledge of your Operating System and hardware configuration.

methyl - the output of his commands is linux-specific.

If you wanted to stick with grep, you could use

-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines. Places a line containing a group separator (--) between contiguous groups of matches.

This may sound simplistic but can't you just grep "Channel"? Either in the way you are doing it now or with a nested grep? Pardon, I'm a little rusty in Unix. :slight_smile:

HI I have one more brain twister for u all i have a 2 text file, i need to find the diff in row and to write in third file but under the same partitions. for eg:File :1 ==================== ============================== ======================== ============================== JOB STATUS - Activity ==================== ============================== ======================== ==============================JOB NAME ,ODATE ,START ,FINISH ,STATUS d_voy_audt_trxn_refnc_uld.ksh ,20110317 ,Mar 17,04:41:49 ,Mar 17,04:43 ,SUCCESS d_isso_actvy_log_uld_cln.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS d_voy_svc_sesn_ctm.ksh , ,~~~ UPSTREAM PENDING/YET TO START ~~~ , , d_svc_evt_sumry_ld_ctm.ksh , ,~~~ UPSTREAM PENDING/YET TO START ~~~ , , ==================== ============================== ======================== ============================== JOB STATUS - Schedule ==================== ============================== ======================== ==============================JOB NAME ,ODATE ,START ,FINISH ,STATUS d_voy_audt_trxn_refnc_uld.ksh ,20110317 ,Mar 17,04:41:49 ,Mar 17,04:43 ,SUCCESS d_isso_actvy_log_uld_cln.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS d_voy_svc_sesn_ctm.ksh , ,~~~ UPSTREAM PENDING/YET TO START ~~~ , , d_svc_evt_sumry_ld_ctm.ksh , ,~~~ UPSTREAM PENDING/YET TO START ~~~ , , ==================== ============================== ======================== ============================== JOB STATUS - Linkage ==================== ============================== ======================== ==============================JOB NAME ,ODATE ,START ,FINISH ,STATUS d_voy_audt_trxn_refnc_uld.ksh ,20110317 ,Mar 17,04:41:49 ,Mar 17,04:43 ,SUCCESS d_isso_actvy_log_uld_cln.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS d_voy_svc_sesn_ctm.ksh , ,~~~ UPSTREAM PENDING/YET TO START ~~~ , , d_svc_evt_sumry_ld_ctm.ksh , ,~~~ UPSTREAM PENDING/YET TO START ~~~ , , #############################################################################################################################File :2 ==================== ============================== ======================== ============================== JOB STATUS - Activity ==================== ============================== ======================== ==============================JOB NAME ,ODATE ,START ,FINISH ,STATUS d_voy_audt_trxn_refnc_uld.ksh ,20110317 ,Mar 17,04:41:49 ,Mar 17,04:43 ,SUCCESS d_isso_actvy_log_uld_cln.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS d_voy_svc_sesn_ctm.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS d_svc_evt_sumry_ld_ctm.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS ==================== ============================== ======================== ============================== JOB STATUS - Schedule ==================== ============================== ======================== ==============================JOB NAME ,ODATE ,START ,FINISH ,STATUS d_voy_audt_trxn_refnc_uld.ksh ,20110317 ,Mar 17,04:41:49 ,Mar 17,04:43 ,SUCCESS d_isso_actvy_log_uld_cln.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS d_voy_svc_sesn_ctm.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS d_svc_evt_sumry_ld_ctm.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS ==================== ============================== ======================== ============================== JOB STATUS - Linkage ==================== ============================== ======================== ==============================JOB NAME ,ODATE ,START ,FINISH ,STATUS d_voy_audt_trxn_refnc_uld.ksh ,20110317 ,Mar 17,04:41:49 ,Mar 17,04:43 ,SUCCESS d_isso_actvy_log_uld_cln.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS d_voy_svc_sesn_ctm.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS d_svc_evt_sumry_ld_ctm.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS #############################################################################################################################Output File: ==================== ============================== ======================== ============================== JOB STATUS - Activity ==================== ============================== ======================== ==============================JOB NAME ,ODATE ,START ,FINISH ,STATUS d_voy_svc_sesn_ctm.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS d_svc_evt_sumry_ld_ctm.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS ==================== ============================== ======================== ============================== JOB STATUS - Schedule ==================== ============================== ======================== ==============================JOB NAME ,ODATE ,START ,FINISH ,STATUS d_voy_svc_sesn_ctm.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS d_svc_evt_sumry_ld_ctm.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS ==================== ============================== ======================== ============================== JOB STATUS - Linkage ==================== ============================== ======================== ==============================JOB NAME ,ODATE ,START ,FINISH ,STATUS d_voy_svc_sesn_ctm.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS d_svc_evt_sumry_ld_ctm.ksh ,20110317 ,Mar 17,03:20:49 ,Mar 17,03:31 ,SUCCESS In the above 2 files the 3 & 4 row is different for all the 3 partition, so i want to write the 3 & 4 row frm file 2 to a new file. SO i need a script which find the non matching row between two file and to write in third file under the same partition.

Assuming this is a Linux kernel, have you tried looking in the /sys filesystem?

It's all driver and device specific, so I can't give you any better answers in this case off-hand, but here's some examples from a system of mine with FC storage:

$ readlink /sys/block/sda
../devices/pci0000:00/0000:00:02.0/0000:09:00.0/0000:0a:00.0/0000:0b:00.0/host2/rport-2:0-0/target2:0:0/2:0:0:1/block/sda
$ readlink /sys/block/sda/device
../../../2:0:0:1

That is *much* easier to parse...