How to Delete a Newline If It Is the Last Character in a File

Removing a newline character at the end of a file

Take advantage of the fact that a) the newline character is at the end of the file and b) the character is 1 byte large: use the truncate command to shrink the file by one byte:

# a file with the word "test" in it, with a newline at the end (5 characters total)
$ cat foo
test

# a hex dump of foo shows the '\n' at the end (0a)
$ xxd -p foo
746573740a

# and `stat` tells us the size of the file: 5 bytes (one for each character)
$ stat -c '%s' foo
5

# so we can use `truncate` to set the file size to 4 bytes instead
$ truncate -s 4 foo

# which will remove the newline at the end
$ xxd -p foo
74657374
$ cat foo
test$

You can also roll the sizing and math into a one line command:

truncate -s $(($(stat -c '%s' foo)-1)) foo

How can I delete a newline if it is the last character in a file?

perl -pe 'chomp if eof' filename >filename2

or, to edit the file in place:

perl -pi -e 'chomp if eof' filename

[Editor's note: -pi -e was originally -pie, but, as noted by several commenters and explained by @hvd, the latter doesn't work.]

This was described as a 'perl blasphemy' on the awk website I saw.

But, in a test, it worked.

Removing the new line in the last line of a text file using sed

This might work for you (GNU sed):

sed -z 's/\n\+$//' file

This will remove a newline(s) at the end of a file provided there are no null characters.

N.B. Normal use of sed i.e. without the -z option which slurps the file into memory, will remove any newlines before the sed commands can act on them.

Delete newline character in text file if next line is less than a certain length

perl -p0777e "s{\r?\n(?=.{0,5}$)}{}mg" test.txt

output

hi hi hi hi hibye
fun fun fun fun fun
batman
shirt shirt shirt
pants pants pantsbelt
paper paper paper

[ Well it took me 2 minutes to write the one-liner and about an hour to explain. ]

Here's the explanation:

Switches:

-p - read every line of the input files, run the code specified by -e for each line, and print the variable $_ (which is modified by the -e code)

-0[octal number] - input line separator; if we specify 0777 the whole file will be considered a line and read at once

