当前位置: 首页 > 知识库问答 >
问题:

在bash中sed重写同一文件[重复]

詹联
2023-03-14

我想删除文件的标题,并替换同一文件中没有标题的内容。

示例:file_student

name age
XYS 24
RTF 56

输出应该是:

XYS 24
RTF 56

情况是我不想为此更改创建任何新文件。se可以这样做吗?

我尝试了:

sed 1d /tmp/file_student.txt |
hadoop fs -copyfromLocal /tmp/file_student.txt /tmp/file_student_no_header.txt

但这不起作用。感谢您的帮助!

共有2个答案

何琨
2023-03-14

如果您不想使用se,请尝试ail-例如,您有一个名为xxx的文件:

<代码>尾巴-n 2 xxx

郭元明
2023-03-14

sed的手册页摘录

-i[SUFFIX]'-in-place[=SUFFIX]'此选项指定要在位编辑的文件。GNU“sed”通过创建一个临时文件并将输出发送到此文件而不是标准输出来实现这一点。(1).

 This option implies `-s'.

 When the end of the file is reached, the temporary file is renamed
 to the output file's original name.  The extension, if supplied,
 is used to modify the name of the old file before renaming the
 temporary file, thereby making a backup copy(2)).

 This rule is followed: if the extension doesn't contain a `*',
 then it is appended to the end of the current filename as a
 suffix; if the extension does contain one or more `*' characters,
 then _each_ asterisk is replaced with the current filename.  This
 allows you to add a prefix to the backup file, instead of (or in
 addition to) a suffix, or even to place backup copies of the
 original files into another directory (provided the directory
 already exists).

 If no extension is supplied, the original file is overwritten
 without making a backup.

因此,您需要将您的se命令更改为sed-i 1d file|任何希望这有所帮助。

 类似资料:
  • 我复制并制作一个文件。 当我对文件进行分类时,我得到: 然后我并立即显示: 但是当我再次使用cat键盘2时,我得到: 用sudo测试,用's/fr/us/'测试 我理解错了什么吗?sed应该写入文件还是需要通过管道覆盖原始文件?非常感谢。 版本:

  • 我创建了一个Java游戏,当游戏结束时,执行一个方法,告诉用户输入他/她的名字,然后他们的分数将保存在playscores中。txt文档。这很好用。然而,我想要的不仅仅是这份文件中一个人的分数。我想要它,这样每个玩游戏的名字和分数将保存在这个文件中。非常感谢您的帮助。 这是我的gameComplete方法代码: 我尝试过不同的东西,比如Objectoutputstream,但不幸的是,我不知道该怎

  • 我有个问题。 有一个自定义mvc结构,当它是根文件夹时,一切都通过.htaccess中的RewriteRule工作,但如果我将它设置为子文件夹,它将停止工作。 .htaccess是 重写引擎在 重写条件%{REQUEST_FILENAME}-重写cond%{REQUEST_FILENAME}-d RewriteRule^(.*)$index.php?route=$1[L,QSA] AddDefau

  • 我在接受hackerrank的挑战。目的是: 读取(int)N;然后读取N个整数并将其平均值打印到小数点后三位。 这是代码: 当我为一些输入运行代码时: 3#(N的值) 4#(N=3个整数) 4 3 那么输出是3.666,但应该是3.667。 所以问题是,到底是有什么方法可以使它正确(正确的四舍五入),还是它只能这样工作? (当上述代码在hackerrank为挑战的Testcase2运行时,问题就

  • 我在更新带有sed删除效果的文件时遇到问题。 当我执行以下操作时,更换 即时shell输出显示最后一行已删除,但在执行时 这表明此删除尚未存储,为什么?

  • 假设我有一个名为. csvABC_DE_FGHI_10_JK_LMN文件。我想从文件名(即10)中提取ID,借助ID位置和文件名分隔符。我有以下两个输入 此处ID可以是数字或字母数字。那么,如何在上述两个输入的帮助下从上述文件中提取10。如何在bash中实现这一点?