AWK if length 语句追加

编程入门 行业动态 更新时间:2024-10-18 10:24:54
本文介绍了AWK if length 语句追加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试解决 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"前面附加另一个分隔符.到目前为止,我有一半的代码,不知道去哪里.我在想这会是这样开头的:

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

有谁知道可以用于附加输入数据的 if 语句之后的正确格式吗?

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

推荐答案

如果该行少于 7 个字段,只需在第 3 个字段的末尾添加一个额外的@":

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 if length 语句追加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-17 20:19:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/921483.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   AWK   length

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!