Need some advise on two for loops to display and to number

--------------------------------------------------------------------------------

Hi,
I need some help on scripting.
I am doing a simple echo of lines and words from a file and I also want to echo the number of lines and words to it.
Where can I get this help
A simple script I am using is
for i in `cat hostfile`
do
echo $i
done
And what I would like to do is something like this:
for i in `cat hostfile`
do
for j in ((j=1;j<=1000;j++))
do
echo $i
echo $j
done
Thanks a lot in advance

--------------------------------------------------------------------------------

To make it more clear, say a file has these names

AAA
BBB
CCC
DDD

so the desired out put could be :

1 AAA
2 BBB
3 CCC
4 DDD

You can use the nl utility to obtain the output you want.

Newbewie,

I might not be clear on what you're asking for but it sounds like you want the output to be something like prefixing each line with it's line number and it's "word" count.

If that's the case something as simple as:

#!/bin/bash
for arg; do
    ARGC="$#"
    file="$1"
    linecount=0
    while IFS="" read line; do
        set -- $line "$@"
        echo "$linecount $(( $# - $ARGC )) $line"
        let linecount+=1
        shift $(( $# - $ARGC  ))
        done  < "$file"
    shift
    done

This assumes that you're going to call it with a list of files to process as described. More sophisticated argument handling could handle switches, or could use standard input if $# starts empty. or do other stuff.

One tricky part of this script is that it keeps using the positional argument list as a way to quickly count the "words" in a line ... and restores the argument list after each line. It's only virtue in all this is that it uses no external commands. It's all shell built-ins.

Another trick here is the use of IFS="" just before the read command. Note that this is only setting the IFS for the scope of the read command itself, not affecting the IFS value for the set and other commands. (Without that bit of hackery the read command ends up stripping leading and trailing whitespace and any internal sequences of whitespace are squashed down to single instances of one space (assuming the normal IFS settings).

The awk that might work just as well would be much shorter:

awk '{ print FNR, NF, $0 }'

There are likely to be differences in word counts between these due to differences in parsing lines into "words." I know that this will usually differ from the output of the wc command. Obviously you may have to use various sorts of regexp or other manipulations depending on what you mean by "words."

JimD
(former Linux Gazette AnswerGuy)

Hi Jim,
This is indeed what I was looking for. However what i get know is like this:

0 1 AAA
1 1 BBB
2 1 CCC

I believe some thing could be done

Thanks a lot for this wonderful help though

cat -n file

Thanks

I am running a for loop to execute a command to , say 100 hosts, but I don't know how many I have done so far or how many are left

Will it make any sense?

Newbewie,

I'm sorry to say that you're not making any sense to me. You say you're executing a command on a 100 hundred hosts and then your message talks about wanting to number the lines in a file (does that file contain a list of hostnames?). In your original message it looked like you were trying to say something about counting the current line number and the number of words on that line (or something that would involve printing each line prefixed by *two* numbers.

So, I really don't know what you're trying to do. If you just want something like a count down then you could use something like this:

 # assume file of hostnames is $1; rest of args contains command to be issued
 # to each of them and any switches to the ssh command
 hostfile="$1"
 shift
 numLines=$( wc -l $hostfile | { read x, c; echo $c;})
 donecount=1
 while read hostname; do
     echo "Executing $* on $hostname ($donecount of $numLines)"
     ssh $hostname "$@"
     let donecount += 1
     done

Where this gives you a prevew of which host is about to be contacted with a summary of how many have been done and how many there were, total, when the process first read the file?

Perhaps investing a bit more effort on posting a clear description of what you're trying to do would help.

JimD (former Linux Gazette AnswerGuy)

Hi there,

if your trying to simply print your file with numbers on each line try this:

#!/bin/bash

set -- $( cat inputfile )

while [ "$int" -le "$lineno" ]
do
eval line=$1
echo "$int ) $line" >> usernameoutput
int=$(( $int + 1 ))
shift
done

outputt is:

1 ) aaa
2 ) bbb
3 ) ccc