Appending newline character End of File

Hi Gurus,

Need help. I'm a beginner in Unix. I have a requirement, need to add or append newline (\n) character in file.

Sample Data:

1|Main|Test|~#
2|Main|Hello|~#
3|Main|Unix|~#
4|Main|File|~#

Output:

1|Main|Test|~#
2|Main|Hello|~#
3|Main|Unix|~#
4|Main|File|~#\n -- append only for the last line.

Thank You.

Gouri Solleti

sed '${s/$/\n/}' file

Add the -i switch to the sed command to make the actual change to the file.

Hello Gouri,

Here are 2 approaches may help for same.

1st:

awk 'NR==1 || NR==2 || NR==3 {print} NR==4 {print $0"\\n"}' add_new_line_char1
 
Output will be as follows.
1|Main|Test|~#
2|Main|Hello|~#
3|Main|Unix|~#
4|Main|File|~#\n

2nd:

awk '/^4/ {print $0"\\n"} !/^4/' add_new_line_char1
 
Output will be as follows.
 
1|Main|Test|~#
2|Main|Hello|~#
3|Main|Unix|~#
4|Main|File|~#\n

Thanks,
R. Singh

Simplest method use echo ""...

#!/bin/bash --posix
echo "Your example string..."
echo -n "1|Main|Test|~#
2|Main|Hello|~#
3|Main|Unix|~#
4|Main|File|~#" > /tmp/text
hexdump -C < /tmp/text
cat < /tmp/text
echo "Now add a newline at the end..."
echo "" >> /tmp/text
### OR ###
# echo '\n' >> /tmp/text
hexdump -C < /tmp/text
cat < /tmp/text

Results:-

Last login: Mon Dec 30 18:15:02 on ttys000
AMIGA:barrywalker~> chmod 755 add_nl.sh
AMIGA:barrywalker~> ./add_nl.sh
Your example string...
00000000  31 7c 4d 61 69 6e 7c 54  65 73 74 7c 7e 23 0a 32  |1|Main|Test|~#.2|
00000010  7c 4d 61 69 6e 7c 48 65  6c 6c 6f 7c 7e 23 0a 33  ||Main|Hello|~#.3|
00000020  7c 4d 61 69 6e 7c 55 6e  69 78 7c 7e 23 0a 34 7c  ||Main|Unix|~#.4||
00000030  4d 61 69 6e 7c 46 69 6c  65 7c 7e 23              |Main|File|~#|
0000003c
1|Main|Test|~#
2|Main|Hello|~#
3|Main|Unix|~#
4|Main|File|~#Now add a newline at the end...
00000000  31 7c 4d 61 69 6e 7c 54  65 73 74 7c 7e 23 0a 32  |1|Main|Test|~#.2|
00000010  7c 4d 61 69 6e 7c 48 65  6c 6c 6f 7c 7e 23 0a 33  ||Main|Hello|~#.3|
00000020  7c 4d 61 69 6e 7c 55 6e  69 78 7c 7e 23 0a 34 7c  ||Main|Unix|~#.4||
00000030  4d 61 69 6e 7c 46 69 6c  65 7c 7e 23 0a           |Main|File|~#.|
0000003d
1|Main|Test|~#
2|Main|Hello|~#
3|Main|Unix|~#
4|Main|File|~#
AMIGA:barrywalker~> _

Below is the command I used:

sed '${s/$/\n/}' sample.txt

tried

sed '${s/$/\n/}' >sample.txt

giving error as -

sed: command garbled: ${s/$/\n/}

---------- Post updated at 01:24 PM ---------- Previous update was at 01:22 PM ----------

Hi Ravinder,

When I used the code, it is working fine for only 4 lines. If I add additional line it is not working. The number of lines in the file could be anything. Which we don't know until the file is generated.

Thank you.

ok Gouri, could you please let us know at which place or expected output for same so that we can help you more for same.

Thanks,
R. Singh

Try

awk '1;  END{print ""}' file

Another version using a variable:-

#!/bin/bash --posix
echo "Your example string..."
text=$(printf "1|Main|Test|~#
2|Main|Hello|~#
3|Main|Unix|~#
4|Main|File|~#")
printf "$text"
echo "Now add a newline at the end..."
text="$text"'\n'
printf "$text"

Results:-

Last login: Mon Dec 30 18:45:09 on ttys000
AMIGA:barrywalker~> chmod 755 add_nl1.sh
AMIGA:barrywalker~> ./add_nl1.sh
Your example string...
1|Main|Test|~#
2|Main|Hello|~#
3|Main|Unix|~#
4|Main|File|~#Now add a newline at the end...
1|Main|Test|~#
2|Main|Hello|~#
3|Main|Unix|~#
4|Main|File|~#
AMIGA:barrywalker~> _

Hello Gouri,

May be following can help.

a=`awk 'END {print NR}' add_new_line_char1`; awk -vs1="$a" 'NR==s1 {print $0"\\n"} !/^4/' add_new_line_char1

So by this we can save how many number of lines are there in file then we can append the new line character as per your requirement.

Thanks,
R. Singh

Here's a Perl version tested on a file with well over 2000 lines:

perl -ne 'print "$_\n" if eof' file

To add a newline at the end of the file:

xargs -d\n < file

There seem to have been a lot of complex possibilities presented in this thread. Why not use something much simpler, for example:

echo >> file

Thank you Gurus. Was able to achieve this functionality, appending \n to the end of file using Informatica.

Thank you again.

The following will add a \n only if missing:

perl -lpe "" file

Write this back to the file with:

perl -i -lpe "" file