I use the following sed command to remove the line number
string from file
sed -i s'/line number//g' file
but what I need to change in sed
syntax in order to remove the line number
only if it exist in the last line of the file?
Deleting line using sed To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.
txt (if it ends with EOF , or \n and then EOF ), the number of lines in new_file. txt may be the same (as file. txt ) after this command (this happens when there is no \n ) - in any case, the contents of the last line is deleted.
sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' : this will remove all the blank lines from end of the file. sed '$d' : this will remove the last line.
I'm not sure I fully understand your question, however this can help you :
sed -i '$ s/line number//g' file
This will perform substitution only on last line (because of $
), replacing line number
by nothing.
man sed
, section Addresses can help you understand how to apply sed commands on part of the text.
For those who are on SunOS which is non-GNU, the following code will help:
sed '$d' tmp.dat > test.dat
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With