仅当列在AWK中包含n个字符时才插入字符

时间:2015-09-08 15:09:54

标签: awk

这是我的档案:

// require.config.js
requirejs.config({
    // ...
    paths            : {
        // ...
        // Tell require.js where to find your module
        "I18n"       : "services/I18n"
    }
});

我试图在第5列中插入零和空格,前提是该字段有8个字符,如下所示:

DI3456###numeric###Closed###RPVFF123###13:00:23###no
DI4321###numeric###Open###RPVFG345###1 10:00:15###yes
DI9876###numeric###Closed###RPVGG678###09:15:16###no
DI6788###numeric###Closed###RPHH123###5 05:05:00##yes

有什么建议吗?

4 个答案:

答案 0 :(得分:1)

拯救......

$ awk 'BEGIN{FS=OFS="###"} length($5)==8 {$5="0 "$5}1' myfile

DI3456###numeric###Closed###RPVFF123###0 13:00:23###no
DI4321###numeric###Open###RPVFG345###1 10:00:15###yes
DI9876###numeric###Closed###RPVGG678###0 09:15:16###no
DI6788###numeric###Closed###RPHH123###5 05:05:00##yes

答案 1 :(得分:0)

awk '-F###' '{if (length($5)==8){$5="0 "$5};print $0}'

答案 2 :(得分:0)

sed -r 's/(#{3})([0-9]{2}:[0-9]{2}:[0-9]{2}#{3}(no|yes))$/\10 \2/' file.txt

应该成功。这个稍微简单一点也应该有用,但它对时间模式的要求不那么严格:

sed -r 's/(#{3})([0-9:]{8}#{3}(no|yes))$/\10 \2/' file.txt

答案 3 :(得分:0)

假设:

$ echo "$tgt"
DI3456###numeric###Closed###RPVFF123###13:00:23###no
DI4321###numeric###Open###RPVFG345###1 10:00:15###yes
DI9876###numeric###Closed###RPVGG678###09:15:16###no
DI6788###numeric###Closed###RPHH123###5 05:05:00##yes

您可以使用Perl:

$ echo "$tgt" | perl -lne '@a=split(/#+/); print "\"$a[4]\"\t\"0 $a[4]\"" if length($a[4])==8'
"13:00:23"  "0 13:00:23"
"09:15:16"  "0 09:15:16"
相关问题