Hi all,
I use bash shell and I have a problem with wc.
I would like to determine the number of lines in a file so I do
wc -l filename
but I don't want to get the filename again
I just would like to have the number of lines and use it in a variable.
Can anybody help?
Thank you,
#!/bin/bash
lines=$(wc -l < myFile)
echo "lines->[${lines}]"
It works, thank you.
I have another problem.
LINES=$\(wc -l < filename\)
echo $LINES
LINES=\`expr $\{LINES\} - 1\`
echo $LINES
but I cannot decrement by 1.
I get the following
12484
expr: non-numeric argument
#!/bin/bash
typeset -i lines
lines=$(( $(wc -l < /tmp/a.txt) - 1 ))
echo "lines-1 = [${lines}]"
sorry but I get
lines-1 = [12482]
': not a valid identifiers
works for me - don't see any ':' in the code.
make sure your code is not 'polluted'....
By using a tail command, you can skip to the 2nd line of a file
> wc -l <file110
21
> tail +2 file110 | wc -l
20
> lines=$(wc -l <file110)
> echo $lines
21
> lines=$(tail +2 file110 | wc -l)
> echo $lines
20
Or, you can use bc as a calculator
> lines=$(wc -l <file110)
> echo $lines -1 | bc
20
> newlines=$(echo $lines -1 | bc)
> echo $newlines
20
thanks a lot. a dos2unix did the job!