如果AWK长度声明追加 [英] AWK if length statement append

查看:223
本文介绍了如果AWK长度声明追加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图修复csv文件丢失数据的问题。列,这应该是空,是缺乏一个双字段分隔符来维持CSV结构。下面的例子说明我的意思:

I am trying to fix a problem of missing data in a csv file. The column, that should be null, is lacking a double field separator to maintain the csv structure. The example below illustrates what I mean:

 1@Sunshine@2/M@L@JRVel@215@WAW
 2@Pass@2/J@L1@JAvar@218@JKDes
 3@Solo@2/K@JRosa@218@WAW
 4@Bomber@2/D@L1@JLOrt@218@GCCon
 5@SmokingCandy@2/Y@L1@MFco@218@SMAs
 6@BigBound@2/H@L1@JCast@218@SMAs
 7@ShootBunies@2/H@L@DLPar@218@DKo

正如你所看到的,排在第三,第四列,没有L或L1,而不是它的JRosa。这将导致一个MySQL数据库的道路不正确的格式化。我希望做的是使用AWK if语句追加JRosa的另一个分隔符盈。到目前为止,我有一个code的一半,不知道哪里去了。我想这会是一个启动,如:

As you can see, in the third row, fourth column, there is no "L" or "L1", instead it's "JRosa". This causes incorrect formating of a mysql database down the road. What I would like to do is use an AWK if statement to append another delimiter infront of "JRosa". So far I have half a code and no idea where to go. I was thinking it would be something that starts like:

awk -F@ '{if(length($4) > 4) print "@"$4}'

刚刚打印出来:

@JRosa

我希望能找到,结果在一个解决方案:

I'm hoping to find a solution that results in:

1@Sunshine@2/M@L@JRVel@215@WAW
2@Pass@2/J@L1@JAvar@218@JKDes
3@Solo@2/K@@JRosa@218@WAW
4@Bomber@2/D@L1@JLOrt@218@GCCon
5@SmokingCandy@2/Y@L1@MFco@218@SMAs
6@BigBound@2/H@L1@JCast@218@SMAs
7@ShootBunies@2/H@L@DLPar@218@DKo

是否有人知道正确格式化后,如果可以用于追加输入数据的语句?

Does anybody know the correct formating after the if statement that can be used to append the input data?

推荐答案

只需添加一个额外的@在第三场结束时,如果有就行了不到7个领域:

Just add an extra "@" at the end of the 3rd field if there's less than 7 fields on the line:

$ awk 'BEGIN{FS=OFS="@"} NF<7{$3=$3FS} 1' file
 1@Sunshine@2/M@L@JRVel@215@WAW
 2@Pass@2/J@L1@JAvar@218@JKDes
 3@Solo@2/K@@JRosa@218@WAW
 4@Bomber@2/D@L1@JLOrt@218@GCCon
 5@SmokingCandy@2/Y@L1@MFco@218@SMAs
 6@BigBound@2/H@L1@JCast@218@SMAs
 7@ShootBunies@2/H@L@DLPar@218@DKo

这篇关于如果AWK长度声明追加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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