awk transpose lines based on pattern and move (copy) before current columns with filling empty fields

喜你入骨 提交于 2021-01-29 08:14:55

问题


Hi i am tring to move with this below formatted text

input:

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

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


回答1:


awk '{if($1=="NICK")
        {line=$0}
      else
        {if(NF==2)
           {print line,$0,"EMPTY"}
         else
           {print line,$0}
        }
     }' file

Output:

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 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR




回答2:


I think this is what you need

$ awk '/^NICK/ {p=$0; next} 
               {print p,$0,(NF>2?"":"EMPTY")}' 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


来源:https://stackoverflow.com/questions/53620717/awk-transpose-lines-based-on-pattern-and-move-copy-before-current-columns-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!