简体   繁体   English

在文件中指定和操作列-Shell / bash

[英]Specifying and manipulating columns inside file - shell/bash

line in file: 文件中的行:

aaa, bbb, ccc, ddd, Sept 18, 2016 ##:##PM

trying to parse it out (desired output) - 试图解析出来(期望的输出)-

aaa, bbb, ccc, ddd, "Sept 18, 2016 etc..."

using unix shell - trying to wrap last two columns in quotes for a date importing process. 使用unix shell-尝试在日期导入过程中将最后两列用引号引起来。 However, am trying to aim for a specified column approach in case a situation arises where the two columns are internal ie: 但是,在出现内部两列的情况下,我试图针对指定的列方法:

aaa, bbb, Sept 18, 2016 etc..., ccc, ddd

thus outputing: 因此输出:

aaa, bbb, "Sept 18, 2016 etc...", ccc, ddd

The command to display it is fine - as i can redirect it to a file and work with it from there 显示它的命令很好-因为我可以将其重定向到文件并从那里使用它

当将示例数据中的##:##替换为实际数字(并假设第二个“示例”中给出的“ etc ...”与第一个示例中的格式匹配)时,以下代码可用于GNU sed

sed -re 's@ ((Jan|Feb|Mar|Apr|May|June|July|Aug|Sept|Oct|Nov|Dec) [[:digit:]]+, [[:digit:]]{4} [[:digit:]]{1,2}:[[:digit:]]{2}[AP]M)([, ]|$)@ "\1"\3 @g'

Your unix tool for column manipulation is awk: 您用于列操作的Unix工具是awk:

awk 'BEGIN{FS=", ";OFS=", "} {$5="\""$5; $6=$6"\""; print $0;}' YOUR_FILE.txt

$5 and $6 are the numbers of the columns you need to put " around. If you want to put " before the 5th and after the 7th column, just update the awk expression accordingly. $ 5和$ 6是您需要在第5列之前和第7列之后放置“”的列号。只需相应地更新awk表达式即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM