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
add comment

5 Answers

up vote 7 down vote accepted

As simple as possible:

xargs whatis < cmdlist.txt > cmdinfo.txt
share|improve this answer
    
Or whatis $(< cmdlist.txt) > cmdinfo.txt using bash and avoiding xargs. –  MvG 4 hours ago
add comment

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 yesterday
    
I tested it on lubuntu 12.04 and it works fine. Also your simpler version works. Maybe I don't understand your comment :) –  Letizia yesterday
    
@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 yesterday
    
    
Sorry, I don't use awk in my answer, where you find it? –  Letizia yesterday
show 5 more comments

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
add comment

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 yesterday
    
okay muru, please read again his question. –  xiaodongjie yesterday
    
He requires to process "." string in his sample text. –  xiaodongjie yesterday
    
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 yesterday
    
muru, okay, I already thought it as the simple way, But it remove "." line. –  xiaodongjie yesterday
show 4 more comments

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 yesterday
    
Your welcome - note other answers such as @xiaodongjie did (you could upvote them / choose those instead :) –  Wilf yesterday
    
try to avoid parsing the output of cat command. –  Avinash Raj yesterday
    
@Pandya while command is mainly used to read a file line by line. See the update of @Letizia's answer. –  Avinash Raj yesterday
1  
show 2 more comments

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.