Skip to content

Commit 847d5b2

Browse files
committed
Change content of a file in block or byte level
1 parent 2df625d commit 847d5b2

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: Change content of a file in block or byte level
3+
date: 2024-10-19T09:10:00+08:00
4+
categories:
5+
- tech
6+
tags:
7+
- dd
8+
- linux
9+
---
10+
11+
Since wrote https://jackliusr.github.io/posts/2024/10/things-learnt-from-recent-preparation-of-1t-data-for-testing/[Things learnt from recent preparation of 1T data for testing], I had figured out how to do that on the following weekends. Today I couldn't get the steps to do so immediately. It is better to note down for my next time usage. The detailed steps is as following
12+
13+
. Find the location in the file
14+
. Get the block contains contents to modify
15+
. Extract the block to a file and modify it
16+
. Replace the target block
17+
18+
19+
scripts:
20+
21+
[source, bash]
22+
----
23+
# test file test.ndjson in /dev/sdc
24+
# line contains aaa need to replace with same length of spaces
25+
> grep -oba aaa test.ndjson
26+
16697:aaa
27+
> lsblk -o NAME,PHY-SEC,LOG-SEC,SIZE
28+
NAME PHY-SEC LOG-SEC SIZE
29+
sda 512 512 388.4M
30+
sdb 4096 512 4G
31+
sdc 4096 512 1T
32+
33+
# backup the file to verify and diff
34+
> cp test.ndjson test2.ndjson
35+
# 16697/4096=4.07
36+
dd if=/home/jack/tmp/test.ndjson bs=4096 skip=4 count=1 > 5block.json
37+
# replace line ["aaa": 1, with spaces
38+
# use sed or vim edit 5block.json
39+
40+
# replace the block 5
41+
dd if=5block.json of=test.ndjson bs=4096 count=1 seek=4
42+
----

0 commit comments

Comments
 (0)