Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure only one file is returned by checking for newline

Tags:

bash

unix

newline

What is the simplest way to check if a string contains newline?

For example, after

FILE=$(find . -name "pattern_*.sh")

I'd like to check for newline to ensure only one file matched.

like image 621
Valentin Milea Avatar asked Jan 27 '12 12:01

Valentin Milea


People also ask

Should all files end with a newline?

So, it turns out that, according to POSIX, every text file (including Ruby and JavaScript source files) should end with a \n , or “newline” (not “a new line”) character. This acts as the eol , or the “end of line” character.

Why is New Line required at end of file?

If however you have a text file format where you require the newline, you get simple data verification very cheap: if the file ends with a line that has no newline at the end, you know the file is broken.


1 Answers

You can use pattern matching:

[[ $FILE == *$'\n'* ]] && echo More than one line
like image 64
choroba Avatar answered Nov 15 '22 18:11

choroba