简体   繁体   中英

Remove blank lines from the ends of a bunch of files

I have a bunch of files with many lines in them, and usually one or two blank lines at the end.

I want to remove the blank lines at the end, while keeping all of the blank lines that may exist within the file.

I want to restrict the operation to the use of GNU utilities or similar, ie bash, sed, awk, cut, grep etc.

I know that I can easily remove all blank lines, with something like:

sed '/^$/d'

But I want to keep blank lines which exist prior to further content in the file.

File input might be as follows:

line1
line2

line4
line5


I'd want the output to look like:

line1
line2

line4
line5

All files are <100K, and we can make temporary copies.

With Perl:

perl -0777 -pe 's/\n*$//; s/$/\n/' file

Second S command ( s/$/\\n/ ) appends again a newline to end of your file to be POSIX compilant.

Or shorter:

perl -0777 -pe 's/\n*$/\n/' file

With Fela Maslen's comment to edit files in place ( -i ) and glob all elements in current directory ( * ):

perl -0777 -pe 's/\n*$/\n/' -i *

Here is an awk solution (Standard linux gawk ). I enjoyed writing.

single line:

awk '/^\s*$/{s=s $0 ORS; next}{print s $0; s=""}' input.txt

using a readable script script.awk

    /^\s*$/{skippedLines = skippedLines $0 ORS; next}
    {print skippedLines $0; skippedLines= ""}

explanation:

/^\s*$/ {                   # for each empty line
    skippedLines = skippedLines $0 ORS; # pad string of newlines
    next;                   # skip to next input line
}
{                           # for each non empty line
    print skippedLines $0;  # print any skippedLines and current input line
    skippedLines= "";       # reset skippedLines
}

If lines containing just space chars are to be considered empty:

$ tac file | awk 'NF{f=1}f' | tac
line1
line2

line4
line5

otherwise:

$ tac file | awk '/./{f=1}f' | tac
line1
line2

line4
line5

This might work for you (GNU sed):

sed ':a;/\S/{n;ba};$d;N;ba' file

If the current line contains a non-space character, print the current pattern space, fetch the next line and repeat. If the current line(s) is/are empty and it is the last line in the file, delete the pattern space, otherwise append the next line and repeat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM