You are not logged in.

#1 2007-12-24 19:55:14

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

grep to search and extract

Any simpler and better way to do this task to extract a selected device configures in xorg.conf between Section and EndSection. In this sample the mouse. This what I did. It works but will not be accurate with all styles of xorg.conf(s)

mouse="mouse"

# Count how many lines between 'Section "InputDevice"' and 'EndSection'
SEARCH=`grep Driver $ConfigFILE | grep $mouse`
grep -A 50 -B 5 "$SEARCH" $ConfigFILE | grep -A 50 -B 5 "$SEARCH" | grep -A 50 InputDevice >/tmp/search.hwd
n=""
cat /tmp/search.hwd | while read LINE; do
if [ "$LINE" = "EndSection" ]; then
exit 1
else
n=$((n+1))
echo "$n" >/tmp/num.hwd
fi
done

#Extract
n=`cat /tmp/num.hwd`
SEARCH=`grep Driver $ConfigFILE | grep $mouse`
MTEXT=`grep -A $n -B 5 "$SEARCH" $ConfigFILE | grep -A 50 InputDevice | grep -B 50 EndSection`
echo "$MTEXT" >/tmp/hwd.tmp

The result (/tmp/hwd.tmp) would be something like this:

Section "InputDevice"
    Identifier  "Serial Mouse"
    Driver      "mouse"
    Option      "Protocol" "Microsoft"
    Option      "Device" "/dev/ttyS0"
    Option      "Emulate3Buttons" "true"
    Option      "Emulate3Timeout" "70"
    Option        "SendCoreEvents"  "true"
EndSection

Section "InputDevice"
    Identifier  "PS/2 Mouse"
    Driver      "mouse"
    Option      "Protocol" "auto"
    Option          "ZAxisMapping"          "4 5"
    Option      "Device" "/dev/psaux"
    Option      "Emulate3Buttons" "true"
    Option      "Emulate3Timeout" "70"
    Option        "SendCoreEvents"  "true"
EndSection

Section "InputDevice"
        Identifier      "USB Mouse"
        Driver          "mouse"
        Option          "Device"                "/dev/input/mice"
    Option        "SendCoreEvents"    "true"
        Option          "Protocol"              "IMPS/2"
        Option          "ZAxisMapping"          "4 5"
        Option          "Buttons"               "5"
EndSection

One of the shortcomes in this solution, if example "PS/2 Mouse" has more lines between the Sections than the Serial Mouse" cases a problem.


Markku

Offline

#2 2007-12-24 21:36:13

skymt
Member
Registered: 2006-11-27
Posts: 443

Re: grep to search and extract

This seems to go beyond what grep was meant to do, but I was able to come up with a working solution in two passes of awk. I'm not an awk expert, so it isn't very pretty, but it seems to work well enough.

awk '/Section/, /EndSection/ {sub(/EndSection/, "EndSection\n:"); print}' /etc/X11/xorg.conf | awk 'BEGIN {RS="\n:\n"} /mouse/' > /tmp/hwd.tmp

Offline

#3 2007-12-25 01:35:41

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: grep to search and extract

awk '{RS=""} {ORS="\n\n"} /Driver *"mouse"/' /etc/X11/xorg.conf >/tmp/hwd.tmp

...should be enough wink

Last edited by ibendiben (2007-12-25 02:00:08)

Offline

#4 2007-12-25 02:07:33

skymt
Member
Registered: 2006-11-27
Posts: 443

Re: grep to search and extract

awk '{RS=""} {ORS="\n\n"} /Driver[[:blank:]]*"mouse"/' /etc/X11/xorg.conf >/tmp/hwd.tmp

Yeah, that's much better. I thought there would be a simpler way! I missed the null RS case in the awk manpage. I made one minor edit to allow for the use of tabs, but otherwise it looks fine.

Offline

#5 2007-12-25 07:00:09

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: grep to search and extract

Thanks for the solutions. Simplified and more accurate indeed.
To extract whole default config from xorg log, in awk how to. The first line begins with Section and last line EndSection.

grep -w -A 0 -B 15 EndSection /var/log/Xorg.0.log | grep -w -A 30 Section > xorg.conf

Markku

Offline

#6 2007-12-25 15:59:08

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: grep to search and extract

rasat, I like your unix-lore puzzles! I was too slow on this one. Maybe for your troubles you should find the code in x.org which parses xorg.conf, and build from there! It would be easier on the brain than trying to grep/sed/awk your way out of things. But then again, grep, sed and awk one-liners are so much fun!

Offline

Board footer

Powered by FluxBB