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


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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
# 2  
You can use the nl utility to obtain the output you want.
# 3  
Line number with word count for every line?

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:

Code:
#!/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:

Code:
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)
# 4  
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
# 5  
Code:
cat -n file

# 6  
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?
# 7  
Question Making sense

Quote:
Originally Posted by newbewie
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:

Code:
 # 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)
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #669
Difficulty: Medium
802.11 technology has its origins in a 1985 ruling by the U.S. FCC that released the ISM band for unlicensed use.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Advise on how to print range of lines above and below a number?

Hi, I have attached an output file which is some kind of database file mapping. It is basically like an allocation mapping of a tablespace and its datafile/s. The output is generated by the SQL script that I found from 401 Authorization Required Excerpts of the file are as below: ... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

Display the last part of a number list

Hi , i have a file wich have 50+ of numbers like : 0.014544106 0.005464263 0.014526045 0.005484374 0.014539412 0.005467600 0.014558349 0.005452185 i would like to display the list from the 6th bit to the end for example 0.005452185 (should become) 2185. I've tried with ... (4 Replies)
Discussion started by: Board27
4 Replies

3. UNIX Desktop Questions & Answers

How to display only the lines from a file which do not contain a given number

a. How do I display the content of the file containing what Ive merged using a filter which would display only the lines of the file which don't contain number, for example 3 or 6. (3 Replies)
Discussion started by: herberwz
3 Replies

4. Shell Programming and Scripting

problem with floating point number loops

Hey, I guess I am just to stupid and am not seeing the "wood for the trees", but I am always getting strange errors. I want to create a mesh with coordinates like: x y z 3.1 3.0 0.75 0 0 1 3.1 2.9 0.75 0 0 1 3.1 2.8 0.75 0 0 1 3.1 2.7 0.75 0 0 1 3.0 ... (10 Replies)
Discussion started by: ergy1983
10 Replies

5. Shell Programming and Scripting

Display string at line number

I have a code here , which should display lines 6,10,14,18,35 of a text file #!/bin/ksh line=6 line=10 line=14 line=18 line=35 for i in 1 2 3 4 5 do val=`echo ${line}` act=`awk 'NR~/^($val)$/' db_CHECKOUT.txt` done; This code is not working. The purpose of the line below is... (3 Replies)
Discussion started by: njafri
3 Replies

6. Shell Programming and Scripting

display unique number

Hi, how i can display all the unique number from my random number script below; #!/usr/bin/perl use strict; my @alphanum = ( 'A' .. 'Z', 'a' .. 'z', 0 .. 9); my $random = join('', map($alphanum,(1..5))); print "$random\n"; Thank You. (1 Reply)
Discussion started by: malaysoul
1 Replies

7. Solaris

Display Serial Number

Hello, I am running Solaris 9 and I need to display the serial number of my machine. How can I do this? Here is my machine info: SunOS birch 5.9 Generic_118558-09 sun4u sparc SUNW,Sun-Fire-V240 Thank you, David (5 Replies)
Discussion started by: dkranes
5 Replies

8. Shell Programming and Scripting

display number of subdirectories

Can anyone tell me what command would display the number of subdirectories in the current or given location? (2 Replies)
Discussion started by: jjamd64
2 Replies

9. Programming

how i display number in words

helo i want to implement the following concept in my project write a c/c++ algorithm for : accept a number from the user not greater than 6 digits and display the number in words i.e. if the input from the user is 18265 then the output should be Eighteen Thousand Two Hundred Sixty Five. if the... (3 Replies)
Discussion started by: amitpansuria
3 Replies

10. UNIX for Dummies Questions & Answers

How to use ps to display processor number

I'm on a Unix 5.2 server and I want to be able to see my processes to verify they are active and which processor they are running on. ps -l will show me the status of process (active/stopped/idle) but to see which processor the process is assigned to I don't know how. Manpages show -o... (1 Reply)
Discussion started by: tumblez
1 Replies

Featured Tech Videos