Create the equivalent of the command WC


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Create the equivalent of the command WC
# 1  
Old 08-12-2013
Create the equivalent of the command WC

hi all, i'm trying to do this exercise, i want to create a script that can substitute WC command in unix, can someone help me?
# 2  
Old 08-12-2013
Hello Marina,

Here is the simple example for counting the lines.
Lets say we have a test file in which we have 2309 lines.


Code:
$ cat test.ksh
i=0
while read line
do
let "i = i +1"
 
done < "test"
echo "lines in file are:"$i
$

Actual comand will also give same lines only as follows.

Code:
$ cat test | wc -l
2309


Thanks,
R. Singh

Last edited by RavinderSingh13; 08-12-2013 at 07:59 AM.. Reason: test file info.
# 3  
Old 08-12-2013
with awk

Code:
awk 'END{print NR}' file

# 4  
Old 08-12-2013
shell doesn't recognize let command
# 5  
Old 08-12-2013
Please try the following code now it should work.

Code:
$ cat test.ksh
i=0
one=1
while read line
do
i= $(($i + $one)) 
done < "test"
echo "lines in file are:"$i
$

Thanks,
R. Singh
# 6  
Old 08-12-2013
now it works, thank you
# 7  
Old 08-12-2013
Quote:
Originally Posted by RavinderSingh13
Code:
$ cat test.ksh
i=0
one=1
while read line
do
i= $(($i + $one)) 
done < "test"
echo "lines in file are:"$i
$

Ignoring the space following the assignment operator in line 5, this script is still buggy. Any text file with lines ending in a backslash will be counted incorrectly.

If for some reason wc cannot be used (this thread smells like homework, so I am not providing any specifics), a tiny (just a few characters) sed script can count the number of lines. No need for a comparitively long-winded shell script.

Regards,
Alister
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. SCO

What is the equivalent command of sysadmsh in UNIXware7.1.3?

Sir I have read the book SCO Unix System Administrators Guide and I have found the command sysadmsh useful for system administration. I have tried the above command in unixware7.1.3 and I got the message command not found. Currently I am running unixware7.1.3 in VmWare WorkStsation10.0. ... (1 Reply)
Discussion started by: rupeshforu3
1 Replies

2. Windows & DOS: Issues & Discussions

DOS Equivalent of UNIX Command

Hi, The title of this post is a little vague but I couldn't think of what to call it. In Unix you can perform the following command ftp -v IPADDRESS <<END put FILE END In a DOS command prompt, is it possible to do the same kind of thing that the "<<END" does? So for example, ... (4 Replies)
Discussion started by: Ste_Moore01
4 Replies

3. UNIX for Dummies Questions & Answers

A faster equivalent for this sed command

Hello guys, I'm cleaning out big XML files (we're talking about 1GB at least), most of them contain words written in a non-latin alphabet. The command I'm using is so slow it's not even funny: cat $1 | sed -e :a -e 's/&lt;*&gt;//g;/&lt;/N;//ba;s/</ /g;s/>/... (4 Replies)
Discussion started by: bobylapointe
4 Replies

4. Shell Programming and Scripting

Create a .sh file for an equivalent Excel VBA code

Hi guys, I am new to Unix, Need your help here. I have installed cygwin software (Unix) in my computer (Windows vista). Now i want to create a shell script (.sh file)/other script which is equivalent of VBA code (at the bottom) and then put this .sh file into bin directory of c:/cygwin. so... (7 Replies)
Discussion started by: bansalpankaj88
7 Replies

5. UNIX for Dummies Questions & Answers

pfiles command equivalent in Linux

May i know what is the equivalent tool in linux for pflies in solaris. ? (2 Replies)
Discussion started by: mohtashims
2 Replies

6. AIX

smitty equivalent command line command

i know after you do something in smitty via the gui, you can click something in smitty that will show you how to do the same thing via the command line, (not using the gui) can anyone tell me how (2 Replies)
Discussion started by: asyed123
2 Replies

7. UNIX for Advanced & Expert Users

Need svcs equivalent command

Hi, I am new to HP-UX. Can someboby help me with the svcs equivalent command in HP-UX ??? svcs is command that we use in Solaris for service status. I need to get the status of services in HP-UX. Thanks in advance. (2 Replies)
Discussion started by: EmbedUX
2 Replies

8. Shell Programming and Scripting

Equivalent command to 'stat'

Can anyone tell me which is the equivalent command to 'stats' in ksh shell which discribes the file system? Thanks in advance Regards, Im_new (6 Replies)
Discussion started by: im_new
6 Replies

9. Solaris

Solaris equivalent of the ioscan command

What is the Solaris equivalent of the HP-UX "ioscan -funC" command which lists all hardware on the system? (5 Replies)
Discussion started by: etc
5 Replies

10. UNIX for Advanced & Expert Users

Hw to create root-equivalent accounts?

Hi all. After installing ssh on a server, i'd like to create a user with root privileges. My problem is that after creating a user rootssh (uid=0, gid=20, /home/rootshh), i make rootssh's ssh keys. The problem is that normally the ssh-keygen should create the keys under $HOME/.ssh/, and actually... (6 Replies)
Discussion started by: penguin-friend
6 Replies
Login or Register to Ask a Question