LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-12-2011, 07:04 AM   #1
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
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!
 
Old 10-12-2011, 07:37 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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?
 
0 members found this post helpful.
Old 10-12-2011, 09:52 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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?
 
1 members found this post helpful.
Old 10-13-2011, 06:14 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

Last edited by David the H.; 10-13-2011 at 06:17 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk on number columns and format problem cristalp Programming 3 08-30-2011 06:14 AM
extracting columns with awk gav251 Programming 7 03-10-2011 08:40 AM
AWK - combining multiple columns AlexYZ Programming 5 02-24-2010 07:09 AM
Using awk to switch columns bioinformatics_guy Linux - Newbie 3 10-30-2008 09:50 AM
Moving columns with sed or awk? btm Linux - Newbie 4 09-27-2007 02:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration