nixCraft Linux Forum

nixCraft

Linux / UNIX 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

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 20-09-2007, 09:17 PM
Junior Member
User
 
Join Date: Sep 2007
OS: Sun Solaris/Linux
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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
  #2 (permalink)  
Old 07-12-2007, 05:32 PM
anilvrathod's Avatar
Junior Member
User
 
Join Date: Dec 2007
Location: Pune
OS: Red Hat
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
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 07-12-2007, 10:37 PM
Junior Member
User
 
Join Date: Sep 2007
OS: Sun Solaris/Linux
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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


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 Off


Similar Threads

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


All times are GMT +5.5. The time now is 10:41 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

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 37 38