Modifying a file without changing inode number

Hi all,
I am struggling to change the content of a file without changing the inode number. The exact issue is as below.

I have a file name test.bak which has 100 lines of text.
I am trying to to delete the first 90 lines of the text in the file.

I know that using sed/awk/head/tail I can delete the lines.
But the general procedure is as follows.

  1. Use sed/awk/head/tail to delete the 90 lines in test.bak
  2. Re-direct to a temporary file. (tmp.bak)
  3. Move the temporary file (tmp.bak) to the original file name (test.bak).

When I move the temporary file to the original file,the inode number of the original file changes. I want to retain the inode number of the original file.
How can I do that?

I am using Solaris 10.

Please help.

Rather than moving the file, cat the tmp file back onto the original file:

sed 's/foo/bar/' <ofile >tfile
cat tfile >ofile
1 Like

Use ed:

printf '1,90d\nw\nq\n' | ed -s test.bak

... or, alternatively, with a heredoc ...

ed -s test.bak <<EOED
1,90d
w
q
EOED

Example:

$ jot 100 > data
$ ls -i data
5490111 data
$ wc -l data
     100 data
$ head data
1
2
3
4
5
6
7
8
9
10
$ printf '1,90d\nw\nq\n' | ed -s data
$ ls -i data
5490111 data
$ wc -l data
      10 data
$ head data
91
92
93
94
95
96
97
98
99
100

Regards,
Alister

1 Like

Thank you very much.
This resolves my issue.