I need to remove only the last (blank space) from the file. When I try wc -l the file name,the number of lines coming is 3 only, however blank space is there in the file.
As I mentioned in my mail when I take the count of the file, it is giving me the value without the next line. However when I open the file in vi mode the cursor goes till the line 'ijkl'. This is looking strange. Any suggestions regarding the file?
Maybe the last line of your file has control/non-printing character(s).
Try the following command and check the ASCII code of the character(s) in the last line.
I am sure that if you follow Durden's suggestion, the last character contained in the file as printed by the od command will not be a newline.
From the symptoms that have been described, it is clear that this is not a text file (because the last character of a non-empty text file must be a newline character and wc -l would return 4 instead of 3). Since there are characters following the last newline character in this file, the behavior of awk, ed, sed, vi, and many other utilities is unspecified. On some implementations these utilities may add a newline to the end of the file (with or without a diagnostic or warning message), may ignore all characters following the last newline character in the file, (although probably less likely) do absolutely anything the programmer thought would be fun in this case, or (more likely) do something the programmer never considered (because (s)he never considered the possibility that the input might not be a properly formed text file).
One reliable way on any Linux or UNIX system to delete any characters following the last newline whether or not there are any is:
echo "" >> filename
printf '$d\nw\nq\n' | ed -s filename
However, even this is only guaranteed to work if there aren't any NUL bytes in the file and there aren't more than (LINE_MAX - 1) consecutive bytes in the file that do not contain a newline character. You should be able to get the value of LINE_MAX on your system by running the command:
A portable approach that can cope with any file type:
file=$1
offset=$(
od -An -v -tu1 "$file" |
sed 's/[[:blank:]]\{1,\}/ /g; s/^ //; s/ $//; y/ /\n/' |
awk '$0=="10" {i=NR} END {print i+0}'
)
dd bs=1 seek=$offset if=/dev/null of="$file"
Everything after the final newline is truncated. Files without a newline are truncated to zero length. The contents of files ending in a newline are not modified.
The byte value sought could be trivially parameterized so that anything following the final occurrence of that value is discarded.
In the vast majority of cases, Don's simpler approach should suffice.
Nicely done Alister. But even this approach has an exception. This code will remove all characters in a file after the last <newline> character as long as the file is encoded using a codeset that is a superset of ASCII.
If you want to use this on IBM's AIX on a mainframe computer (or any other OS that uses EBCDIC to encode text), you'll need to change the $0=="10" in the awk command to $0=="37" .
Quite right. Parameterizing the byte value along with echo | od -An -tu1 yield a solution for any character encoding with a single-byte newline (a multibyte sequence can be matched, but not without complicating the code).