问题描述
我正打算搬到下面这种格式化的文本中
Hi i am tring to move with this below formatted text
输入:
NICK JEFF
NOAA A4:80:15
NOAA A4:80:17
NOOI D0:F2:0B BASE-TREE_IN-M_K
NICK STAN
NOAA C1:46:6B
NOOI D5:75:0C BASE-TREE_OUT_OUT
NICK INDEXER
NOOI D5:75:0C SEAT_25
NOAA C1:46:6B
NICK VUZER
NOAA A4:F2:CD
NOOI D0:F2:2D SEAT_42_FLIGHT
NOAA A4:F2:CD
NICK CAPTAIN
NOOI A4:82:8D
NOAA A4:82:8F
NOOI 40:63:07 SYS-BRAVO_X1
NOOI 40:62:DB SYS-BRAVO_X2
NOOI 40:62:B5 SYS-BRAVO_X3
NOOI D0:47:4A BASE-TREE_OUT_OUT
NOOI 51:30:45 NOBASE-INDEX_OUT_FIF
NOOI A4:82:8D
NOAA A4:82:8F
我尝试了这个,但是它错误地复制了我的价值观
i tried this one but its copying me values wrongly
awk '{for (i=1; i<=1; i++) { print $i " " $1" "$2" "$3}}' input
预期的输出应如下所示:
expected output should be sorted out like below:
NICK JEFF NOAA A4:80:15 EMPTY
NICK JEFF NOAA A4:80:17 EMPTY
NICK JEFF NOOI D0:F2:0B BASE-TREE_IN-M_K
NICK STAN NOAA C1:46:6B EMPTY
NICK STAN NOOI D5:75:0C BASE-TREE_OUT_OUT
NICK INDEXER NOOI D5:75:0C SEAT_25
NICK INDEXER NOAA C1:46:6B EMPTY
NICK VUZER NOAA A4:F2:CD EMPTY
NICK VUZER NOOI D0:F2:2D SEAT_42_FLIGHT
NICK VUZER NOAA A4:F2:CD EMPTY
NICK CAPTAIN NOOI A4:82:8D EMPTY
NICK CAPTAIN NOAA A4:82:8F EMPTY
NICK CAPTAIN NOOI 40:63:07 SYS-BRAVO_X1
NICK CAPTAIN NOOI 40:62:DB SYS-BRAVO_X2
NICK CAPTAIN NOOI 40:62:B5 SYS-BRAVO_X3
NICK CAPTAIN NOOI D0:47:4A BASE-TREE_OUT_OUT
NICK CAPTAIN NOOI 51:30:45 NOBASE-INDEX_OUT_FIF
NICK CAPTAIN NOOI A4:82:8D EMPTY
NICK CAPTAIN NOAA A4:82:8F EMPTY
表示模式始终是缺刻",输入文件的文件读取方向是从上到下,并且像jeff stan这样的昵称被放置(复制),这表示它被设置为第一和第二列,其后是输入的前几列.文本"EMPTY"已添加到空白字段,以保留格式/具有相等的列/字段编号,并填充值
it means pattern is "nick" all the time and file reading direction of input file is from top to down and nickname like jeff stan etc.. is placed (copied) before it means its set as 1st and 2nd columns followed by previous columns from input. Text "EMPTY" is added to empty fields to preserve format/to have equal columns / fields numbers filled with values
推荐答案
awk '{if($1=="NICK")
{line=$0}
else
{if(NF==2)
{print line,$0,"EMPTY"}
else
{print line,$0}
}
}' file
输出:
NICK JEFF NOAA A4:80:15 EMPTY
NICK JEFF NOAA A4:80:17 EMPTY
NICK JEFF NOOI D0:F2:0B BASE-TREE_IN-M_K
NICK STAN NOAA C1:46:6B EMPTY
NICK STAN NOOI D5:75:0C BASE-TREE_OUT_OUT
NICK INDEXER NOOI D5:75:0C SEAT_25
NICK INDEXER NOAA C1:46:6B EMPTY
NICK VUZER NOAA A4:F2:CD EMPTY
NICK VUZER NOOI D0:F2:2D SEAT_42_FLIGHT
NICK VUZER NOAA A4:F2:CD EMPTY
NICK CAPTAIN NOOI A4:82:8D EMPTY
NICK CAPTAIN NOAA A4:82:8F EMPTY
NICK CAPTAIN NOOI 40:63:07 SYS-BRAVO_X1
NICK CAPTAIN NOOI 40:62:DB SYS-BRAVO_X2
NICK CAPTAIN NOOI 40:62:B5 SYS-BRAVO_X3
NICK CAPTAIN NOOI D0:47:4A BASE-TREE_OUT_OUT
NICK CAPTAIN NOOI 51:30:45 NOBASE-INDEX_OUT_FIF
NICK CAPTAIN NOOI A4:82:8D EMPTY
NICK CAPTAIN NOAA A4:82:8F EMPTY
"> 8个强大的Awk内置变量– FS,OFS,RS,ORS,NR,NF,文件名,FNR
这篇关于awk根据模式转置线,并在当前列之前移动(复制)并填充空白字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!