awk for concatenation of column values | Unix Linux Forums | UNIX for Dummies Questions & Answers

  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 Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-27-2012
Gussifinknottle Gussifinknottle is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 14 March 2014, 2:04 PM EDT
Location: Cambridge, MA
Posts: 68
Thanks: 15
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
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 17 April 2014, 8:35 PM EDT
Location: Galactic Empire
Posts: 3,282
Thanks: 227
Thanked 1,157 Times in 1,094 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
Last Activity: 17 April 2014, 7:46 AM EDT
Location: Aachen, Germany
Posts: 3,514
Thanks: 51
Thanked 819 Times in 782 Posts
Try:
Code:
$ awk '$4=$4=="A"?$3$3:$2$3' OFS="\t" file

    #4  
Old 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 12-27-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 17 April 2014, 8:35 PM EDT
Location: Galactic Empire
Posts: 3,282
Thanks: 227
Thanked 1,157 Times in 1,094 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
mukulverma2408 mukulverma2408 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 28 January 2014, 4:15 AM EST
Posts: 52
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
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 18 April 2014, 3:16 PM EDT
Location: Amsterdam
Posts: 8,874
Thanks: 230
Thanked 2,141 Times in 1,922 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 Thread

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 12:40 AM.