5

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.

0

5 Answers 5

9

As simple as possible:

xargs whatis < cmdlist.txt > cmdinfo.txt
1
  • 1
    Or whatis $(< cmdlist.txt) > cmdinfo.txt using bash and avoiding xargs.
    – MvG
    Jul 14, 2014 at 7:47
5

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
10
  • don't parse the output of cat command. Jul 13, 2014 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 :)
    – Lety
    Jul 13, 2014 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. Jul 13, 2014 at 11:22
  • Useless use of cat Jul 13, 2014 at 11:25
  • Sorry, I don't use awk in my answer, where you find it?
    – Lety
    Jul 13, 2014 at 11:26
3

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
7
  • 1
    I appreciate using for
    – Pandya
    Jul 13, 2014 at 9:42
  • Your welcome - note other answers such as @xiaodongjie did (you could upvote them / choose those instead :)
    – Wilf
    Jul 13, 2014 at 9:46
  • try to avoid parsing the output of cat command. Jul 13, 2014 at 11:11
  • @Pandya while command is mainly used to read a file line by line. See the update of @Letizia's answer. Jul 13, 2014 at 11:12
  • 2
    Useless use of cat Jul 13, 2014 at 11:26
2

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).

2

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.

9
  • 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, 2014 at 9:42
  • okay muru, please read again his question. Jul 13, 2014 at 9:45
  • He requires to process "." string in his sample text. Jul 13, 2014 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, 2014 at 9:49
  • muru, okay, I already thought it as the simple way, But it remove "." line. Jul 13, 2014 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .