插入在每一行的开头的文字从2到端 - 5 [英] Insert text at the beginning of each line from 2 to end - 5

查看:75
本文介绍了插入在每一行的开头的文字从2到端 - 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个文件中(就地插入)每一行的开头插入一个字,接着一个制表符但是从2号线到所有的行,但最后5行开始。

I want to insert a word followed by a tab character at the start of each line in a file (in-place insertion) but starting from line number 2 to all the lines but last 5 lines.

因此​​,如果一个文件有10条线路,我想插入从2号线到线5号 - 我想保持线1和6-10完好在这种情况下

So if a file has 10 lines, I want to insert from line number 2 to line number 5 - I want to keep lines 1 and 6-10 intact in this case.

该文件有以百万线(目前高达10元)

The file can have lines in millions (currently upto 10 million)

sed -i "s/^/word\t/" filename 

以上的作品,但我想插入第一个和最后一个5行。也给出了线范围内,计算的行数将是另一个操作。由于行号可以改变,这种额外的操作可以成为一个开销。寻找一个有效的解决方案。这里是我到目前为止已经试过:

The above works, but I want to insert on the first and last 5 lines. Also given a line range, calculating the number of lines will be another operation. Since the line numbers can vary, this extra operation can become an overhead. Looking for an efficient solution. Here is what I have tried so far:

COUNT=$((`wc -l test_csnap_delta.csv | cut -d ' ' -f 1` - 5))
sed -n -i '2,$COUNT s/^/word\t/' 

不过上面是删除整个文件中的数据。

However the above is deleting the entire file data.

先谢谢了。

推荐答案

此作品,未经precounting文件中的行数:

This works without precounting the number of lines in the file:

sed -ni '1{p;b}; 2{N;N;N;N}; $p; $!{N;s/^/word /;P;D}' filename

此缓冲器五线,使在第一行的置换在缓冲器和打印和删除它。当文件的最后一行被读取,打印缓冲区没有做任何换人。

This buffers five lines and makes the substitution on the first line in the buffer and prints and deletes it. When the last line in the file is read, the buffer is printed without doing any substitutions.


  • 1 {磷; B} - 读的第一线,打印不变,并分支到结束

  • 2 {N; N; N; N} - 当读线2条,追加四个行来创建一个五线缓冲

  • $ P - 当读取文件的最后一行,打印留在缓冲区中的线不变

  • $ ! - 在当前行不是文件中的最后一行...

  • N - 追加下一行到缓冲区(模式空间)

  • S / ^ /字/ - 使第一行替换缓冲区中的

  • P - 只打印缓冲区中的
  • 的第一行
  • D - 只删除第一行中的缓冲

  • 1{p;b} - read the first line, print it unchanged and branch to the end
  • 2{N;N;N;N} - when line 2 is read, append four more lines to create a five-line buffer
  • $p - when the last line of the file is read, print the lines that remain in the buffer unchanged
  • $! - when the current line is not the last line in the file...
  • N - append the next line to the buffer (pattern space)
  • s/^/word / - make the substitution on the first line in the buffer
  • P - print only the first line in the buffer
  • D - delete only the first line in the buffer

请注意,这不会为包含少于6行的文件正常工作。

Note that this won't work properly for files that consist of fewer than 6 lines.

这是使用AWK同样的想法:

This is the same idea using AWK:

awk 'FNR == 1 {print; next} FNR == 2 {for (ptr = 0; ptr <= 4; ptr++) {buffer[ptr] = $0; getline}; ptr = 0} {sub(/^/, "word ", buffer[ptr]); print buffer[ptr]; buffer[ptr] = $0; ptr = (ptr + 1) % 5} END {for (i = 0; i <= 4; i++) {print buffer[(ptr + i) % 5]}}' filename > outputfile
mv outputfile filename

这是破了多条线路上:

Here it is broken out on multiple lines:

FNR == 1 {
    print
    next
}
FNR == 2 {
    for (ptr = 0; ptr <= 4; ptr++) {
        buffer[ptr] = $0
        getline
    }
    ptr = 0
}
{
    sub(/^/, "word ", buffer[ptr])
    print buffer[ptr]
    buffer[ptr] = $0
    ptr = (ptr + 1) % 5
}
END {
    for (i = 0; i <= 4; i++) {
        print buffer[(ptr + i) % 5]
    }
}

这篇关于插入在每一行的开头的文字从2到端 - 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