|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Try: Code:
$ awk '$4=$4=="A"?$3$3:$2$3' OFS="\t" file |
|
#4
|
|||
|
|||
|
The script by bipinajith is not giving any output while script by RudiC is working fine.
Manoj |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
Quote:
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 WWGNU/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 WWSunOS 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
|
|||
|
|||
|
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" infilewhat is the use of this 1 |
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
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 | ||
|
![]() |
| Tags |
| awk, concatenate |
| Thread Tools | Search this Thread |
| 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 |
|
|