trim lines

I want to delete all lines from 1 till a certain line.

how to do that without redirection to another file (tail solution)?

thanx

With GNU sed:

sed -i 1,nd file

n = number of lines

With perl:

perl -i -ne'print unless 1..n' file
head -5 filename

gives first 5 lines of the file filename.

If you want to keep the nr of lines as a variable, you can use command:

head -n $nr_of_lines_reqd filename

Hi and thanks for your replies.
Actually I was not clear in my first post
the output of the perl/sed/head will be displayed on stdout. I expect the result to be in the file itself and not on the stdout. i.e. the result will should be a shorter file (without creating a new file)

thanks again.

Melanie, what you want doesn't exist, at least not in standard UNIX. Every utility (sed, awk, ...) will create a separate file and if you want to replace your file with the trimmed one you will have to "mv" it.

Th GNU sed does indeed have the "-i" option allowing for in-place editing, but even then a temporary file (probably somewhere in /var/tmp) will be created by the tool. The same is true for editors like vi, ed, etc..

As there is no difference in principle where this temporary file is created that means that all these utilities work the same way.

If you want to preserve the i-node number of the file in question do the following: create a intermediate file and then use "cat" to overwrite the old file:

# head -${some_number} file > newfile
# cat newfile > file
# rm -f newfile

This will make sure that "file" will have the same i-node number before and after the change.

I hope this helps.

bakunin

You have to redirect to another file. awk or sed can be used.

An awk solution...

awk '{if (NR<3) print}' file.dat

Well, in that case a simple head can also be used to get the output to another file.

I guess we cannot trim the file without creating another file.

You were clear,
but you didn't try the commands I suggested :slight_smile:

And, of course, bakunin is rigth,
these commands use temporary files implicitly.