Command output redirection in script not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Command output redirection in script not working
# 1  
Old 05-21-2012
Command output redirection in script not working

I need to count the number of lines in a .txt file and put it in a variable.

I am using the following code

Code:
#!/bin/bash

count = $(wc -l "some file.txt" | awk '{print$1}')
echo $count

It is giving the following error.

Code:
line3: count: command not found

What am I doing wrong here? Smilie
# 2  
Old 05-21-2012
Spacing
Code:
#!/bin/bash  
count=$(wc -l "some file.txt" | cut -d\  -f1) 
echo $count

This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 05-21-2012
Code:
count=$(wc - l < file)

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 05-21-2012
Counting lines by Awk

Code:
awk 'END { print NR }' file


Last edited by Franklin52; 05-21-2012 at 08:38 AM.. Reason: Please use code tags
This User Gave Thanks to pankaj_tarale For This Post:
# 5  
Old 05-21-2012
As Skrynesaver said it is spacing:
Code:
count=$(wc -l < file)

Smilie

--
There are several methods of determining the number of lines in a file, another one for example:
Code:
sed -n $= file

But wc -l is by far the most efficient of the standard Unix utils ( see Alternative for wc -l: Comparison )

--
@Skrynesaver, this does not work universally, since some wc's right-align the number:
Code:
$ wc -l infile |  cut -d\  -f1
$ wc -l infile                  
       4 infile
$ wc -l < infile
       4
$ wc -l infile | awk '{print $1}'
4


Last edited by Scrutinizer; 05-21-2012 at 09:20 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 05-21-2012
Code:
count=`cat some_file|wc -l`
echo $count


Last edited by zaxxon; 05-21-2012 at 10:07 AM.. Reason: code tags
This User Gave Thanks to RobP For This Post:
# 7  
Old 05-21-2012
Quote:
Originally Posted by RobP
Code:
count=`cat some_file|wc -l`
echo $count

This is Useless Use of Cat.
This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command output redirection to file issues

Hi, I have a peculiar issue w.r.t redirecting the command output to a file when using loop. I am redirecting command output to same file in a series of if condition statements, but if one block of if condition statement writes the log to the file , the subsequent block of if condition... (7 Replies)
Discussion started by: ananan
7 Replies

2. Shell Programming and Scripting

>& redirection not working within csh script

I'm having a strange problem with basic >& output redirection to a simple log file in csh. When I run this particular output redirection on the command line, it works, but then when I run the same output redirection command >& in my c shell script, I get a blank log file. Nothing is output to the... (5 Replies)
Discussion started by: silencio
5 Replies

3. Shell Programming and Scripting

Cygwin script log redirection not working well

I have a simple script which will send a curl request and redirect the output to a log file. for i in {1..20} do curl google.com -is >>log.log & echo "request # $i" >> log.log doneAfter it completes the execution, if I run the following command I should see 20 lines because I am printing... (4 Replies)
Discussion started by: heykiran
4 Replies

4. Shell Programming and Scripting

Output redirection of c binary file to a file in shell script is failing

I am struck up with a problem and that is with output redirection. I used all the ways for the redirection of the output of c binary to a file, still it is failing. Here are the different ways which I have used: ./a.out | tee -a /root/tmp.txt 2>&1 ./a.out | tee -a /root/tmp.txt 1>&1 ./a.out |... (2 Replies)
Discussion started by: Maya29988
2 Replies

5. Shell Programming and Scripting

Shell script output redirection question

OS : AIX 6.1 Shell : Korn in the url https://forums.oracle.com/forums/thread.jspa?threadID=361463&tstart=0 I came across a crontab entry example 00 23 * * 1,3,5 <complete shell script path> 1> <log file> 2>&1 From googling , I gathered that 0 - stdin 1 - stdout 2 - stderr I... (5 Replies)
Discussion started by: polavan
5 Replies

6. UNIX and Linux Applications

output redirection command

Dear All, ./waf --run scratch/myfirst > log.out 2>&1 The above is a command line to redirect the output to a file called log.out. what is the 2>&1 part for ? Thank you (2 Replies)
Discussion started by: knowledgeSeeker
2 Replies

7. Shell Programming and Scripting

Read input and output redirection filename within a script

Hello everyone, My requirement is that within a script I need to construct the command line exactly that it was invoked with. For example : sh a.sh arg1 arg2 arg3 < input.txt > output.txt Now within a.sh, I construct a file which has these contents " sh a.sh arg1 arg2 arg3 < input.txt >... (8 Replies)
Discussion started by: hedonist12
8 Replies

8. Shell Programming and Scripting

Help with tar --append command output redirection

I am using tar command to append daily database backups on tape. "tar --append " command help me to do this. But tar --append command does not produce any output on stdout if it succeed. I want the output for that appended command to a log file. This log file should contain only the name of the... (0 Replies)
Discussion started by: pganguly46
0 Replies

9. Shell Programming and Scripting

Help with Output Redirection of a Unix Shell Script

Hi Guys, I have a script for which the stdout and stderr should be redirected to a log file, they should not be printed on the screen. Could you please let me know the procedure to redirect the output of the script to a log file. Thanks in advance. --- Aditya (5 Replies)
Discussion started by: chaditya
5 Replies

10. Shell Programming and Scripting

Asking about shell script input output redirection

Hi, Can anyone please tell me what these lines do? ls >& outfile ls outfile 2>&1 Thanks. (1 Reply)
Discussion started by: trivektor
1 Replies
Login or Register to Ask a Question