bash脚本:在给定的字节偏移位置将输入文件中的字节追加到先前存在的文件中

bash脚本:在给定的字节偏移位置将输入文件中的字节追加到先前存在的文件中,bash,shell,Bash,Shell,我正在努力实现的目标: 我有一个文件,我需要从某个位置复制某些字节 并将它们附加到该文件给定位置的文件中 我的想法是这样的: xxd -s $startOffset -l $numBytes inFile | dd of=fileToModify seek=$location conv=notrunc 我也有这个,但它只适用于在文件开头追加 read -p "Enter target file :> " targetFile read -p "Enter source file to a

我正在努力实现的目标: 我有一个文件,我需要从某个位置复制某些字节 并将它们附加到该文件给定位置的文件中

我的想法是这样的:

xxd -s $startOffset -l $numBytes inFile | dd of=fileToModify seek=$location conv=notrunc
我也有这个,但它只适用于在文件开头追加

read -p "Enter target file :> " targetFile
read -p "Enter source file to append at the end of target file :> " inputFile
dd if=$inputFile of=$targetFile oflag=append conv=notrunc
提前谢谢你

试试这个:

# copy certain bytes from a certain location 
file=$1
certainlocation=$2
certainbytes=$3
# Append them to a file at a given location of that file
givenlocation=$4

dd if=$file of=$file iflag=skip_bytes oflag=seek_bytes,append conv=notrunc skip=$certainlocation seek=$givenlocation count=1 bs=$certainbytes
用法:

> printf "1\n2\n3\n4\n" > /tmp/1; ./1.sh /tmp/1 4 2 2; cat /tmp/1;
1+0 records in
1+0 records out
2 bytes copied, 0.000378992 s, 5.3 kB/s
1
2
3
4
3
第一个文件的内容

$ cat first
fskasfdklsgdfksdjhgf sadjfsdjfhf
dsfghkasdfg sadfhsdfh hskdjfksdfgkfg
jhfksjdafhksdjfh


ksdjhfsdjfh
sceond文件的内容

$ cat second
jfhasjdhfjskdhf dshfjsdfh3821349832749832]
87348732642364
]yfisdfhshf936494
sdfisdfsdfsa;dlf
9346934623984
shell脚本的内容

$ cat cppaste.sh
dd if=$1 of=$2 bs=1 count=$3 status=noxfer
dd if=$4 of=$2 bs=1 seek=$3 status=noxfer
finsize=$(stat -c%s $2)
dd if=$1 of=$2 bs=1 skip=$3 seek=$finsize oflag=append status=noxfer
使用正确的参数执行shell脚本

$ bash cppaste.sh first  third 10 second
10+0 records in
10+0 records out
107+0 records in
107+0 records out
92+0 records in
92+0 records out
结果文件的内容

$ cat third
fskasfdklsjfhasjdhfjskdhf dshfjsdfh3821349832749832]
87348732642364
]yfisdfhshf936494
sdfisdfsdfsa;dlf
9346934623984
gdfksdjhgf sadjfsdjfhf
dsfghkasdfg sadfhsdfh hskdjfksdfgkfg
jhfksjdafhksdjfh


ksdjhfsdjfh

您想将其他内容推回,还是就地覆盖?标准UNIX文件系统在操作系统级别无法在文件中插入部分内容,而不会在以后覆盖内容--这就是为什么用于随机访问插入(通过数据库等)的文件格式包括使用/免费位图、块列表、,一些现代Linux文件系统允许在文件的中间插入和删除块,但是(1)它只在非常特定的文件上运行,(2)它只在单个块上运行,也就是说,您只能在块边界位置添加块大小的块——这意味着您可以在文件的4k边界添加4kb NUL页,但是你不能只在某个随机位置添加一行,而不必重写它之后的所有内容来将这些内容移回…这就是为什么你在问题中的示例必须使用
dd
来重写插入点之后文件的整个剩余部分。如果您自己想这样做,那没关系,但它不是一个“附加”,即使用
O\u append
写入文件时,不需要写任何东西,只需要在结尾添加新内容。@charlesduff感谢您抽出时间写回。因此,基本上我正在尝试编写另一个输出文件,将它们连接在一起,这很好。我只是想找到最好的方法,我认为这是将三个部分连接到一个新的缓冲区中,然后用新的缓冲区重写原始文件。新的缓冲区包括:将旧文件的开始复制到插入点。复制新材料,从插入点+1复制旧文件,直到结束。您在正确的轨道上。您缺少从中提取字节的第二个文件(我们称之为
file2
),它是字节源,需要从e中提取的字节数,然后需要将
file2
附加到原始文件的某个位置(
file
)。如对我的问题的评论中所述,
file2
中的字节源不能附加到
文件中,所以可能需要一个
tempFile
,或者使用一个缓冲区将其放在一起,并在末尾覆盖文件,或者在dd中创建一个
outpuFile
只需更改
of=file2
?我确实尝试过,但它将其添加到文件末尾而不是给定位置。这太棒了!但是,如何从
$fileToAppend
中选择起始位置和字节数;我不知道你需要这个。可能会将
cat
替换为
dd
替换为
skip
count
,然后。顺便说一句,如果您只需要支持支持reflink的文件系统,那么第一个
dd
可以替换为
cp--reflink
truncate
,这样做要快得多,因为包含插入点的块之前的所有内容都可以重用,而无需重写磁盘上的位。谢谢!稍加调整,我就完成了!我只需要添加我想要复制的字节的位置和要复制的字节数的功能。
$ cat third
fskasfdklsjfhasjdhfjskdhf dshfjsdfh3821349832749832]
87348732642364
]yfisdfhshf936494
sdfisdfsdfsa;dlf
9346934623984
gdfksdjhgf sadjfsdjfhf
dsfghkasdfg sadfhsdfh hskdjfksdfgkfg
jhfksjdafhksdjfh


ksdjhfsdjfh