Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Basically, I'm wanting to automate adding something to xorg.conf in the right place, I've used some commands to get the line number of the line I want to manipulate, but I'm not really sure how to go about passing this line number (as an argument and NOT something to be manipulated) to sed. I have been told about xargs and looked at the docs on it, but after some reading and experimentation I can't seem to get it to work.

In case anyone can think of a better method entirely, the process I want to automate is just finding the line containing both "Identifier" and "Monitor0" (there will only be one) and adding a line below it. The problem with just finding Monitor0 and manipulating that line is that there are multiple lines with Monitor0 in.

I've got this far:

fgrep -n "Monitor0" </etc/X11/xorg.conf | fgrep "Identifier" | cut -f1 -d:

This gives out the line number which I'm wanting to pass to sed, but I'm not really sure how to do it.

...or is there a simpler way which I'm not seeing?

share|improve this question

I can think of a framework to do this in a perl script, but i don't have that have experience with sed so you would have to take core of the details.

Write a perl script which first identifies the line (not the line number) at which you have to append. (Something like: $line = system("fgrep -n ..."); ) Then run sed like this:sed s/$line/$line.'\n'.$linetoadd/ <$oldfile >$newfile.

share|improve this answer
Sadly I don't know perl well enough to confidently do that, and I'd rather just have it as a command. I'm sure it can be done with xargs, I'm just not experienced enough to work out how. – Matthieu Cartier Dec 21 '10 at 12:16
up vote 0 down vote accepted

Sorted it (thanks #cogs on SynIRC), solution for this particular issue is as follows:

sed '/\bIdentifier\b.*\bMonitor0\b/ a\\tOption "NoDDC"'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.