nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Script to display number of lines and words from a file

This is a discussion on Script to display number of lines and words from a file within the Shell scripting forums, part of the Development/Scripting category; Hi, I need some help on scripting. I am doing a simple echo of lines and words from a file ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 09-20-2007, 10:17 PM
Junior Member
User
 
Join Date: Sep 2007
My distro: Sun Solaris/Linux
Posts: 7
Rep Power: 0
newbewie is on a distinguished road
Default Script to display number of lines and words from a file

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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-07-2007, 06:32 PM
anilvrathod's Avatar
Junior Member
User
 
Join Date: Dec 2007
Location: Pune
My distro: Red Hat
Posts: 13
Rep Power: 0
anilvrathod is on a distinguished road
Default Shell Scripting .......................

Dear Friend
Qus:-
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.

Ans:

# vi example.sh
#!/bin/bash
# read a file line by line
file=/file_path_for_count
x=0
lns=`wc -l $file`
y=`expr "$lns" : '\([0-9]*\)'`
****or**** use one of them y statement
y=`cat $file | wc -l `

while [ "$x" -lt "$y" ]
do
let x=x+1
head -n $x $file | tail -n 1
done
exit 0

Reply With Quote
  #3 (permalink)  
Old 12-07-2007, 11:37 PM
Junior Member
User
 
Join Date: Sep 2007
My distro: Sun Solaris/Linux
Posts: 7
Rep Power: 0
newbewie is on a distinguished road
Default

Thanks Man

This definetly makes sense.

However this the scenerio.
Let say I have install 100 patches to a system (Sun Machine). And I am using patchadd command , so I would like to see the output of the patchadd command as well as the number of patches it has installed.

In other words count per line with the command

so the out put might look like this:

1 patch "xxxx001" has been installed
2 patch "xxxx002" has been installed
.
.
.
.
100 patch "xxx100" has been installed

so in other words not just echo of line but some command as well

Sincere apologies if I look like a fool but ....
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Display number of processors on linux tom Getting started tutorials 7 08-12-2008 11:51 AM
How to print some lines from a file Prahlad Shell scripting 3 04-24-2008 12:25 AM
UNIX Display or list number of files in a current directory raj Getting started tutorials 1 12-07-2007 11:55 AM
Calculations across different lines & columns of a file Guest Shell scripting 2 09-16-2005 05:18 AM
searching for bad words script marinm Shell scripting 1 02-03-2005 05:32 PM


All times are GMT +5.5. The time now is 05:52 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36