You can do this with head and tail, you just miss out some important
parameters these commands can take!
head -n -NUMBER FILE
this command will return all but the last NUMBER lines of FILE
tail -n +NUMBER FILE
this command will return all lines of FILE starting from NUMBER
so... if FILE is your file, CH are the lines you want to cut from the
top... and CT the lines you want to cut from the bottom,
CH=`expr + `
head -n -CT FILE | tail -n +CH > NEWFILE
We need to add 1 to CH, because tail starts printing from that line.
The following script would make things a bit easier... Basic checks
are in place (but not all that should be). It accepts top and bottom
lines to be cropped as parameters. It also requires a filename or a
directory which should be preceded by "-d".
#!/bin/bash
# crop.sh
# Crops the specified number of lines from the top and bottom part of
one or more files
#ext holds the extension added to modified files
ext=".cropped";
ERR="Invalid Parameters\nSyntax: $0 [HEADNUM] [TAILNUM] <[FILE] || [-d
DIRECTORY]>";
DIR=0
# parameter 1 is the lines you want to cut from the top
if [ -n "$1" ]; then
CH=`expr $1 + 1`
else
echo -e $ERR;
exit;
fi
# parameter 2 is the lines you want to cut from the bottom
if [ -n "$2" ]; then
CT=$2
else
echo -e $ERR;
exit
fi
# parameter 3 is the file you want to edit. '-d' will expert a
directory in parameter 4.
if [ -n "$3" ]; then
if [ "$3" == "-d" ]; then
DIR=1;
else
if [ -f "$3" ]; then
FILE=$3
else
echo -e $ERR;
exit;
fi
fi
else
echo -e $ERR;
exit;
fi
if [ $DIR -eq 0 ]; then
# Copy the contents of the file, skipping the (tail) lines and the
head lines we don't need anymore
( head -n -$CT $FILE | tail -n +$CH > $FILE$ext && echo "$FILE
cropped and saved as $FILE$ext" ) || echo "Something failed! :)";
else
if [ -n "$4" ] && [ -d "$4" ]; then
cd $4
for i in `ls $4`
do
# Copy the contents of the file twice,
# first by skipping the (tail) lines we don't
need anymore,
# then by skipping the head lines we don't need anymore
( head -n -$CT $i | tail -n +$CH > $i$ext && echo "$i
cropped and saved as $i$ext" ) || echo "Something failed! :)";
done
else
echo -e $ERR
exit
fi
fi
On 8/26/06, Aaron Mehl <[EMAIL PROTECTED]> wrote:
Hi all,
I find often that I want to use standard tools to do a simple repetitive
task but can't conceptualize the way to achieve it.
I have 20 text files with text to delete on top and on bottom.
I know that head and tail both will Show me the parts I want to delete,
but not delete them.
cut deletes but I don't see how it does it by line.
Are these the right tools to do what I want?
for example text file A.
lines 1-12 delete
line 30-35 delete
What is the strategey I should use to acheve this?
=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]
--
|
| Alex Alexander :: +30 693 8778114
| Linux :: Debian :: 2.6.17 :: KDE 3.5.4
| www.justaeuro.com
\
=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]