LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   AWK: add columns while keep format for other columns (http://www.linuxquestions.org/questions/programming-9/awk-add-columns-while-keep-format-for-other-columns-907757/)

cristalp 10-12-2011 07:04 AM

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
Please notice that the spaces between fields are diffferent.

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
It print out
Code:

column1 column2 column3 colmn4 column5 column6
which has only a single space between each two columns. But what I need is to keep the orignal format. The number of spaces should not be changed afeter adding new colomns.

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!

grail 10-12-2011 07:37 AM

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?

crts 10-12-2011 09:52 AM

Hi,

do you mean something like this:
Code:

awk -F='\n' '{$2="field5" ; $3="field6" ;print $0}' file
You will have to be a bit more verbose on the expected formatting of the added fields. Are there supposed to be as many whitespaces as there are between column 2 and 3? Will the number of spaces differ on different rows?

David the H. 10-13-2011 06:14 AM

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.


All times are GMT -5. The time now is 09:46 AM.