LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2004, 07:28 PM   #1
saiz66
Member
 
Registered: Apr 2003
Posts: 225

Rep: Reputation: 30
[c shell] How do I find how many lines a file has?


Question is in topic.. would I use awk? Thanks!
 
Old 10-07-2004, 07:37 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
wc -l <file> | cut -d " " -f 2
or
cat <file> | wc -l

awk 'END{ print NR-1}' <file>


Cheers,
Tink

Last edited by Tinkster; 10-07-2004 at 07:39 PM.
 
Old 10-07-2004, 11:08 PM   #3
saiz66
Member
 
Registered: Apr 2003
Posts: 225

Original Poster
Rep: Reputation: 30
I am not sure if it is your code or something i did but it prints it a little bit weird

#!/bin/csh
#NAME OF SCRIPT: search.scr

if ($#argv != 2) then
echo "Requires two file arguments"
echo "Usage: $0 search_word file_name"
exit 1
endif
if (! -r $argv[2]) then
echo "The file can not be read"
endif
#grep -n $argv[1] $argv[2]

cat $argv[2] | wc -l

awk 'END{ print NR-1}' $argv[2]

co-sun12% cat test.txt
Hi. This is a test message and should be ignored.
co-sun12% ./search.scr something test.txt
1
0




Why is there a tab before the 1 and what is the 0?
 
Old 10-08-2004, 12:08 AM   #4
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
The tab is because of wc command (check out the man). Can be removed by:

cat test.txt |wc -l |tr -d " "

The zero is because of NR-1 (no. of records -1). You should use:

awk 'END{ print NR}' $argv[2]

HTH.
 
Old 10-08-2004, 08:20 AM   #5
saiz66
Member
 
Registered: Apr 2003
Posts: 225

Original Poster
Rep: Reputation: 30
so the cat and the awk command do the same thing? not in general i mean but in that code.
 
Old 10-08-2004, 03:01 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Yes, they do ... I posted them as alternatives.

As for the -1 in the awk ... I put it in because
my test file didn't have a terminating new-line,
and awk output 1 line more than wc ... :)

Which means that (without a more complex
awk script) the cat | wc combo win :)



Cheers,
Tink

Last edited by Tinkster; 10-08-2004 at 03:02 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting lines into a file through shell scripting false-hopes Linux - General 1 10-22-2005 11:39 AM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
Help, help! How to find lines in between searched patterns? wujee Linux - Software 1 01-06-2005 02:52 PM
Shell Script (adding a prefix to output lines) rheza Programming 3 01-04-2005 01:18 PM
shell script to copy lines from a file Warmduvet Programming 2 09-14-2004 09:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration