biolocal:ShellFileEditing
From Wiki CEINGE
(Difference between revisions)
Revision as of 12:33, 14 January 2008 (edit) Gianluca (Talk | contribs) (New page: Every command described here can be easily made in other scripting high level language (php). The use of bash is preferrable when you can't or won't set up an entire programming environeme...) ← Previous diff |
Revision as of 12:45, 14 January 2008 (edit) (undo) Gianluca (Talk | contribs) Next diff → |
||
Line 1: | Line 1: | ||
- | Every command described here can be easily | + | Every command described here can be easily obtained in a high level scripting language (php). The use of bash is preferrable when you can't or won't set up an entire programming environement to launch simple commands against a list of text files. |
+ | |||
+ | '''Delete last line from a file/s''' | ||
+ | |||
+ | To obtain a text of a file without last line, just execute: | ||
+ | |||
+ | <pre> | ||
+ | cuttedfile=`sed '$d' filename`; | ||
+ | </pre> | ||
+ | |||
+ | Now you just need to write the new text in the same file: | ||
+ | |||
+ | <pre> | ||
+ | echo "$cuttedfile">filename; | ||
+ | </pre> | ||
+ | |||
+ | If you want to execute the same operation on a list of files contained in the same directory: | ||
+ | |||
+ | <pre> | ||
+ | for filename in `ls`; \ | ||
+ | do \ | ||
+ | cuttedfile=`sed '$d' $filename`; \ | ||
+ | echo "$cuttedfile">$filename; \ | ||
+ | done; | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | Please, pay attention to the double quotes used to echo the $cuttedfile variable; they are needed to ensure the content of the text file is entirely kept (carrige returns, tabs and so on). |
Revision as of 12:45, 14 January 2008
Every command described here can be easily obtained in a high level scripting language (php). The use of bash is preferrable when you can't or won't set up an entire programming environement to launch simple commands against a list of text files.
Delete last line from a file/s
To obtain a text of a file without last line, just execute:
cuttedfile=`sed '$d' filename`;
Now you just need to write the new text in the same file:
echo "$cuttedfile">filename;
If you want to execute the same operation on a list of files contained in the same directory:
for filename in `ls`; \ do \ cuttedfile=`sed '$d' $filename`; \ echo "$cuttedfile">$filename; \ done;
Please, pay attention to the double quotes used to echo the $cuttedfile variable; they are needed to ensure the content of the text file is entirely kept (carrige returns, tabs and so on).