how to flip values of two columns and add an extra column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to flip values of two columns and add an extra column
# 1  
Old 10-17-2008
Bug 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  
Old 10-17-2008
awk '{if( $2 > $3) {tmp=$2; $2=$3;$3=tmp " Rev_Com";} print}' Input_File
# 3  
Old 10-17-2008
On what criteria is row 2 spared out of it?
# 4  
Old 10-20-2008
Thanks

Thanks ranjithpr! Its working fine.

Hi zaxxon,

row two is spared out because its column2<column3.
# 5  
Old 10-20-2008
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;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy columns from one file into another and get sum of column values and row count

I have a file abc.csv, from which I need column 24(PurchaseOrder_TotalCost) to get the sum_of_amounts with date and row count into another file say output.csv abc.csv- UTF-8,,,,,,,,,,,,,,,,,,,,,,,,, ... (6 Replies)
Discussion started by: Tahir_M
6 Replies

2. Shell Programming and Scripting

Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns

Hello All, I have a requirement in which i will be given a sql query as input in a file with dynamic number of columns. For example some times i will get 5 columns, some times 8 columns etc up to 20 columns. So my requirement is to generate a output query which will have 20 columns all the... (7 Replies)
Discussion started by: vikas_trl
7 Replies

3. Shell Programming and Scripting

Splitting the numeric vs alpha values in a column to distinct columns

How could i take an input file and split the numeric values from the alpha values (123 vs abc) to distinc columns, and if the source is blank to keep it blank (null) in both of the new columns: So if the source file had a column like: Value: |1 | |2.3| | | |No| I would... (7 Replies)
Discussion started by: driftlogic
7 Replies

4. Linux

To get all the columns in a CSV file based on unique values of particular column

cat sample.csv ID,Name,no 1,AAA,1 2,BBB,1 3,AAA,1 4,BBB,1 cut -d',' -f2 sample.csv | sort | uniq this gives only the 2nd column values Name AAA BBB How to I get all the columns of CSV along with this? (1 Reply)
Discussion started by: sanvel
1 Replies

5. Shell Programming and Scripting

Add extra column if no column are less

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 File A.txt 11 LAL01A_1 1213 12 0 0 7 30 2 -122 1 300 14854 5000 2 221 LAL01A_1 1313 14 0 0 7 30 1 -122 2 300 14854 5000 2 12 LAL01A_1 1234 15 0 0 7 30 0 -122 1 300 14854 5000 2 I have file A.txt now i want to use if condition : if thr... (3 Replies)
Discussion started by: pareshkp
3 Replies

6. Shell Programming and Scripting

Add the values in second and third columns with group by on first column.

Hi All, I have a pipe seperated file. I need to add the values in second and third columns with group by on first column. MYFILE_28012012_1115|47|173.90 MYFILE_28012012_1115|4|0.00 MYFILE_28012012_1115|6|22.20 MYFILE_28012012_1116|47|173.90 MYFILE_28012012_1116|4|0.00... (3 Replies)
Discussion started by: angshuman
3 Replies

7. Shell Programming and Scripting

Average of columns with values of other column with same name

I have a lot of input files that have the following form: Sample Cq Sample Cq Sample Cq Sample Cq Sample Cq 1WBIN 23.45 1WBIN 23.45 1CVSIN 23.96 1CVSIN 23.14 S1 31.37 1WBIN 23.53 1WBIN 23.53 1CVSIN 23.81 1CVSIN 23.24 S1 31.49 1WBIN 24.55 1WBIN 24.55 1CVSIN 23.86 1CVSIN 23.24 S1 31.74 ... (3 Replies)
Discussion started by: isildur1234
3 Replies

8. Shell Programming and Scripting

Selecting lowest and highest values in columns 1 and 2, based on subsets in column 3

Hi, I have a file with the following columns: 361459 447394 CHL1 290282 290282 CHL1 361459 447394 CHL1 361459 447394 CHL1 178352861 178363529 AGA 178352861 178363529 AGA 178363657 178363657 AGA Essentially, using CHL1 as an example. For any line that has CHL1 in... (2 Replies)
Discussion started by: hubleo
2 Replies

9. UNIX for Dummies Questions & Answers

Add extra columns help

Hi Gurus, This below script adds a column extra to my flat file..But how can i add another column, Say if i just put mention an other column beside the first column..it does get generated but as one column only while IFS="" read r; do printf "dummy\t%s\n" "$r" done < xxx.txt > zzz.txt ... (9 Replies)
Discussion started by: saggiboy10
9 Replies

10. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies
Login or Register to Ask a Question