bash sed 如何删除文件中的前 17 行和后 8 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14752348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:30:37  来源:igfitidea点击:

sed how to delete first 17 lines and last 8 lines in a file

linuxbashsedtext-processing

提问by Deano

I have a big file 150GB CSV file and I would like to remove the first 17 lines and the last 8 lines. I have tried the following but seems that's not working right

我有一个 150GB 的大文件 CSV 文件,我想删除前 17 行和最后 8 行。我已经尝试了以下但似乎不起作用

sed -i -n -e :a -e '1,8!{P;N;D;};N;ba' 

and

sed -i '1,17d' 

I wonder if someone can help with sed or awk, one liner will be great?

我想知道是否有人可以帮助使用 sed 或 awk,一个班轮会很棒吗?

回答by choroba

headand tailare better for the job than sedor awk.

head并且tailsed或更适合这份工作awk

tail -n+18 file | head -n-8 > newfile

回答by Ed Morton

awk -v nr="$(wc -l < file)" 'NR>17 && NR<(nr-8)' file

回答by Scrutinizer

All awk:

所有 awk:

awk 'NR>y+x{print A[NR%y]} {A[NR%y]=
Try this :

sed '{[/]<n>|<string>|<regex>[/]}d' <fileName>       
sed '{[/]<adr1>[,<adr2>][/]d' <fileName>
}' x=17 y=8 file

回答by Scrutinizer

LENGTH=`wc -l < file`
head -n $((LENGTH-8)) file | tail -n $((LENGTH-17)) > file

where

在哪里

  1. /.../=delimiters

  2. n = line number

  3. string = string found in in line

  4. regex = regular expression corresponding to the searched pattern

  5. addr = address of a line (number or pattern )

  6. d = delete

  1. /.../=分隔符

  2. n = 行号

  3. 字符串 = 在行中找到的字符串

  4. regex = 搜索模式对应的正则表达式

  5. addr = 行地址(数字或模式)

  6. d = 删除

Refer this link

参考这个链接

回答by Adam Sznajder

LENGTH=`wc -l < file`
head -n $((LENGTH-8)) file | tail -n $((LENGTH-8-17)) > file

Edit: As mtk posted in comment this won't work. If you want to use wcand track file length you should use:

编辑:正如 mtk 在评论中发布的那样,这是行不通的。如果你想使用wc和跟踪文件长度,你应该使用:

LENGTH=`wc -l < file`
head -n $((LENGTH-8)) file > file
LENGTH=`wc -l < file`
tail -n $((LENGTH-17)) file > file

or:

或者:

sed -i '' -e $'1,17d;:a\nN;19,25ba\nP;D' file.txt

What makes this solution less elegant than that posted by choroba :)

是什么让这个解决方案不如 choroba 发布的那么优雅 :)

回答by jcsahnwaldt says GoFundMonica

Similar to Thor's answer, but a bit shorter:

类似于雷神的回答,但更短一点:

1,{front}d;:a\nN;{front+2},{front+tail}ba\nP;D

The -i ''tells sed to edit the file in place. (The syntax may be a bit different on your system. Check the man page.)

-i ''告诉sed编辑文件到位。(语法在您的系统上可能略有不同。请查看手册页。)

If you want to delete frontlines from the front and tailfrom the end, you'd have to use the following numbers:

如果front要从前面和tail最后删除行,则必须使用以下数字:

1,17d     # delete lines 1 ... 17, goto start
:a        # define label a
N         # add next line from file to buffer, quit if at end of file
19,25ba   # if line number is 19 ... 25, goto start (label a)
P         # print first line in buffer
D         # delete first line from buffer, go back to start

(I put them in curly braces here, but that's just pseudocode. You'll have to replace them by the actual numbers. Also, it should work with {front+1}, but it doesn't on my machine (macOS 10.12.4). I think that's a bug.)

(我在这里将它们放在花括号中,但这只是伪代码。您必须用实际数字替换它们。此外,它应该可以使用{front+1},但它在我的机器(macOS 10.12.4)上不起作用。我认为这是一个错误。)

I'll try to explain how the command works. Here's a human-readable version:

我将尝试解释该命令的工作原理。这是一个人类可读的版本:

{
  ghead -17  > /dev/null
  sed -n -e :a -e '1,8!{P;N;D;};N;ba'
} < my-bigfile > subset-of

First we skip 17 lines. That's easy. The rest is tricky, but basically we keep a buffer of eight lines. We only start printing lines when the buffer is full, but we stop printing when we reach the end of the file, so at the end, there are still eight lines left in the buffer that we didn't print - in other words, we deleted them.

首先我们跳过 17 行。这很容易。其余的很棘手,但基本上我们保留了 8 行的缓冲区。我们只在缓冲区满时开始打印行,但是当我们到达文件末尾时我们停止打印,所以最后,缓冲区中还有八行我们没有打印 - 换句话说,我们删除了它们。

回答by sotapme

I learnt this today for the shell.

我今天为 shell 学习了这个。

##代码##

One has to use a non consuming head, hence the use of gheadfrom the GNU coreutils.

必须使用非消耗的head,因此使用ghead来自 GNU coreutils 的 。