-l - strip input lines from ending \n, set the output line separator equal to the input line separator. (I removed it, cause it's actually not needed here)

Now the regular expression:

s{\r?\n(?=.{0,5}$)}{}mg

s{pattern}{replacement} - search for pattern in variable $_ and replace it with replacement

pattern parts:

\r?\n - match every newline symbol. For Unix \n would be enough, \r? - optional match of CR that may be necessary for old perl versions under Windows. Actually I think \r? can be removed too.

(?=pattern) - a positive look-ahead match of pattern, a zero width match, that is it does not consume the characters.

.{0,5}$ - match from zero to five characters ending with

s{}{} operator modifiers: m - multiline matching, makes $ match just before \n everywhere in text, not only at the end of the line.
g - global matching, replace every occurrence in the text.

Finally, how it all works:

Perl slurps the whole file (-0777) and (-p), then it searches for every occurence of \r?\n that is followed by no more than 5 non-newline characters and a newline: (?=.{0,5}$).

Every occurrence is replaced by the empty string {}.

I think I've been clear enough.

Additional information can be obtained from: perldoc perlre, perldoc perlop , perldoc perlrun.

How to remove newline from the end of a file using Perl

You can try this:

perl -pe 'chomp if eof' file.txt

Here is another simple way, if you need it in a script:

open $fh, "file.txt"; 
@lines=<$fh>; # read all lines and store in array
close $fh;
chomp $lines[-1]; # remove newline from last line
print @lines;

Or something like this (in script), as suggested by jnhc for the command line:

open $fh, "file.txt"; 
while (<$fh>) {
chomp if eof $fh;
print;
}
close $fh;

Remove newline at the end of a file in python

use str.rstrip() method:

my_file =  open("text.txt", "r+")
content = my_file.read()
content = content.rstrip('\n')
my_file.seek(0)

my_file.write(content)
my_file.truncate()
my_file.close()

Remove the last newline from file in Java

You can use replaceFirst which use regex with this one [\n\r]+$, like so :

trimm = trimm.replaceFirst("[\n\r]+$", "");

Full code

I tried this piece of code :

public static void main(String[] args) throws Exception {
String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\n" +
"ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\r\n";

System.out.println("---------------------------------------------------Before replace Start of the input---------------------------------------------------");
System.out.println(trimm);
System.out.println("---------------------------------------------------Before replace End of the input---------------------------------------------------");

System.out.println("---------------------------------------------------After replace Start of the input---------------------------------------------------");
trimm = trimm.replaceFirst("[\n\r]+$", "");
System.out.println(trimm);
System.out.println("---------------------------------------------------After replace End of the input---------------------------------------------------");

}

The output :

---------------------------------------------------Before replace Start of the input---------------------------------------------------
ABC 123|1|2 ABC '123|1|2|"Jan 30 2018 2:34:13:000AM"|dd1|1|"Jan 30 2018 2:56:08:000AM"|EST' ABC 20180821
ABC 123|1|2 ABC '123|1|2|"Jan 30 2018 2:34:13:000AM"|dd1|1|"Jan 30 2018 2:56:08:000AM"|EST' ABC 20180821

---------------------------------------------------Before replace End of the input---------------------------------------------------
---------------------------------------------------After replace Start of the input---------------------------------------------------
ABC 123|1|2 ABC '123|1|2|"Jan 30 2018 2:34:13:000AM"|dd1|1|"Jan 30 2018 2:56:08:000AM"|EST' ABC 20180821
ABC 123|1|2 ABC '123|1|2|"Jan 30 2018 2:34:13:000AM"|dd1|1|"Jan 30 2018 2:56:08:000AM"|EST' ABC 20180821
---------------------------------------------------After replace End of the input---------------------------------------------------

Note that there are a break line before the replace and after the replace only the last break line is removed.

Ideone demo


More details

I tried this three solutions :

Code 1 (Your code)

public static void main(String[] args) throws Exception {
String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\n" +
"ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\r\n";

try {
File fout = new File("path");
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
trimm = trimm.replaceAll("[\n\r]+$", "");
bw.write(trimm);
//bw.newLine();//<-----------------------note this
bw.close();
} catch (FileNotFoundException e) {
// File was not found
e.printStackTrace();
} catch (IOException e) {
// Problem when writing to the file
e.printStackTrace();
}
}

Code 2

public static void main(String[] args) throws Exception {
String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\n" +
"ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\r\n";

Path path = Paths.get("path");

try (BufferedWriter writer = Files.newBufferedWriter(path))
{
writer.write(trimm.replaceFirst("[\n\r]+$", ""));
}
}

Code 3

public static void main(String[] args) {
String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\n" +
"ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\r\n";

try {
Files.write(Paths.get("path"), trimm.replaceFirst("[\n\r]+$", "").getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}

and all the three codes gives me :

result

Remove newline if preceded with specific character on non-consecutive lines

Your data sample imply that there are no several consecutive lines ending with %.

In that case, you may use

sed '/%$/{N;s/\n//}' file.txt > output.txt

It works as follows:

  • /%$/ - finds all lines ending with %
  • {N;s/\n//} - a block:

    • N - adds a newline to the pattern space, then appends the next line of input to the pattern space
    • s/\n// - removes a newline in the current pattern space.

See the online sed demo.

How to remove the last characters of each line in a file?

Open your file with open("fileName.ext") method and then iterate each line. on each line you can perform the slicing function [:]

eachLineData = 'username1:password1:dd/mm/yy'
expectedResult = eachLineData[:-9]
print(expectedResult)

Full Code Example:

# Using readlines()
file1 = open('myfile.txt', 'r')
Lines = file1.readlines()

for line in Lines:
expectedResult = line[:-9]
print(expectedResult)

Delete last character from line that contains specific word

This would do it:

sed '/in_bytes/ s/,$//'

Where /in_bytes/ is a search pattern ensuring only matching lines will execute the following command s/,$//, which is a standard substitution to remove a trailing comma.

Example: https://ideone.com/3hEXBY



Related Topics



Leave a reply



Submit