Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-27-2012
Registered User
 
Join Date: Jan 2009
Location: Cambridge, MA
Posts: 62
Thanks: 14
Thanked 0 Times in 0 Posts
awk for concatenation of column values

Hello,

I have a table as shown below. I want to concatenate values in col2 and col3 based on a value in col4.



Code:
1	X	Y	A
3	Y	Z	B
4	A	W	B
5	T	W	A


If col4 is A, then I want to concatenate col3 with itself. Otherwise it should concateneate col2 with col3.



Code:
1	X	Y	YY
3	Y	Z	YZ
4	A	W	AW
5	T	W	WW


I can do it with a script. However, is there single line awk to do this?

Thanks,
Guss
Sponsored Links
    #2  
Old 12-27-2012
Yoda's Avatar
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,498
Thanks: 167
Thanked 811 Times in 780 Posts

Code:
awk '$4!="A"{$4=$2$3;}$4=="A"{$4=$3$3;}1' OFS="\t" infile

The Following User Says Thank You to Yoda For This Useful Post:
Gussifinknottle (01-11-2013)
Sponsored Links
    #3  
Old 12-27-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Location: Aachen, Germany
Posts: 2,028
Thanks: 25
Thanked 459 Times in 444 Posts
Try:
Code:
$ awk '$4=$4=="A"?$3$3:$2$3' OFS="\t" file

    #4  
Old 12-27-2012
Registered User
 
Join Date: Jul 2011
Posts: 4
Thanks: 4
Thanked 0 Times in 0 Posts
The script by bipinajith is not giving any output while script by RudiC is working fine.

Manoj
Sponsored Links
    #5  
Old 12-27-2012
Yoda's Avatar
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,498
Thanks: 167
Thanked 811 Times in 780 Posts
Quote:
Originally Posted by manojmalhotra13 View Post
The script by bipinajith is not giving any output while script by RudiC is working fine.
Manoj
I'm not sure which OS and awk you are running! Here is the o/p from 3 different plaforms:-

HP-UX

Code:
awk '$4!="A"{$4=$2$3;}$4=="A"{$4=$3$3;}1' OFS="\t" infile
1       X       Y       YY
3       Y       Z       YZ
4       A       W       AW
5       T       W       WW

GNU/Linux

Code:
gawk '$4!="A"{$4=$2$3;}$4=="A"{$4=$3$3;}1' OFS="\t" infile
1       X       Y       YY
3       Y       Z       YZ
4       A       W       AW
5       T       W       WW

SunOS

Code:
nawk '$4!="A"{$4=$2$3;}$4=="A"{$4=$3$3;}1' OFS="\t" infile
1       X       Y       YY
3       Y       Z       YZ
4       A       W       AW
5       T       W       WW

Sponsored Links
    #6  
Old 12-30-2012
Registered User
 
Join Date: Jul 2012
Posts: 53
Thanks: 12
Thanked 2 Times in 2 Posts
Hi bipinajith,
your code is working fine, could you please also explain it a bit :

Code:
awk '$4!="A"{$4=$2$3;}$4=="A"{$4=$3$3;}1' OFS="\t" infile

what is the use of this 1
Sponsored Links
    #7  
Old 12-30-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,807
Thanks: 147
Thanked 1,771 Times in 1,608 Posts
Everything in awk has the form condition{action} . If the condition evaluates to 1 then the action is performed. If the condition is omitted then the default condition is 1, so the action is always performed. If the action is omitted then the default action is performed, which is {print $0} .

In this case the condition is "1" so that evaluates to 1 and the action is omitted, therefore {print $0} is performed, which is "print the entire record".

Last edited by Scrutinizer; 12-30-2012 at 06:55 AM..
Sponsored Links
Reply

Tags
awk, concatenate

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare values of fields from same column with awk lucasvs UNIX for Dummies Questions & Answers 11 06-20-2012 03:15 AM
Read first column and sum values in second column using awk kidncute UNIX for Dummies Questions & Answers 15 03-13-2012 05:51 AM
for each different entry in column 1 extract maximum values from column 2 in unix/awk Diya123 Shell Programming and Scripting 1 07-19-2011 02:37 PM
How to pick values from column based on key values by usin AWK repinementer Shell Programming and Scripting 16 07-24-2009 08:59 AM
averaging column values with awk johnmillsbro Shell Programming and Scripting 18 01-26-2009 12:20 PM



All times are GMT -4. The time now is 09:11 AM.