I have a file with a list of info. Each line has either 12 or 13 fields. For example I will use a file will 3 and 4 fields.
e.g. myfile.txt
user password location date
john passwd1 new_york today
passwd2 london yesterday
jeff passwd3 new_york tomorrow
I am looking for a way that would take johns name and copy it to the next line in the blank space.
so far I am trying something like:
Awk '{{if(NF==4) print $0; name=$1}; if(NF==3) print $name,$0}' ./myfile.txt >> ./my_editedfile.txt.
Every time I try, however, it either prints the literal $1, nothing, or the first field of the next line twice. i.e. line two will look like on of the following;
$1 passwd2 london yesterday
passwd2 london yesterday
passwd2 passwd2 london yesterday
Can someone help to set a variable on one line and then use it to print on the following one?
print name, $0
...awk '{if (NF==4) name=$1}; {print (NF==3) ? name $0 : $0 }' file