LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to extract module names and append to modules.autoload.d (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-extract-module-names-and-append-to-modules-autoload-d-753214/)

bianchiaz 09-07-2009 12:29 AM

How to extract module names and append to modules.autoload.d
 
Hello all I am new to the forums. Super noob here trying to learn some command line and the Linux system. I do not have a working system yet. I have been trying to accomplish this simple task all day and I am not having any luck. I have two books on learning the command line and I can not find anything in them for this particular issue. I am sure this can be accomplished using sed but that is way over my head at this time.
I have compiled my own kernel. It is a modular kernel (I made most options as modules) .

This is a Gentoo system and I am at a part in the handbook where I have to put all my modules into /etc/modules.autoload/kernel-2.6

Here is the problem...
I made a text file called my_modules.txt . Than grepped the module directory contents to it. Doing this pulled in the directory structure with it.
Here are the commands I issued.
find /lib/modules/kernel-2.6.30-gentoo-r6 -type f -iname '*.o' -or -iname '*.ko' | grep '.ko$' > my_modules.txt
This gave me all the modules in my new text file. It also gave me the file structure. There are 94 modules. I do not want to type in 94 drivers/modules in the auto load file.

My text file looks like this:
/lib/modules/2.6.30-gentoo-r6/kernel/net/netfilter/module.ko
/lib/modules/2.6.30-gentoo-r6/kernel/net/drivers/module.ko
/lib/modules/2.6.30-gentoo-r6/kernel/net/netfilter/module.ko
and so on and so on 94 times.

I just want to grep the module name into the file without all the extra text in the beginning of the line.
Than I can just do a : cat my_modules.txt | grep 'ko$' > /etc/modules.autoload.d/kernel-2.6 I believe this will append it to the modules.autoload.d/kernel-2.6 file.

I hope this was clear. I have learned a lot today trying to do this but I never accomplished the task I set out to do.
This is my first post. I hope I was clear on this issue.
Thank you,
RJ

colucix 09-07-2009 01:39 AM

Hi and welcome to LinuxQuestions! :)

I can offer two different methods, so that you have new material to study! ;) One is in pure bash and it is the way to read a file line-by-line. It simply uses "parameter substitution" to extract just the module name:
Code:

while read line
do
  echo ${line##*/}
done < my_modules.txt  >> /etc/modules.autoload.d/kernel-2.6

the second one is by using sed, just substituting the first part of the line up to the last slash with... simply nothing!
Code:

sed 's%.*/%%g' my_modules.txt  >> /etc/modules.autoload.d/kernel-2.6
I want to make you note that append something to a file is accomplished by the double redirection sign. If you use a single > the file will be completely overwritten with the new output and the original content will be lost. Another useful method to accomplish the same task of extracting modules names without their path is by the -printf predicate of the find command. Look for it in the manual page and it will be immediately clear. Hope this helps a bit.

bianchiaz 09-07-2009 08:45 AM

Thank you thank you thank you!:hattip:

The bash script worked great! I was going about it all wrong and doing extra steps I see. Although I did learn much more about the grep command in my quest.

I will study your example after I get my system rebooted with the new system running.:study: I found some examples in my book "From bash to Z Shell - Conquering the Command line." I am assuming you are splitting the line up into words than extracting the last word on each line... than appending it to modules.autoload.d file.

I should have a working Linux system in about an hour. Off to make my fstab file.

How do I post my code in the code box I see all the time?

Thanks again,

RJ

Oh, I almost forgot. I was using > instead of >> earlier in my installation. This would explain why my make.conf.example is no longer an example file.:doh: I wanted to add my make.conf file into the make.conf.example file for further tweaking and I hammered it.
I used a single > instead of >> in the command below...:tisk:
# cat /etc/make.conf | grep >> /etc/make.conf.example

bianchiaz 09-07-2009 08:59 AM

I would like to clarify that I did extra steps that were not necessary. I did not need to make a separate text file and grep the lines of text into them. In hind site I see that I just needed to enter the directory where the new modules were placed. Than execute the bash command/script given while substituting mymodules.txt for the original file to extract from.

RJ

colucix 09-07-2009 09:24 AM

Quote:

Originally Posted by bianchiaz (Post 3673016)
I am assuming you are splitting the line up into words than extracting the last word on each line... than appending it to modules.autoload.d file.

Not quite correct. The codes above does not split the line: they just remove the longest part from the front of the line, up to the last slash.
Quote:

Originally Posted by bianchiaz (Post 3673016)
How do I post my code in the code box I see all the time?

Good question. You have two available methods:

1. if you switch to advanced mode (look at the button "Go Advanced" in the quick reply box below this post) then you will see a button marked with #. Just select the text you want to embed in CODE tags and press that button.

2. without switching to advanced mode you can always manually type the [CODE] and [/CODE] tags around the text you want to put inside. When the post will be published (or if you click on "Preview Post" when in advanced mode) the CODE tags will create the box around the text.

Note that this is not just a fancy feature: they are very useful (especially for other memebers reading your pieces of code) because they preserve spacing, indentation and so on. This is not possible in normal text or in QUOTE tags.


All times are GMT -5. The time now is 07:59 PM.