Just as I said in your previous thread (speaking of which, it's generally good manners to post follow-ups to any replies you get, to let us know whether it was useful to you), awk is not always the proper tool to use.
Whether or not you
want to use something else shouldn't be your main consideration; good scripters know all of the common tools at their disposal, and can choose the best one for the job at hand. If you don't know at least the basic uses of sed, then you should take some time out to learn it.
Like I said before, awk is
field-based, which means that each of the
$1,
$2, parameters holds one word from the input line,
discarding the whitespace that separated them. It then inserts
new delimiters when printing, which is why your output came out formatted that way.
It's sometimes possible to play around with awk's field separator variables and other advanced features (I'm thinking specifically of gawk's RT variable) in order to manipulate the whitespace, but that's often more trouble than it's worth.
In any case, your current problem isn't really field-related. If all you want to do is add something to the end of the line, then
just add it to the end of the line. sed is perfect for doing this, so I suggest it again:
Code:
sed 's/$/ field5 field6/' file.txt
Where
$ is the line-end regex anchor. The replacement text is the entire literal string you want to tack on, including whitespace.
Actually though, awk
can be easily used here too. Since the
$0 parameter contains the entire unaltered line, you can simply print
$0 and whatever you want to come after it.
Code:
awk '{print $0" field5 field6"}' file.txt
Be sure to quote the string so that it prints out literally, otherwise the whitespace will be dropped and the words be treated as (empty) variables, meaning nothing will be printed.