使用 awk 和 sed 将空白值替换为之前的第一列值

使用 awk 和 sed 将空白值替换为之前的第一列值
john    math
        science
paul    math
        science
rosy    math
jill    science
rob     math
        science
hary    math

期望的输出:

john    math
john    science
paul    math
paul    science
rosy    math
jill    science
rob     math
rob     science
hary    math

答案1

使用awk, 通过作用于多个字段

$ awk 'NF==1{print p "\t" $1; next} {p=$1} 1' ip.txt
john    math
john    science
paul    math
paul    science
rosy    math
jill    science
rob     math
rob     science
hary    math
  • {p=$1} 1对于单个字段以外的行,保存第一列并打印该行
  • NF==1{print p "\t" $1; next}如果仅存在一个字段,则打印上一个字段tab以及输入行中的字段。next将跳过其余语句并处理下一行


如果tab分离不起作用,请使用column

$ awk 'NF==1{print p,$1; next} {p=$1} 1' ip.txt | column -t
john  math
john  science
paul  math
paul  science
rosy  math
jill  science
rob   math
rob   science
hary  math

答案2

你可以用这种方式做到sed这一点:

sed ':n; N; s/\n[^ ]/&/; tsplit; s/^\([^ ]* *\)\([^ ]*\n\) *\([^ ]*\)$/\1\2\1\3/; :split; h; s/\n.*$//; p; g; s/^.*\n//; bn' test.txt

解释

sed '# Start label for loop
:n
# Read next string to main buffer, separated by "\n"
N
# Split and print first string if second starts with non-space character
s/\n[^ ]/&/
tsplit
# "^\([^ ]* *\)" -- First word with spaces, \1.
# "\([^ ]*\n\)"  -- Second word, \2.
# " *"           -- Spaces in second line, throw them.
# "\([^ ]*\\)"   -- Second word in second line, \3.
s/^\([^ ]* *\)\([^ ]*\n\) *\([^ ]*\)$/\1\2\1\3/
# Splitting
:split
# Send both lines to hold buffer
h
# Delete second line, print first
s/\n.*$//
p
# Lines to main buffer, delete first.
g
s/^.*\n//
# Now second file is first, start loop again.
bn' test.txt

相关内容