The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep all the columns based on a particular column pmallur UNIX for Advanced & Expert Users 2 09-03-2008 04:51 PM
Convert two column data into 8 columns NickC Shell Programming and Scripting 8 06-28-2008 12:19 PM
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 07:05 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
replace a column values with the first value in column sumeet UNIX for Advanced & Expert Users 3 02-06-2007 01:13 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-17-2008
smriti_shridhar smriti_shridhar is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 47
Smile how to flip values of two columns and add an extra column

Hi guys,

Couldn't find the solution of this problem. Please Help!

I have a file-

Input_File

TC200232 92 30
TC215306 2 74
TC210135 42 14

I want an output file in which if column2>column3, the values are swapped and an additional column with value Rev_Com is added to those rows.

Output_File

TC200232 30 92 Rev_Com
TC215306 2 74
TC210135 14 42 Rev_Com

Thanks in advance.

Last edited by smriti_shridhar; 10-17-2008 at 08:37 AM.. Reason: formatting
  #2 (permalink)  
Old 10-17-2008
ranjithpr ranjithpr is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 157
awk '{if( $2 > $3) {tmp=$2; $2=$3;$3=tmp " Rev_Com";} print}' Input_File
  #3 (permalink)  
Old 10-17-2008
zaxxon's Avatar
zaxxon zaxxon is online now Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,288
On what criteria is row 2 spared out of it?
  #4 (permalink)  
Old 10-20-2008
smriti_shridhar smriti_shridhar is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 47
Thanks

Thanks ranjithpr! Its working fine.

Hi zaxxon,

row two is spared out because its column2<column3.
  #5 (permalink)  
Old 10-20-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
awk :

Code:
awk '{
if($2>$3)
{
	t=$2
	$2=$3
	$3=t
	$4="Rev_Com"
	NF=4
}
	print
}' file
perl:

Code:
open FH,"<file";
while(<FH>){
	@arr=split(" ",$_);
	if ($arr[1]>$arr[2]){
		print $arr[0]," ",$arr[2]," ",$arr[1],"Rev_Com\n";
	}
	else{
		print $_;
	}
}
close FH;
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0