7

I am trying to do something like this:

  if [ $(wc -l $f) -lt 2 ]

where $f is a file. When I run this, I get the error message:

  [: too many arguments

Does anybody know how to fix this line in my command?

The full script is:

for f in *.csv
do
  if [ $(wc -l $f) -lt 2 ]
      then
      echo $f
      fi
done

3 Answers 3

10

at least in my case wc -l filename does output 32 filename being 32 the number of lines. so you must strip of the filename after the line count. You could change your code from

if [ $(wc -l $f) -lt 2 ]

to

if [ $(wc -l $f | cut -f1 -d' ') -lt 2 ]

or

if [ $(wc -l < $f) -lt 2 ]

If does not solve your problem please add the output of wc -l filename to your question or as a comment.

Sign up to request clarification or add additional context in comments.

Comments

3

Try this :

  if (( $(wc -l < "$f") < 2 ))

or if you want to keep your syntax :

  if [ $(wc -l < "$f") -lt 2 ]

Note

((...)) is an arithmetic command, which returns an exit status of 0 if the expression is nonzero, or 1 if the expression is zero. Also used as a synonym for "let", if side effects (assignments) are needed. See http://mywiki.wooledge.org/ArithmeticExpression

Comments

0

What you need is probably

if [ `cat $f | wc -l` -lt 2 ]

instead of

if [ $(wc -l $f) -lt 2 ]

2 Comments

No, this does not work, it has the same error msg when I run this command.
@user788171 Edited the answer. Check now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.