Linux awk 计算字段数并相应地添加
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9841036/
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
awk count number of fields and add accordingly
提问by Deano
I have the following file.csv
我有以下 file.csv
111111 | 111111 | 22222 | 44444 | 4445454 | 67554333 |
I can count the number of fields using the following
我可以使用以下方法计算字段数
awk -F '|' '{print NF}' file.csv
in my database schema I have 33 fields, however some of the lines in my csv file has less than 33 fields therefore when I import the file, it complains about miss match.
在我的数据库模式中,我有 33 个字段,但是我的 csv 文件中的某些行少于 33 个字段,因此当我导入文件时,它会抱怨未匹配。
using awk how can I go about adding NULL fields spreader by | in order to full up 33 rows
使用 awk 我如何去添加 NULL 字段扩展器 | 为了填满 33 行
your help is highly appreciated.
非常感谢您的帮助。
Thank you
谢谢
采纳答案by kev
To add empty fields at the end of line:
在行尾添加空字段:
awk -F'|' -v OFS='|' '{for(i=NF+1;i<=33;i++)$i=""}1' file.csv
回答by potong
This might work for you:
这可能对你有用:
sed ':a;s/|/&/33;t;s/$/|/;ta' file.csv