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.
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.
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.
You can use pattern matching:
[[ $FILE == *$'\n'* ]] && echo More than one line
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With