Adding new line at the end of file

Hi

I have few files. For some files the cursor is at the end of last line. For other files, cursor is at the new line at the end.

I want to bring the cursor down to next line for the files that are having cursor at the end of last line

In otherwords, I want to introduce a blank line at the end of line, if it doesn't exist

Here is example.. for test_file cusor is at the end of line3. I want to bring the cursor down to the next line. Then only, I can process that file. Please help me. Feel free to ask me if you unable to understand my requirements

***test_file****

line1
line2
line3
line4

for f in your_files;do sed '${/^$/!s/$/\
/;}' "$f">"$f"_ && mv "$f"_ "$f"
done
  1. There is a new line after s/$/\ !!!
  2. If you have GNU sed you can use the -i option
    instead of temporary files.

Thank you for your response

It is giving me below error

$ sed '$a' test.webM > ./test.webM_
sed: Function $a cannot be parsed.

I want to introduce a new blank line at the end of file only if it doesn't exist

Thank you
Somesh

Yep,
check the corrected version above.

Yes.. it is working fine for the files that doens't have a blank line at the end of file

But, if the file already contains a blank line at the end, the modified file is becoming zero bytes. It shouldn't modify the file, if it already has blank line at the end

Please suggest some thing

$ ls -ltr test2.webM
-rw-r--r-- 1 spamarth users 15010 Nov 5 16:34 test2.webM
$ sed '${/^$/!s/$//;}' test2.webM > ./test2
$ ls -ltr test2.webM test2
-rw-r--r-- 1 spamarth users 15010 Nov 5 16:34 test2.webM
-rw-r--r-- 1 spamarth users 0 Nov 5 16:35 test2
$

I told you the command is:

sed '${/^$/!s/$/\
/;}' filename

and NOT:

sed '${/^$/!s/$//;}'

It works even on my old Solaris :slight_smile:

$ cat file1
line1
line2
line3
line4
$ cat file2
line1
line2
line3
line4

$ sed '${/^$/!s/$/\
> /;}' file1
line1
line2
line3
line4

$ sed '${/^$/!s/$/\
/;}' file2
line1
line2
line3
line4

$