创建新列,其中包含使用bash中现有列的串联
我有一个大的tsv.gz文件(40GB),我想从现有变量col3
中提取一个字符串,将其存储在新变量new_var
(放置在开始)并保存所有新文件。 我是新来的示例,
col1 col2 col3 col4
1 positive 12:1234A 100
2 negative 10:9638B 110
3 positive 5:0987A 100
4 positive 8:5678A 170
数据“ old_file.tsv.gz”所需的数据“ new_file.tsv.gz”
New_var col1 col2 col3 col4
12 1 positive 12:1234A 100
10 2 negative 10:9638B 110
5 3 positive 5:0987A 100
8 4 positive 8:5678A 170
我是bash中的新手,所以我尝试了多件事,但我被卡住了,我尝试过
zcat old_file.tsv.gz | awk '{print New_var=$3,$0 }' | awk '$1 ~ /^[0-9]:/{print $0 | (gzip -c > new_file.tsv.gz) }'
我觉得我有多个问题。 {print new_var = $ 3,$ 0}
确实创建了col3
的副本,但不会重命名它。然后,当我添加代码的最后一部分awk'$ 1〜 /^[0-9]: /{打印$ 0 | (gzip -c> new_file.tsv.gz)}'
...好吧,没有什么都没有出现(如果我忘记了括号,但找不到问题)。 我也不确定这种方式是否是最好的方法。 有什么想法使它起作用吗?
I have a large tsv.gz file (40GB) for which I want to extract a string from an existing variable col3
, store it in a new variable New_var
(placed at the beginning) and save everything the new file.
an example of the data "old_file.tsv.gz"
col1 col2 col3 col4
1 positive 12:1234A 100
2 negative 10:9638B 110
3 positive 5:0987A 100
4 positive 8:5678A 170
Desired data "new_file.tsv.gz"
New_var col1 col2 col3 col4
12 1 positive 12:1234A 100
10 2 negative 10:9638B 110
5 3 positive 5:0987A 100
8 4 positive 8:5678A 170
I am new in bash so I have tried multiple things but I get stuck, I have tried
zcat old_file.tsv.gz | awk '{print New_var=$3,$0 }' | awk '$1 ~ /^[0-9]:/{print $0 | (gzip -c > new_file.tsv.gz) }'
I think I have multiple problems. {print New_var=$3,$0 }
do create a duplicate of col3
but doesn't rename it. Then when I add the last part of the code awk '$1 ~ /^[0-9]:/{print $0 | (gzip -c > new_file.tsv.gz) }'
...well nothing comes up (I tried to look if I forget a parenthesis but cannot find the problem).
Also I am not sure if this way is the best way to do it.
Any idea how to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在单独的文件中制作一个尴尬脚本(用于可读性),例如
1.awk
:现在处理输入(例如
1.csv.gz
),用awk文件:Make an AWK script in a separate file (for readability), say
1.awk
:Now process the input (say
1.csv.gz
) with the AWK file:我建议使用一个选项卡(
\ t
)和:
作为输入字段分隔器:作为一行:
请参阅: 8强大的内置变量 - fs , ofs ,rs,ors, nr ,nf,fileName,fnr
I suggest to use one tab (
\t
) and:
as input field separator:As one line:
See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR