Skip to content
Advertisement

How to grep a particular pattern

I am trying to get a particular pattern, but i get the result for all possible solutions

ps -eo pid,cmd | grep  "qemu.*-name lubuntu" | grep -v grep | awk '{print   $1}'

I did try all the solutions available here

ps -eo pid,cmd | grep "qemu.*-name lubuntu" | grep -v grep                                                                 
  451 qemu-system-x86_64 -enable-kvm -name lubuntu-clone -S -machine pc-i440fx-trusty,accel=kvm,usb=off -cpu SandyBridge,+osxsave,+pcid,+pdcm,+xtpr,+tm2,+est,+smx,+vmx,+ds_cpl,+monitor,+dtes64,+pbe,+tm,+ht,+ss,+acpi,+ds,+vme -m 2048 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid a0b6cbec-3223-fa5e-c6d6-ab88d5736dac -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/lubuntu-clone.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -no-shutdown -boot strict=on -device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 -drive file=/var/lib/libvirt/images/lubuntu-clone.img,if=none,id=drive-ide0-0-0,format=raw -device ide-hd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0,bootindex=1 -drive if=none,id=drive-ide0-1-0,readonly=on,format=raw -device ide-cd,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0 -netdev tap,fd=26,id=hostnet0 -device rtl8139,netdev=hostnet0,id=net0,mac=52:54:00:82:d8:6b,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -vnc 127.0.0.1:0 -device cirrus-vga,id=video0,bus=pci.0,addr=0x2 -device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x5
  467 qemu-system-x86_64 -enable-kvm -name lubuntu -S -machine pc-i440fx-trusty,accel=kvm,usb=off -cpu SandyBridge,+osxsave,+pcid,+pdcm,+xtpr,+tm2,+est,+smx,+vmx,+ds_cpl,+monitor,+dtes64,+pbe,+tm,+ht,+ss,+acpi,+ds,+vme -m 2048 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid 9d9b5a31-53ce-113b-9a1c-cc0308fcb174 -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/lubuntu.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -no-shutdown -boot strict=on -device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 -drive file=/var/lib/libvirt/images/lubuntu.img,if=none,id=drive-ide0-0-0,format=raw -device ide-hd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0,bootindex=1 -drive if=none,id=drive-ide0-1-0,readonly=on,format=raw -device ide-cd,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0 -netdev tap,fd=26,id=hostnet0 -device rtl8139,netdev=hostnet0,id=net0,mac=52:54:00:43:ed:de,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -vnc 127.0.0.1:1 -device cirrus-vga,id=video0,bus=pci.0,addr=0x2 -device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x5

How do I get only the PID: 467?

Advertisement

Answer

If your goal is to grep only for “lubuntu”, not “lubuntu-somethingelse”, then you can add a space afterwards:

ps -eo pid,cmd | grep "qemu.*-name lubuntu " | grep -v grep | awk '{print $1}'

Or without the extra commands:

pgrep -f "qemu.*-name lubuntu "

Edit: If you need something reliable and general-purpose, not for your own script, then parsing the commandline is always going to be a bad idea. There are too many things that can go wrong. Since you’re using libvirt for managing the VMs, have a look at scripting virsh instead.

Edit2: So you can either get the pid by getting the pid attribute from the domstatus node in /var/run/libvirt/qemu/lubuntu.xml, or get the same information via the virsh desc --live lubuntu.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement