Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I need some help writing a regular expression. I'm doing: Code: modprobes=(`modprobe -l`) for filename in "${modprobes[@]}" do echo $filename done This outputs the likes of: /lib/modules/2.6.24-23-generic/ubuntu/net/et131x/et131x.ko /lib/modules/2.6.24-23-generic/ubuntu/net/e1000e-ich9m/e1000e-ich9m.ko /lib/modules/2.6.24-23-generic/ubuntu/net/atl2/atl2.ko ...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    6

    Bash scripting substring

    Hi,
    I need some help writing a regular expression.

    I'm doing:

    Code:
    modprobes=(`modprobe -l`)
    for filename in "${modprobes[@]}"
    do
    echo $filename
    done
    This outputs the likes of:

    /lib/modules/2.6.24-23-generic/ubuntu/net/et131x/et131x.ko
    /lib/modules/2.6.24-23-generic/ubuntu/net/e1000e-ich9m/e1000e-ich9m.ko
    /lib/modules/2.6.24-23-generic/ubuntu/net/atl2/atl2.ko
    /lib/modules/2.6.24-23-generic/ubuntu/net/igb/igb.ko
    /lib/modules/2.6.24-23-generic/ubuntu/acpi/acer_acpi/wmi-acer.ko
    /lib/modules/2.6.24-23-generic/ubuntu/acpi/acer_acpi/acer_acpi.ko
    /lib/modules/2.6.24-23-generic/ubuntu/block/drbd/drbd.ko
    /lib/modules/2.6.24-23-generic/ubuntu/block/iscsitarget/iscsi_trgt.ko
    /lib/modules/2.6.24-23-generic/ubuntu/hwmon/hdaps_ec.ko
    /lib/modules/2.6.24-23-generic/ubuntu/misc/pbe5.ko
    /lib/modules/2.6.24-23-generic/ubuntu/misc/nozomi.ko
    /lib/modules/2.6.24-23-generic/ubuntu/misc/thinkpad_ec.ko
    /lib/modules/2.6.24-23-generic/ubuntu/misc/fsc_btns.ko
    /lib/modules/2.6.24-23-generic/ubuntu/misc/av5100.ko
    /lib/modules/2.6.24-23-generic/ubuntu/misc/ndiswrapper/ndiswrapper.ko

    Instead of echoing this, I want to match the name of the module. So, for /lib/modules/2.6.24-23-generic/ubuntu/misc/ndiswrapper/ndiswrapper.ko I want to echo ndiswrapper.

    Could someone help? I know its pretty basic but I am new to bash and regexes
    Thanks,
    Stephen

  2. #2
    Just Joined!
    Join Date
    Feb 2009
    Posts
    45
    Quote Originally Posted by sroberts82
    I'm doing:

    Code:
    modprobes=(`modprobe -l`)
    for filename in "${modprobes[@]}"
    do
    echo $filename
    done
    This outputs the likes of:

    Code:
    /lib/modules/2.6.24-23-generic/ubuntu/net/et131x/et131x.ko
    /lib/modules/2.6.24-23-generic/ubuntu/net/e1000e-ich9m/e1000e-ich9m.ko
    /lib/modules/2.6.24-23-generic/ubuntu/net/atl2/atl2.ko
    […]
    /lib/modules/2.6.24-23-generic/ubuntu/misc/av5100.ko
    /lib/modules/2.6.24-23-generic/ubuntu/misc/ndiswrapper/ndiswrapper.ko
    Instead of echoing this, I want to match the name of the module. So, for /lib/modules/2.6.24-23-generic/ubuntu/misc/ndiswrapper/ndiswrapper.ko I want to echo ndiswrapper.
    You probably want this:
    Code:
    modprobe -l | sed -e 's/.*\/\(.*\).ko/\1/';
    You can apply «sed» in the same way to your version of the code. But I think the principle should be clear.

  3. #3
    Just Joined! alex1983-0112's Avatar
    Join Date
    Apr 2008
    Location
    Russia
    Posts
    16
    or:

    modprobes=(`modprobe -l`)
    for filename in "${modprobes[@]}"
    do
    awk '{FS="."; print $1;}' `basename $filename`
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...