Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Suppose I have list of commands in text file as below (cat cmdlist.txt):-

cut
lshw
top
awk
sensors

Now I want to get simple information on that command by whatis cut, whatis lshw etc. respectively and print these out-put of whatis <commadnd> in text file say cmdinfo.txt

Desired output of cmdinfo.txt (cat cmdinfo.txt):-

cut (1)              - remove sections from each line of files
lshw (1)             - list hardware
top (1)              - display Linux processes
awk (1)              - pattern scanning and text processing language
sensors (1)          - print sensors information

How do I achieve cmdinfo.txt file with output of whatis for commands from cmdlist.txt Respectively?

This is only sample txt files.

Suggest simple script if needed.

share|improve this question

5 Answers 5

up vote 9 down vote accepted

As simple as possible:

xargs whatis < cmdlist.txt > cmdinfo.txt
share|improve this answer
1  
Or whatis $(< cmdlist.txt) > cmdinfo.txt using bash and avoiding xargs. –  MvG Jul 14 at 7:47

Much simpler one,

$ while read -r line; do whatis "$line"; done < cmdlist.txt > cmdinfo.txt
cut (1)              - remove sections from each line of files
lshw (1)             - list hardware
top (1)              - display Linux processes
awk (1)              - pattern scanning and text processing language
sensors (1)          - print sensors information

Use the below command to write the result into a file.

while read -r line; do whatis "$line"; done < cmdlist.txt > cmdinfo.txt
share|improve this answer
    
don't parse the output of cat command. –  Avinash Raj Jul 13 at 11:14
    
I tested it on lubuntu 12.04 and it works fine. Also your simpler version works. Maybe I don't understand your comment :) –  Letizia Jul 13 at 11:19
    
@Leiizia what did you do is cat file | awk '{print $1}', what i did is awk '{print $1}' file. I think you understand from that. –  Avinash Raj Jul 13 at 11:22
    
Useless use of cat –  Avinash Raj Jul 13 at 11:25
    
Sorry, I don't use awk in my answer, where you find it? –  Letizia Jul 13 at 11:26

Very simply:

for i in $(cat cmdlist.txt); do whatis $i ; done > cmdinfo.txt

This loops through every entry (i) in the output of $(cat cmdlist.txt), and runs a whatis on very entry. Example output:

cut (1)              - remove sections from each line of files
lshw (1)             - list hardware
top (1)              - display Linux processes
awk (1)              - pattern scanning and processing language
sensors (1)          - print sensors information

N.B. even though in many examples you will find i used, you don't have to use it - you can use most plain alphanumeric strings- e.g.:

for jnka127nsdn in $(cat input.txt); do whatis $jnka127nsdn ; done

To do it without parsing cat:

while read -r i ; do whatis $i ; done < cmdlist.txt
share|improve this answer
1  
I appreciate using for –  Pandya Jul 13 at 9:42
    
Your welcome - note other answers such as @xiaodongjie did (you could upvote them / choose those instead :) –  Wilf Jul 13 at 9:46
    
try to avoid parsing the output of cat command. –  Avinash Raj Jul 13 at 11:11
    
@Pandya while command is mainly used to read a file line by line. See the update of @Letizia's answer. –  Avinash Raj Jul 13 at 11:12
2  
Useless use of cat –  Avinash Raj Jul 13 at 11:26

Something like this:

#! /bin/bash
in_file=$1
out_file=$2

while read command
do
    whatis $command 
done < $in_file >> $out_file

I have chosen to leave stderr untouched, since if you have a command in there for which whatis information is not available, you should be made aware of that (and look elsewhere, if needed).

share|improve this answer

I think that you can get properly result with following command.

$ for i in `cat cmdlist.txt`;do whatis $i 2>&1;done | sed "s,: nothing appropriate.,,g" > cmdinfo.txt

In fact,

$ for i in `cat cmdlist.txt`;do whatis $i 2>&1;done

The command, first command's one part will show outputs as following .

cut (1)              - remove sections from each line of files
lshw (1)             - list hardware
top (1)              - display Linux tasks
.: nothing appropriate.
.: nothing appropriate.
.: nothing appropriate.
tr (1)               - translate or delete characters

you can do it with whatis $(cat cmdlist.txt), but it's output includes following lines.

.: nothing appropriate.
.: nothing appropriate.
.: nothing appropriate.

above sed command remove some output lines you needless.

Question is changed as now. If all of the lines of cmdlist.txt can be listed from whatis, Can use following command as the simple way.

whatis `cat 1.txt` 2>/dev/null > cmdinfo.txt

If you just need the lines that can be completely listed from whatis, Can use following command as the simpleway.

whatis `cat 1.txt` > cmdinfo.txt

However you can choose one way of various ones.

share|improve this answer
    
Odd. Since the 'nothing appropriate' line is printed to stderr, why would you pipe stderr to stdin and then strip it away? Better to simple redirect stderr to /dev/null? –  muru Jul 13 at 9:42
    
okay muru, please read again his question. –  xiaodongjie Jul 13 at 9:45
    
He requires to process "." string in his sample text. –  xiaodongjie Jul 13 at 9:46
    
I figured the dots are ellipsis. Even so, you can replace the command with: whatis $i 2>/dev/null >> cmd.txt. No need for the sed. –  muru Jul 13 at 9:49
    
muru, okay, I already thought it as the simple way, But it remove "." line. –  xiaodongjie Jul 13 at 9:53

Your Answer

 
discard

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.