[SOLVED] Linux command to delete the last non empty line from a file
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
But the above command instead deleting the last line and copying to the new file,like below.
Code:
$ cat file2
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21
Total number of rows: 5
<<Blank line >>
<<Blank line >>
I am new to Linux and seeking help from the forum.
The sed command you posted only does one thing; delete the last line of a file. If you're talking about more complex operations, like examining that line to see if it's blank, and copying/appending output to a different file, you will have to write a small script to do so. Read the "Question Guidelines" link in my posting signature...there are many easily-found bash scripting tutorials that can help you get started, but we aren't going to write your scripts for you.
To give you a hint, this:
Code:
[ -z "$line" ] && continue
..is a small bash-script line that will check the variable ($line) to see if it's empty and if so, skip it.
Also in that result is a command to search for and remove based on a pattern, thus if that one line with it's summary comments is desired to be removed, then find something unique in that, search for that pattern, and remove the line matching that pattern.
How messy it's going to be depends on whether the file contains other blank lines besides the ones at the end. If there are none, then the task is pretty straightforward with awk or perl, doable with some difficulty with sed (the sed language is, after all, Turing-complete).
If there are embedded blank lines, then processing the lines in reverse order is the only single-step solution that comes to mind. (Multi-step method: Pre-process the file to locate the trailing blank lines, then use sed to delete the specific line number that precedes them.)
The END section is only needed if trailing empty lines must be retained.
Regarding empty versus blank, /./ means non-empty, replace it with NF for non-blank
The END section is only needed if trailing empty lines must be retained.
Regarding empty versus blank, /./ means non-empty, replace it with NF for non-blank
The tac command is like cat, but reads in reverse, last line to first line.
The awk command sets an internal variable NF containing the number of fields when it reads a line. The ! is the NOT operator, so !NF is true when no fields are found in the line and an empty line is printed.
When an awk variable is initialised without a value, it defaults to zero, so n=0. When the first line containing fields is found, then n is set to 1 and nothing is printed. Subsequently all lines containing fields are printed.
I will let MadeInGermany explain his clever creation manipulating the record separator variable in conjunction with a buffer.
The tac command is like cat, but reads in reverse, last line to first line.
The awk command sets an internal variable NF containing the number of fields when it reads a line. The ! is the NOT operator, so !NF is true when no fields are found in the line and an empty line is printed.
When an awk variable is initialised without a value, it defaults to zero, so n=0. When the first line containing fields is found, then n is set to 1 and nothing is printed. Subsequently all lines containing fields are printed.
I will let MadeInGermany explain his clever creation manipulating the record separator variable in conjunction with a buffer.
What difference does it make if the code is changed to (I need to understand it as my sas code is not allowing the double codes,but the functionality if the code should not change) as below.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.