Hi
How can I delete last line from all files from a Directory
Bye
Mangesh
On 06/29/06 11:27, Mangesh V Rakhunde wrote:
How can I delete last line from all files from a Directory
this is a simple and dangerous script - no sanity and error checks done.. you are encouraged to add your own.
for f in * do lines=`cat $f|wc -l` let lines = lines - 1 head -n $lines $f > ${f}.new mv ${f}.new $f done
I am sure the command "sed"(or may be "awk") can somehow do this in just one line .. so wait till a sed guru bangs the hell out of me .. :-)
On 6/29/06, Laxminarayan G Kamath A laxminarayan@deeproot.co.in wrote:
On 06/29/06 11:27, Mangesh V Rakhunde wrote:
How can I delete last line from all files from a Directory
for f in * do lines=`cat $f|wc -l` let lines = lines - 1 head -n $lines $f > ${f}.new mv ${f}.new $f done
I am sure the command "sed"(or may be "awk") can somehow do this in just one line .. so wait till a sed guru bangs the hell out of me .. :-)
You can delete the last line of a file like so:
sed '$d" file
Put that in the for loop.
Regards, NMK
Sometime Today, LGKA cobbled together some glyphs to say:
On 06/29/06 11:27, Mangesh V Rakhunde wrote:
How can I delete last line from all files from a Directory
[snip]
I am sure the command "sed"(or may be "awk") can somehow do this in just one line .. so wait till a sed guru bangs the hell out of me .. :-)
well, you asked for it :P
sed -ne '$q;p' < file.txt > file.txt.bak && mv file.txt.bak file.txt
Philip
On 06/29/06 11:27, Mangesh V Rakhunde wrote:
How can I delete last line from all files from a Directory
this is a simple and dangerous script - no sanity and error checks done.. you are encouraged to add your own.
for f in * do lines=`cat $f|wc -l` let lines = lines - 1 head -n $lines $f > ${f}.new mv ${f}.new $f done
I tested above code on my system but it throws errors.
s[3]: lines: not found. s[4]:lines: the specified number is not valid for this command has anubody a clue?
Dinesh
On 6/29/06, Dinesh Kumar dinesh@denabank.co.in wrote:
On 06/29/06 11:27, Mangesh V Rakhunde wrote:
How can I delete last line from all files from a Directory
this is a simple and dangerous script - no sanity and error checks done.. you are encouraged to add your own.
for f in * do lines=`cat $f|wc -l` let lines = lines - 1 head -n $lines $f > ${f}.new mv ${f}.new $f done
I tested above code on my system but it throws errors.
s[3]: lines: not found. s[4]:lines: the specified number is not valid for this command has anubody a clue?
Should be $lines instead of lines in the second line of the do block, methinks.
Dinesh
NMK