LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-07-2009, 01:29 AM   #1
bianchiaz
LQ Newbie
 
Registered: Sep 2009
Location: Detroit
Distribution: Fedora 15 with LXDE window manager.
Posts: 7

Rep: Reputation: 0
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

Last edited by bianchiaz; 09-07-2009 at 09:53 AM. Reason: Renamed Title to be more specific.
 
Old 09-07-2009, 02:39 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 09-07-2009, 09:45 AM   #3
bianchiaz
LQ Newbie
 
Registered: Sep 2009
Location: Detroit
Distribution: Fedora 15 with LXDE window manager.
Posts: 7

Original Poster
Rep: Reputation: 0
Thank you thank you thank you!

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. 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. 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...
# cat /etc/make.conf | grep >> /etc/make.conf.example
 
Old 09-07-2009, 09:59 AM   #4
bianchiaz
LQ Newbie
 
Registered: Sep 2009
Location: Detroit
Distribution: Fedora 15 with LXDE window manager.
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 09-07-2009, 10:24 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by bianchiaz View Post
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 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 02:48 PM
Grep command and adding a line Treikayan Linux - Newbie 4 07-03-2009 10:57 AM
Command line text file viewing and editing - .odt etc. mitchellray Linux - Newbie 1 04-14-2009 04:41 PM
Help me in Grep Command + cd command in single line JeiPrakash Linux - Newbie 3 05-27-2008 05:16 AM
Editing at the command line subnet_rx Linux - General 2 01-09-2007 02:12 PM


All times are GMT -5. The time now is 08:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration