Is there a command or bash function to check if a file has multiple lines? Lets say i have these two files:
foo.txt
Hello World
bar.txt
Hello
World
Is there a command or bash function to check if a file has multiple lines? Lets say i have these two files:
foo.txt
Hello World
bar.txt
Hello
World
You can use this script:
name:test.sh
#!/bin/bash
back=`wc -l $1`
nl=${back:0:1}
if [ $nl = "1" ];then
echo "single line"
else
echo "multi lines"
fi
use this command to change the file mod:
chmod a+x test.sh
Then execute the script,paramater is the file you want to test:
./test.sh hello
I solved it with this command:
wc -l foo.txt | awk '{ print $1 }'
Output: 1
wc -l bar.txt | awk '{ print $1 }'
Output: 2
This shows the number of lines, in a script I can check wheter this number is bigger than 1 or not and my problem is solved.
You could do something like this:
cat foo.txt | wc -l
This would read the file and return a count of the lines within the file.
cat
- read the file
piped into wc
(word count) -l
(No. of lines)