Bash:删除文件中的字符,仅在第一行

Bash:删除文件中的字符,仅在第一行,bash,sed,awk,Bash,Sed,Awk,我正在制作一个脚本,将大量文件从一个路径复制到另一个路径。 路径上的任何文件在第一行都有大量的“垃圾”,直到“returnpath…”这个词出现 文件内容示例: §°ç§°*é*é*§°ç§°çççççççReturn-PathOTHERTHINGS REST OF THE FILE EOF 也许sed或awk能帮上忙 问题: 我想要文件的全部内容,除了前面的任何内容,然后是“返回路径”,它应该只在第一行剥离,这样做: Return-PathOTHERTHINGS REST OF THE FI

我正在制作一个脚本,将大量文件从一个路径复制到另一个路径。 路径上的任何文件在第一行都有大量的“垃圾”,直到“returnpath…”这个词出现

文件内容示例:

§°ç§°*é*é*§°ç§°çççççççReturn-PathOTHERTHINGS
REST
OF
THE
FILE
EOF
也许sed或awk能帮上忙

问题:

我想要文件的全部内容,除了前面的任何内容,然后是“返回路径”,它应该只在第一行剥离,这样做:

Return-PathOTHERTHINGS
REST
OF
THE
FILE
EOF
重要事项:在返回路径为“二进制”之前的任何内容,实际上的文件都被视为二进制。。。 如何解决?

试试:

sed '1s/.*Return-Path/Return-Path/'
此命令仅在第一行用“返回路径”替换“返回路径”之前的任何内容。

请尝试:

sed '1s/.*Return-Path/Return-Path/'

此命令仅在第一行用“Return Path”替换“Return Path”之前的任何内容。

我现在不想编写此代码,但可能会给您一个提示。“返回路径”为11个字符。您可以从偏移量为“n”的文件中使用

因此,如果你做一个循环,n从零开始递增,直到结果匹配“returnpath”,你可以计算出需要从前面删除多少字节。然后你可以用另一个“dd”来做


或者,看一下通过“xxd”运行文件,用“sed”编辑文件,然后用“xxd-r”以另一种方式通过“xxd”运行它。

我现在不想编写此代码,但可能可以给您一个提示。“返回路径”为11个字符。您可以从偏移量为“n”的文件中使用

因此,如果你做一个循环,n从零开始递增,直到结果匹配“returnpath”,你可以计算出需要从前面删除多少字节。然后你可以用另一个“dd”来做


或者,看看通过“xxd”运行文件,用“sed”编辑文件,然后用“xxd-r”以另一种方式通过“xxd”运行文件。

好的,这是新的一天,现在我确实想为您编写代码:-)

这个算法在我对你同样问题的另一个回答中有描述

#!/bin/bash
################################################################################
# behead.sh
# Mark Setchell
# 
# Utility to remove stuff preceding specified string near start of binary file
#
# Usage: behead.sh <infile> <outfile>
################################################################################
IN=$1
OUT=$2
SEARCH="Return-Path"
for i in {0..80}; do
   str=$(dd if="$1" bs=1 count=${#SEARCH} iseek=$i 2> /dev/null)
   if [ "$str" == $SEARCH ]; then
      # The following line will go faster if you exchange "bs" and "iseek" 
      # parameters, because it will work in bigger blocks, it just looks
      # wrong, so I haven't done it.
      dd if="$1" of="$OUT" bs=1 iseek=$i 2> /dev/null
      exit $?
   fi
done
echo String not found, sorry.
exit 1
如果将我的脚本保存为“斩首.sh”,则需要使其可执行,如下所示:

#
# Create binary with 15 bytes of bash, then "Return-Path", then entire bash in file "bashed"
(dd if=/bin/bash bs=1 count=15 2>/dev/null; echo -n 'Return-Path'; cat /bin/bash) > bashed
#
# Chop off junk at start of "bashed" and save in "restored"
./behead.sh bashed restored
#
# Check the restored "bash" is exactly 11 bytes longer than original, 
# as it has "Return-Path" at the beginning
ls -l bashed restored
chmod +x behead.sh
./behead.sh inputfile outputfile
然后您可以这样运行它:

#
# Create binary with 15 bytes of bash, then "Return-Path", then entire bash in file "bashed"
(dd if=/bin/bash bs=1 count=15 2>/dev/null; echo -n 'Return-Path'; cat /bin/bash) > bashed
#
# Chop off junk at start of "bashed" and save in "restored"
./behead.sh bashed restored
#
# Check the restored "bash" is exactly 11 bytes longer than original, 
# as it has "Return-Path" at the beginning
ls -l bashed restored
chmod +x behead.sh
./behead.sh inputfile outputfile

顺便说一下,二进制文件中没有“一行”的概念,所以我假设了前80个字符——当然,您可以随意更改它

好吧,这是新的一天,现在我真的想为你编写这个:-)

这个算法在我对你同样问题的另一个回答中有描述

#!/bin/bash
################################################################################
# behead.sh
# Mark Setchell
# 
# Utility to remove stuff preceding specified string near start of binary file
#
# Usage: behead.sh <infile> <outfile>
################################################################################
IN=$1
OUT=$2
SEARCH="Return-Path"
for i in {0..80}; do
   str=$(dd if="$1" bs=1 count=${#SEARCH} iseek=$i 2> /dev/null)
   if [ "$str" == $SEARCH ]; then
      # The following line will go faster if you exchange "bs" and "iseek" 
      # parameters, because it will work in bigger blocks, it just looks
      # wrong, so I haven't done it.
      dd if="$1" of="$OUT" bs=1 iseek=$i 2> /dev/null
      exit $?
   fi
done
echo String not found, sorry.
exit 1
如果将我的脚本保存为“斩首.sh”,则需要使其可执行,如下所示:

#
# Create binary with 15 bytes of bash, then "Return-Path", then entire bash in file "bashed"
(dd if=/bin/bash bs=1 count=15 2>/dev/null; echo -n 'Return-Path'; cat /bin/bash) > bashed
#
# Chop off junk at start of "bashed" and save in "restored"
./behead.sh bashed restored
#
# Check the restored "bash" is exactly 11 bytes longer than original, 
# as it has "Return-Path" at the beginning
ls -l bashed restored
chmod +x behead.sh
./behead.sh inputfile outputfile
然后您可以这样运行它:

#
# Create binary with 15 bytes of bash, then "Return-Path", then entire bash in file "bashed"
(dd if=/bin/bash bs=1 count=15 2>/dev/null; echo -n 'Return-Path'; cat /bin/bash) > bashed
#
# Chop off junk at start of "bashed" and save in "restored"
./behead.sh bashed restored
#
# Check the restored "bash" is exactly 11 bytes longer than original, 
# as it has "Return-Path" at the beginning
ls -l bashed restored
chmod +x behead.sh
./behead.sh inputfile outputfile

顺便说一下,二进制文件中没有“一行”的概念,所以我假设了前80个字符——当然,您可以随意更改它

对不起,我忘了指定它们是“二进制”文件,它们是“二进制”文件,直到那个单词。。。前两行的示例:IMS6^^^^^^^^^^^^ XCP`fL^^^^^^^^返回路径:从asmtp1.website.it(122.1.53.13)通过mail1.damn.comExcuse me收到,但我忘了指定它们是“二进制”文件,它们是“二进制”文件,直到那个单词。。。前两行的示例:IMS6^^^^^^^^^^^^^^^^^^^^^^^返回路径:从asmtp1.website.it(122.1.53.13)通过mail1.com接收