sed不工作在Linux Centos,Ubuntu的,

我有一个简单的脚本,给我一些文件的值。 我正在使用sed来获取该值(语法在下面给出)。 这些命令一直工作到昨天。 但现在我没有得到任何价值,当我运行这些命令。 我没有改变任何东西,所以我很惊讶,是什么原因。 任何人都可以请告诉我如何debugging我的问题? 下面是文件的文字:

May 1 11:59:31 box2 kernel: usb 1-3: new high speed USB device using ehci_hcd and address 24 May 1 11:59:31 box2 kernel: usb 1-3: New USB device found, idVendor=0411, idProduct=0105 May 1 11:59:31 box2 kernel: usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=5 May 1 11:59:31 box2 kernel: usb 1-3: Product: USB-SATA Bridge May 1 11:59:31 box2 kernel: usb 1-3: Manufacturer: BUFFALO May 1 11:59:31 box2 kernel: usb 1-3: SerialNumber: 00001412AA38 May 1 11:59:31 box2 kernel: usb 1-3: configuration #1 chosen from 1 choice May 1 11:59:31 box2 kernel: scsi27 : SCSI emulation for USB Mass Storage devices May 1 11:59:38 box2 kernel: scsi 27:0:0:0: Direct-Access BUFFALO External HDD PQ: 0 ANSI: 2 CCS May 1 11:59:38 box2 kernel: sd 27:0:0:0: Attached scsi generic sg6 type 0 May 1 11:59:38 box2 kernel: sd 27:0:0:0: [sdf] 976773168 512-byte logical blocks: (500 GB/465 GiB) May 1 11:59:38 box2 kernel: sd 27:0:0:0: [sdf] Write Protect is off May 1 11:59:38 box2 kernel: sd 27:0:0:0: [sdf] Assuming drive cache: write through May 1 11:59:38 box2 kernel: sd 27:0:0:0: [sdf] Assuming drive cache: write through May 1 11:59:38 box2 kernel: sdf: sdf1 May 1 11:59:38 box2 kernel: sd 27:0:0:0: [sdf] Assuming drive cache: write through May 1 11:59:38 box2 kernel: sd 27:0:0:0: [sdf] Attached SCSI disk 

脚本是:

  SERIAL=$(sed -n '5s/A.*: //p' filename) SIZE=$(sed -n '10s/A.*: //p' filename) MOUNT=$(sed -n '14s/A.*: //p' filename) 

我只能假设你在四月份在大写字母A上匹配,现在已经不在,因为现在是五月份。

也许而不是'A',你应该使用'^'。

以及@wfaulk所说的,如果提供的文本是你使用的那么数字应该是6,11,15。

 SERIAL=$(sed -n 's/.*SerialNumber: \(.*\)/\1/p' filename) SIZE=$(sed -n 's/.*logical blocks: \(.*\)/\1/p' filename) 

可能会更可靠。 尽pipe给出的数据我看不到更好的获取挂载点的方法。

从评论

 MOUNT=$(sed -n 's/.* sd[az]: \(.*\)/\1/p' filenaem) 

日志文件中的东西可能因日志旋转而消失。 上次我不得不这样做,我发现lshw工具非常有用。 例如, lshw -class disk -quiet将满足您的需求。 lshw默认在ubuntu上可用,也可以用于centos / redhat( http://www.ducea.com/2006/06/03/install-lshw-on-rhel-fedora-centos/ )。

我知道这是无关紧要的,但是我希望这对于OP是有用的。