清除这条线的方法

时间:2015-09-18 20:21:34

标签: bash awk printing

我在文本文件中有以下数据(底部)。我正在使用awk将行更改为列,但是它很麻烦。

cat 5_lines.txt |awk -F\' '{print $1,"\n" "\n" $2,"\n" $3,"\n" $4,"\n" $5,"\n" $6,"\n" $7,"\n" $8,"\n" $9,"\n" $10,"\n" $11,"\n" $12,"\n" $13,"\n" $14,"\n" $15,"\n" $16,"\n" $17,"\n" $18,"\n" $19,"\n" $20,"\n" $21,"\n" $22,"\n" $23,"\n" $24,"\n" $25,"\n" $26,"\n" $27,"\n" $28,"\n" $29,"\n" $30,"\n" $31,"\n" $32,"\n" $33,"\n" $34"\n"}'|sed -e 's/  */ /g' -e 's/\[//g' -e 's/,//g'

我很欣赏一些缩短此命令的awk部分的建议。提前谢谢。

####预期输出
BBBB 4575 XXX.YYY.ZZZ.191    
part = TBD Part    
model = TBD Model    
serial = TBD Serial    
wpn = TBD WPN    
feature = 0    
mods =    
date = 0-00-00 00:00:00    
hours = 0    
key1 = 0    
key2 = 0    
key3 = 0    
key4 = 0    
key5 = 0    
key6 = 0    
key7 = 0    
key8 = 0    
14

BBBB 4576 XXX.YYY.ZZZ.121    
part = TBD Part    
model = TBD Model    
serial = TBD Serial    
wpn = TBD WPN    
feature = 0    
mods =    
date = 0-00-00 00:00:00    
hours = 0    
key1 = 0    
key2 = 0    
key3 = 0    
key4 = 0    
key5 = 0    
key6 = 0    
key7 = 0    
key8 = 0    
14

BBBB 4578 XXX.YYY.ZZZ.162    
part = TBD Part    
model = TBD Model    
serial = TBD Serial    
wpn = TBD WPN    
feature = 0    
mods =    
date = 0-00-00 00:00:00    
hours = 0    
key1 = 0    
key2 = 0    
key3 = 0    
key4 = 0    
key5 = 0    
key6 = 0    
key7 = 0    
key8 = 0    
14

BBBB 4580 XXX.YYY.ZZZ.179    
part = TBD Part    
model = TBD Model    
serial = TBD Serial    
wpn = TBD WPN    
feature = 0    
mods =    
date = 0-00-00 00:00:00    
hours = 0    
key1 = 0    
key2 = 0    
key3 = 0    
key4 = 0    
key5 = 0    
key6 = 0    
key7 = 0    
key8 = 0    
14

BBBB 4581 XXX.YYY.ZZZ.15    
part = TBD Part    
model = TBD Model    
serial = TBD Serial    
wpn = TBD WPN    
feature = 0    
mods =    
date = 0-00-00 00:00:00    
hours = 0    
key1 = 0    
key2 = 0    
key3 = 0    
key4 = 0    
key5 = 0    
key6 = 0    
key7 = 0    
key8 = 0    
14

原始数据

BBBB 4575 XXX.YYY.ZZZ.191  part = TBD Part  model = TBD Model  serial = TBD Serial  wpn = TBD WPN  feature = 0  mods =  date = 0-00-00 00:00:00  hours = 0  key1 = 0  key2 = 0  key3 = 0  key4 = 0  key5 = 0 key5 = 0  key6 = 0  key7 = 0  key8 = 0  14    
BBBB 4576 XXX.YYY.ZZZ.121  part = TBD Part  model = TBD Model  serial = TBD Serial  wpn = TBD WPN  feature = 0  mods =  date = 0-00-00 00:00:00  hours = 0  key1 = 0  key2 = 0  key3 = 0  key4 = 0  key5 = 0 key5 = 0  key6 = 0  key7 = 0  key8 = 0  14    
BBBB 4578 XXX.YYY.ZZZ.162  part = TBD Part  model = TBD Model  serial = TBD Serial  wpn = TBD WPN  feature = 0  mods =  date = 0-00-00 00:00:00  hours = 0  key1 = 0  key2 = 0  key3 = 0  key4 = 0  key5 = 0 key5 = 0  key6 = 0  key7 = 0  key8 = 0  14    
BBBB 4580 XXX.YYY.ZZZ.179  part = TBD Part  model = TBD Model  serial = TBD Serial  wpn = TBD WPN  feature = 0  mods =  date = 0-00-00 00:00:00  hours = 0  key1 = 0  key2 = 0  key3 = 0  key4 = 0  key5 = 0 key5 = 0  key6 = 0  key7 = 0  key8 = 0  14    
BBBB 4581 XXX.YYY.ZZZ.15  part = TBD Part  model = TBD Model  serial = TBD Serial  wpn = TBD WPN  feature = 0  mods =  date = 0-00-00 00:00:00  hours = 0  key1 = 0  key2 = 0  key3 = 0  key4 = 0  key5 = 0 key5 = 0  key6 = 0  key7 = 0  key8 = 0  14

3 个答案:

答案 0 :(得分:1)

如果您不坚持使用awk,这可能是更好的选择

cat yourfile.txt  | tr '[' ',' | tr -d "['\]]"  | sed 's/, /\n/g'

答案 1 :(得分:1)

鉴于您新发布的预期输出:

$ cat tst.awk
{
    delete f
    nf=1
    for (i=1;i<NF;i++) {
        if ($(i+1) == "=") {
            nf++
        }
        f[nf] = (f[nf]=="" ? "" : f[nf] OFS) $i
    }
    f[++nf] = $i

    for (i=1; i<=nf; i++) {
        print f[i]
    }
    print ""
}

$ awk -f tst.awk file
BBBB 4575 XXX.YYY.ZZZ.191
part = TBD Part
model = TBD Model
serial = TBD Serial
wpn = TBD WPN
feature = 0
mods =
date = 0-00-00 00:00:00
hours = 0
key1 = 0
key2 = 0
key3 = 0
key4 = 0
key5 = 0
key5 = 0
key6 = 0
key7 = 0
key8 = 0
14

BBBB 4576 XXX.YYY.ZZZ.121
part = TBD Part
model = TBD Model
serial = TBD Serial
wpn = TBD WPN
feature = 0
mods =
date = 0-00-00 00:00:00
hours = 0
key1 = 0
key2 = 0
key3 = 0
key4 = 0
key5 = 0
key5 = 0
key6 = 0
key7 = 0
key8 = 0
14

BBBB 4578 XXX.YYY.ZZZ.162
part = TBD Part
model = TBD Model
serial = TBD Serial
wpn = TBD WPN
feature = 0
mods =
date = 0-00-00 00:00:00
hours = 0
key1 = 0
key2 = 0
key3 = 0
key4 = 0
key5 = 0
key5 = 0
key6 = 0
key7 = 0
key8 = 0
14

BBBB 4580 XXX.YYY.ZZZ.179
part = TBD Part
model = TBD Model
serial = TBD Serial
wpn = TBD WPN
feature = 0
mods =
date = 0-00-00 00:00:00
hours = 0
key1 = 0
key2 = 0
key3 = 0
key4 = 0
key5 = 0
key5 = 0
key6 = 0
key7 = 0
key8 = 0
14

BBBB 4581 XXX.YYY.ZZZ.15
part = TBD Part
model = TBD Model
serial = TBD Serial
wpn = TBD WPN
feature = 0
mods =
date = 0-00-00 00:00:00
hours = 0
key1 = 0
key2 = 0
key3 = 0
key4 = 0
key5 = 0
key5 = 0
key6 = 0
key7 = 0
key8 = 0
14

请注意,脚本中的第一个循环只能执行print s - f[]数组仅用于存储字段(输出行),以便它们可供您使用你想在识别字段后再对字段做任何其他事情。

编辑:我从你的一条评论中看到你的输入格式实际上并不是你在问题中发布的内容。它看起来像这样:

$ cat file
BNSF 4575 10.145.181.191 ['part = TBD Part', 'model = TBD Model', 'serial = TBD Serial', 'wpn = TBD WPN', 'feature = 0', 'mods = ', 'date = 0-00-00 00:00:00', 'hours = 0', 'key1 = 0', 'key2 = 0', 'key3 = 0', 'key4 = 0', 'key5 = 0', 'key6 = 0', 'key7 = 0', 'key8 = 0', '14']

这使得解决方案变得更加简单:

$ awk -F"', '" -v OFS='\n' -v ORS='\n\n' '{sub(/ \[\047/,OFS,$1); sub(/\047\]/,"",$NF)}1' file
BNSF 4575 10.145.181.191
part = TBD Part
model = TBD Model
serial = TBD Serial
wpn = TBD WPN
feature = 0
mods =
date = 0-00-00 00:00:00
hours = 0
key1 = 0
key2 = 0
key3 = 0
key4 = 0
key5 = 0
key6 = 0
key7 = 0
key8 = 0
14

在发布问题时,它总是最好的,以显示您的数据真实情况而非不同之处。

答案 2 :(得分:0)

试试这个sed one-liner:

sed -r 's/\S+\s*$/\n&/;s/\S+\s=/\n&/g' file

此行为您提供预期的输出。