简体   繁体   English

使用awk / sed在制表符分隔文件的中间添加重复的内容

[英]Add repeated content in middle of tab delimited file using awk/sed

I have a text file (tab delimited) and I need to add a new header row in line 3 我有一个文本文件(制表符分隔),我需要在第3行中添加一个新的标题行

[Tab] ABC123   ABC124
[Tab] High     High
ENSG  8.9      7.2

The new line I want to be will be line 3, and then I'll want to print European 5 times, tab between each of them, then Asian 6 times, tab between each of them... 我想成为的新行将是第3行,然后我将要打印欧洲5次,在每个之间进行制表,然后在亚洲6次,在它们之间进行制表...

[Tab] ABC123   ABC124
[Tab] High     High    
[Tab] European European [this will be the new line]
ENSG  8.9      7.2

I'm think of using sed so that I can use 我想使用sed,以便我可以使用

sed '4 i' sed'4 i'

But I don't really know how to do the actually printing/appending of new content into that new line I'm guessing I'd need to start /European/\\t/a or similar. 但是我真的不知道如何将新内容实际打印/添加到新行中,我想我需要启动/ European / \\ t / a或类似名称。

Basically struggling, probably because I'm not googling intelligently! 基本上是在挣扎,可能是因为我没有聪明地进行谷歌搜索!

Use sed '3i\\ \\tEuropean\\tEuropean' file : 使用sed '3i\\ \\tEuropean\\tEuropean' file

$ cat file
        ABC123          ABC124
        High            High
ENSG    8.9             7.2

$ sed '3i\ \tEuropean\tEuropean' file
        ABC123          ABC124
        High            High
        European        European
ENSG    8.9             7.2

Edit: 编辑:

I probably do something like this awk 'NR==3{for(i=0;i<10;i++)s=s"\\tEuropean";print s}1' : 我可能会做这样awk 'NR==3{for(i=0;i<10;i++)s=s"\\tEuropean";print s}1'

$ awk 'NR==3{for(i=0;i<10;i++)s=s"\tEuropean";print s}1' file
        ABC123          ABC124
        High            High
        European        European        European    European    European    European    European    European    European    European
ENSG    8.9             7.2

There are several ways to do this. 有几种方法可以做到这一点。 One technique is: 一种技术是:

t="$(printf \\t)"  # Assign t to be a string with one tab
sed -e '4i\
European${t}European...
' input-file > output-file

With some shells, you can do things like t=$"\\t" , and with some sed you do not need a literal newline after the i , but the above is pretty portable. 使用某些shell,您可以执行t=$"\\t" ,使用某些sed您不需要在i之后使用文字换行符,但是上面的代码非常可移植。 An you can always use a literal tab (you might need to type ctrl-v tab at an interactive prompt). 您可以始终使用文字tab (您可能需要在交互式提示下键入ctrl-v tab )。

This might work for you (GNU): 这可能对您有用(GNU):

sed -r '2!b;G;:a;/(\tEuropean){10}/!s/$/\tEuropean/;ta' file

Where 10 can be any number of the repeated field \\tEuropean 其中10可以是任意数量的重复字段\\tEuropean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM