-3

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
Black
  • 18,150
  • 39
  • 158
  • 271

3 Answers3

1

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
hongbochen
  • 123
  • 6
1

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.

Black
  • 18,150
  • 39
  • 158
  • 271
0

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)

thonnor
  • 1,206
  • 2
  • 14
  • 28