Operating System - HP-UX
1836752 Members
2784 Online
110109 Solutions
New Discussion

question on cat <file> |wc -l output redirection

 
SOLVED
Go to solution
Narasimham
Advisor

question on cat <file> |wc -l output redirection

i wanted to store the output of the command
"cat |wc -l" into a avariable COUNT and wanted to use that $COUNT value for some other purpose . Is it possible to do like that ? ..any one can help in doing like that

Thanks in advance

Narasimham

5 REPLIES 5
Volker Borowski
Honored Contributor
Solution

Re: question on cat <file> |wc -l output redirection

Hi,
you need to use reverse quotes:

COUNT=`cat |wc -l`

echo $COUNT

Hope this helps
Volker
A. Clay Stephenson
Acclaimed Contributor

Re: question on cat <file> |wc -l output redirection

Hi:

Method 1:
COUNT=$(cat file|wc -l)

Method 2:
cat file|wc -l | read COUNT

Method 3:
COUNT=`cat file>|wc -l`

If it ain't broke, I can fix that.
Sanjay_6
Honored Contributor

Re: question on cat <file> |wc -l output redirection

Hi,

Try,

COUNT=`cat file_name|wc -l`
echo $COUNT

Hope this helps.

Regds
Darrell Allen
Honored Contributor

Re: question on cat <file> |wc -l output redirection

Hi Narasimham,

A slight variation...

You can simplify all the above answers by using "wc -l file" instead of "cat file | wc -l".

You'll get the same results.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Bill Hassell
Honored Contributor

Re: question on cat <file> |wc -l output redirection

COUNT=$(wc -l file)

Note: The use of reverse quotes (accent grave, back tics) is deprecated these days. That's fancy Unix-speak that means outdated and not recommended. The replacement is $(...) where ... is the command(s) that are to be executed. One of the reasons for not using the back tics is the difficulty is recognizing the very different character. This is especially true in faxes and far too many fonts. Even in the ITRC forums, the proportional font used to display questions and responses makes them look similar: `` ''


Bill Hassell, sysadmin