Linux - check if there is an empty line at the end of a file
Olivier Pirson's answer is neater than the one I posted here originally (it also handles empty files correctly). I edited my solution to match his.
In bash:
newline_at_eof()
{
if [[ -s "$1" && -z "$(tail -c 1 "$1")" ]]
then
echo "Newline at end of file!"
else
echo "No newline at end of file!"
fi
}
As a shell script that you can call (paste it into a file, chmod +x <filename>
to make it executable):
#!/bin/bash
if [[ -s "$1" && -z "$(tail -c 1 "$1")" ]]
then
echo "Newline at end of file!"
else
echo "No newline at end of file!"
fi
How to detect file ends in newline?
@Konrad: tail does not return an empty line. I made a file that has some text that doesn't end in newline and a file that does. Here is the output from tail:
$ cat test_no_newline.txt
this file doesn't end in newline$
$ cat test_with_newline.txt
this file ends in newline
$
Though I found that tail has get last byte option. So I modified your script to:
#!/bin/sh
c=`tail -c 1 $1`
if [ "$c" != "" ]; then
echo "no newline"
fi
Why is it recommended to have empty line in the end of a source file?
Many older tools misbehave if the last line of data in a text file is not terminated with a newline or carriage return / new line combination. They ignore that line as it is terminated with ^Z (eof) instead.
How can I test if line is empty in shell script?
Since read
reads whitespace-delimited fields by default, a line containing only whitespace should result in the empty string being assigned to the variable, so you should be able to skip empty lines with just:
[ -z "$line" ] && continue
How do I get the last non-empty line of a file using tail in Bash?
You can use Awk:
awk '/./{line=$0} END{print line}' my_file.txt
This solution has the advantage of using just one tool.
How to add an empty line at the end of these commands?
So how can I add a blank line to the end of the file within the
scripts that convert fastq to fasta?
I would use GNU sed
following replace
cat $1 >> final.fasta
using
sed '$a\\n' $1 >> final.fasta
Explanation: meaning of expression for sed
is at last line ($
) append newline (\n
) - this action is undertaken before default one of printing. If you prefer GNU AWK
then you might same behavior following way
awk '{print}END{print ""}' $1 >> final.fasta
Note: I was unable to test any of solution as you doesnot provide enough information to this. I assume above line is somewhere inside loop and $1
is always name of file existing in current working directory.
to check if a line before and after a string empty
Could you please try following if you are ok with awk
.
awk -v string="Hello" '
FNR==NR{
a[FNR]=$0
next
}
($0==string) && a[FNR-1]=="" && a[FNR+1]==""{
a[FNR-1]=a[FNR]=a[FNR-2]="del_flag"
}
END{
for(i=1;i<=length(a);i++){
if(a[i]!="del_flag"){
print a[i]
}
}
}
' Input_file Input_file
Related Topics
Is There an Equivalent to Dbus on Osx
Why Processes Are Deprived of CPU for Too Long While Busy Looping in Linux Kernel
Developing a Web App with Mono (Opensource .Net) Any Examples
Installing Oracle Instantclient on Linux Without Setting Environment Variables
Execute Bash Script Remotely via Curl
Embedded Linux - Mechanism for Deploying Firmware Updates
In a Bash Script, What Would $'\0' Evaluate to and Why
Sqlite Wal Performance Improvement
What's The Purpose of Mmap Memory Protection Prot_None
Siege Aborted Due to Excessive Socket Failure
Is There Any Difference Between Executable Binary Files Between Distributions
Linux of Equivalent Cryptprotectmemory
"Invalid Arithmetic Operator" in Shell
Can a Pipe in Linux Ever Lose Data
Open Eclipse Juno with Error "No More Handles ..." in Ubuntu 12.04