从Linux中的分隔文件中删除一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12716392/
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
Delete a column from a delimited file in linux
提问by MyFirstName MyLastName
I have a file in the following format:
我有以下格式的文件:
col1|col2|col3|col4
a|b|c|d
e|f||h
i|j|k|l
I would like to delete col3 (with the delimiter "|") from the header and the data as well. Can this be done using awk/sed?
我想从标题和数据中删除 col3(带有分隔符“|”)。这可以使用 awk/sed 完成吗?
Plese NOTE that the data in col3 maybe empty (row 2).
请注意 col3 中的数据可能为空(第 2 行)。
The output should be:
输出应该是:
col1|col2|col4
a|b|d
e|f|h
i|j|l
回答by Kent
awk 'BEGIN{FS=OFS="|"}{print ,,}' file
should give you the output.
应该给你输出。
it is the very basic awk usage.
这是非常基本的 awk 用法。
edit
编辑
you didn't mention 70 columns... :(
你没有提到 70 列... :(
try this:
尝试这个:
awk -F'|' '{s="";for(i=1;i<=NF;i++){f=(NF==i)?"":FS;if(i!=3)s=s $i f;}print s}' file
回答by tripleee
You could simply use cut
.
你可以简单地使用cut
.
cut -d'|' -f1-2,4- file
回答by Dean
Here's a possible sed solution:
这是一个可能的 sed 解决方案:
sed -i.bak filename -e 's;\(^.*|.*|\).*|\(.*\);;'
This will work great for your example, and could be adjusted for other examples, but isn't really a general purpose solution.
这对您的示例非常有用,并且可以针对其他示例进行调整,但并不是真正的通用解决方案。
Explanation:
解释:
-i.bak
Edit the file in place, first making a backup called filename.bak
.
-i.bak
就地编辑文件,首先创建一个名为filename.bak
.
\(^.*|.*|\)
From the start of the line, match everything up to and including the second delimiter. The parenthesis group this match (group 1).
\(^.*|.*|\)
从行首开始,匹配所有内容,直到并包括第二个分隔符。括号分组此匹配项(组 1)。
.*|
Match everything up to and including the last delimiter.
.*|
匹配包括最后一个分隔符在内的所有内容。
\(.*\)
Match the rest and group (group 2).
\(.*\)
匹配其余和组(组 2)。
\1\2
Replace all of the previous matches with the text from group 1 and group 2.
\1\2
用第 1 组和第 2 组中的文本替换之前的所有匹配项。
回答by Jonathan Wakely
Using cut
is the right answer, but if you really want to use awk
it's easier than Kent shows:
使用cut
是正确的答案,但如果你真的想使用awk
它比 Kent 显示的更容易:
awk -F'|' 'BEGIN {OFS="|"} {for (n=3; n < NF; ++n) $n = $(n+1); --NF; print}'
Just shuffle the fields after $3
down, then by altering the value of NF
you change the number of fields.
只需在$3
down后洗牌字段,然后通过改变NF
你的值来改变字段的数量。
回答by potong
This might work for you (GNU sed):
这可能对你有用(GNU sed):
sed 's/[^|]*|//3' file
回答by minhas23
cut command will help to achieve this
cut 命令将有助于实现这一目标
cat filname | cut -d'|' -f1,2,4
回答by discipulus
Another awk
solution could be useful if you have many columns
awk
如果您有很多列,另一种解决方案可能会很有用
awk -F'|' '{="";##代码##=##代码##;=}1' FPAT='[^|]+' OFS='|' file