Reputation: 49
I have data that looks like this:
TEST1
AAAAA MUTUAL
BBBBB MUTUAL
CCCCC MUTUAL
TEST2
DDDDD SINGLE
EEEEE SINGLE
FFFFF SINGLE
how do change with Shell script in Unix Solaris ?
AAAAA TEST1
BBBBB TEST1
CCCCC TEST1
DDDDD TEST2
EEEEE TEST2
FFFFF TEST2
Upvotes: 0
Views: 160
Reputation: 16997
awk 'NF==1{val=$1; next}{ $2 = val}1' infile
# or
awk 'NF==1{val=$1; next}{ print $1, val }' infile
If you want to try this on a Solaris/SunOS system, change awk at the start of the script to /usr/xpg4/bin/awk
or /usr/xpg6/bin/awk
or nawk
Explanation:
NF==1{val=$1; next}
if no of fields in record/line/row equal to 1, then save first column in variable val
next
stop processing further condition, go to next line
{ $2 = val}
set saved value to second field ( so lines/records/rows which are not skipped will be modified here )
1
at the end does default operation that is print current/record/row, print $0. To know how awk works try, awk '1' infile, which will print all records/lines, whereas awk '0' infile prints nothing. Any number other than zero is true, which triggers the default behavior.
Upvotes: 2
Reputation: 133528
Could you please try following and let me know if this helps.
nawk '/^TEST/{val=$0;next} {print $1,val}' Input_file
Upvotes: 0