How to check newline character in file?

Hi All,

I have file with only one record,always be only one record. as like below.
if that line contains newline end of the line..no need to add, if not just add the new line character.

END OF FILE. ROW COUNT: 7

Please help me..

Thanks,

Try:

awk 1 file
sed '' file

Thanks quick reply.. i will try..

---------- Post updated at 03:02 AM ---------- Previous update was at 02:56 AM ----------

i am really appreciate...one more thanks...

you can use

 
wc -l < filename

it will show the output as 2 ( if you have a newline )

1 Like
sed r file

or

if [[ $(od -w10 -c file|tac|sed -n '2s/.*\(..\)$/\1/p') = "\n" ]] ; then echo "$(<file)" ; else echo -en "$(<file)\n" ; fi

@ygemici: why all that bash/ksh juggling :), when this part:

echo "$(<file)" 

would suffice? or (POSIX)

printf "%s\n" "$(cat file)"

yes you're right :slight_smile: , actually just printf was enough instead of if loop :b:

printf "%s\n" "$(cat file)"

That would be 1 if there is a newline, 0 if not...

Hmm.. i dont think so.. i tried in my solaris machine.

 
$ echo "TEST" > test.txt
$ wc -l < test.txt
       1
$ echo "TEST
> " > test.txt
$ wc -l < test.txt
       2
$ printf "TEST" > test.txt
$ wc -l < test.txt
       0
$