Unix/Linux Go Back    


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 !!

awk for concatenation of column values

UNIX for Dummies Questions & Answers


Tags
awk, concatenate

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-27-2012
Gussifinknottle Gussifinknottle is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 24 April 2015, 8:50 AM EDT
Location: Cambridge, MA
Posts: 76
Thanks: 17
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 Unix and Linux 12-27-2012
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 27 April 2015, 5:07 PM EDT
Location: Galactic Empire
Posts: 3,402
Thanks: 240
Thanked 1,216 Times in 1,142 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 Unix and Linux 12-27-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 28 April 2015, 8:55 AM EDT
Location: Aachen, Germany
Posts: 5,769
Thanks: 101
Thanked 1,524 Times in 1,433 Posts
Try:
Code:
$ awk '$4=$4=="A"?$3$3:$2$3' OFS="\t" file

    #4  
Old Unix and Linux 12-27-2012
manojmalhotra13 manojmalhotra13 is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 28 December 2012, 12:39 AM EST
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 Unix and Linux 12-27-2012
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 27 April 2015, 5:07 PM EDT
Location: Galactic Empire
Posts: 3,402
Thanks: 240
Thanked 1,216 Times in 1,142 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 Unix and Linux 12-30-2012
mukulverma2408 mukulverma2408 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 4 September 2014, 7:17 AM EDT
Posts: 61
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 Unix and Linux 12-30-2012
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 27 April 2015, 10:37 PM EDT
Location: Amsterdam
Posts: 10,126
Thanks: 326
Thanked 2,667 Times in 2,385 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
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 11:56 AM.