AWK: add columns while keep format for other columns
Dear Experts,
I meat some problems while editing file with AWK. I am really fresh, so if my question seems stupid, please ignore it and forgive me. I do not hope to annoy any body and take too much time from others. So, my question/ I have a file include columns: Code:
column1 column2 column3 colmn4 What I want to do is to add "column5 column6". That is add two more fileds with specific number of spaces between them after the 4th field of each line. I tried: Code:
awk '$5 = filed5 , $6 = filed6 {print}' FILENAME Code:
column1 column2 column3 colmn4 column5 column6 I know may be sed can do this, but I really prefer a AWK solution as I used it more often. It would be greatly appreciqated if there is any solution from you. Thank you for your kind help in advanced! |
It is not so much a silly question but it is the same type of question you have already asked here.
Why have you started a second thread on the same question? |
Hi,
do you mean something like this: Code:
awk -F='\n' '{$2="field5" ; $3="field6" ;print $0}' file |
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 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 |
All times are GMT -5. The time now is 09:48 PM. |