Thursday, April 28, 2022

[SOLVED] Remove last line from all files of a specific extension

Issue

I have several files with same extension .txt in a directory. I want to remove the last line from all .txt files.

What I did is

find . -type f -name '*.txt' -exec sed '$d' {} \;

This prints the desired output from sed to the terminal for every .txt file.

What I want is to modify the respective files.


Solution

Try this:--

sed  -i '$d' *.txt

"$" is used as a line number and means the last line in the file.

"d" is usd to delete the respective line(last line in this case).

"*.txt" is used to select all files whose extension is .txt in the present directory.



Answered By - Ravi Saroch
Answer Checked By - Terry (WPSolving Volunteer)