Change Data base on Column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change Data base on Column
# 1  
Old 01-12-2015
Change Data base on Column

Base of last two column i want to change may data

if Last two Column have A and C then Copy Column $4 to Column $3.

Input :-

Code:
DD142 0_1 DD142_A DD142_B A B
DD142 1_1 DD142_B DD142_C B C
DD142 2_1 DD142_A DD142_C A C
DD142 3_1 DD142_A  A 
DD142 3_2 DD142_A  A 
DD142 4_1 DD142_B  B 
DD142 4_2 DD142_B  B 
DD142 5_1 DD142_C  C 
DD142 5_2 DD142_C  C 
DD709 0_1 DD709_A DD709_B A B
DD709 1_1 DD709_B DD709_C B C
DD709 2_1 DD709_A DD709_C A C

Output :-

Code:
DD142 0_1 DD142_A 
DD142 1_1 DD142_B 
DD142 2_1 DD142_C
DD142 3_1 DD142_A  
DD142 3_2 DD142_A  
DD142 4_1 DD142_B  
DD142 4_2 DD142_B  
DD142 5_1 DD142_C  
DD142 5_2 DD142_C  
DD709 0_1 DD709_A 
DD709 1_1 DD709_B 
DD709 2_1 DD709_C

# 2  
Old 01-12-2015
Hi,
A awk solution:
Code:
$ cat finput.data 
DD142 0_1 DD142_A DD142_B A B
DD142 1_1 DD142_B DD142_C B C
DD142 2_1 DD142_A DD142_C A C
DD142 3_1 DD142_A  A 
DD142 3_2 DD142_A  A 
DD142 4_1 DD142_B  B 
DD142 4_2 DD142_B  B 
DD142 5_1 DD142_C  C 
DD142 5_2 DD142_C  C 
DD709 0_1 DD709_A DD709_B A B
DD709 1_1 DD709_B DD709_C B C
DD709 2_1 DD709_A DD709_C A C
$ awk '$(NF-1) == "A" && $NF == "C" {$3=$4}NF=3' finput.data 
DD142 0_1 DD142_A
DD142 1_1 DD142_B
DD142 2_1 DD142_C
DD142 3_1 DD142_A
DD142 3_2 DD142_A
DD142 4_1 DD142_B
DD142 4_2 DD142_B
DD142 5_1 DD142_C
DD142 5_2 DD142_C
DD709 0_1 DD709_A
DD709 1_1 DD709_B
DD709 2_1 DD709_C

Regards.
This User Gave Thanks to disedorgue For This Post:
# 3  
Old 01-13-2015
Quote:
Originally Posted by disedorgue
Hi,
A awk solution:
Code:
$ cat finput.data 
DD142 0_1 DD142_A DD142_B A B
DD142 1_1 DD142_B DD142_C B C
DD142 2_1 DD142_A DD142_C A C
DD142 3_1 DD142_A  A 
DD142 3_2 DD142_A  A 
DD142 4_1 DD142_B  B 
DD142 4_2 DD142_B  B 
DD142 5_1 DD142_C  C 
DD142 5_2 DD142_C  C 
DD709 0_1 DD709_A DD709_B A B
DD709 1_1 DD709_B DD709_C B C
DD709 2_1 DD709_A DD709_C A C
$ awk '$(NF-1) == "A" && $NF == "C" {$3=$4}NF=3' finput.data 
DD142 0_1 DD142_A
DD142 1_1 DD142_B
DD142 2_1 DD142_C
DD142 3_1 DD142_A
DD142 3_2 DD142_A
DD142 4_1 DD142_B
DD142 4_2 DD142_B
DD142 5_1 DD142_C
DD142 5_2 DD142_C
DD709 0_1 DD709_A
DD709 1_1 DD709_B
DD709 2_1 DD709_C

Regards.
Hello disedorgue,

If Number of fields are less than 6 and last 2 columns have values A and C so above solution will work then
also(which I guess pareshkp doesn't want seems user want to exchange only DD values) so adding a minor condition to it.
Code:
awk '($(NF-1)=="A" && $NF=="C" && NF==6){$3=$4} NF=3' Input_file

Output will be as follows.
Code:
DD142 0_1 DD142_A
DD142 1_1 DD142_B
DD142 2_1 DD142_C
DD142 3_1 DD142_A
DD142 3_2 DD142_A
DD142 4_1 DD142_B
DD142 4_2 DD142_B
DD142 5_1 DD142_C
DD142 5_2 DD142_C
DD709 0_1 DD709_A
DD709 1_1 DD709_B
DD709 2_1 DD709_C

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 01-13-2015
Thanks to all ,

Both Solution Works Fine Smilie
# 5  
Old 01-13-2015
Both solutions work fine for your case, but RavinderSingh13's solution is most safe because it checks the number of field.

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Column to Row base on first and second Column

Input :- Column to Row Time CT 1 2 3 17:45 X 10 15 20 18:00 X 15 20 30 18:15 X 10 10 10 18:30 X 5 5 5 17:45 Y 10 15 20 18:00 Y 15 20 30 18:15 Y 10 10 10 18:30 Y 5 5 5 output:- Time P X Y 17:45 1 10 10 18:00 1 15 15 18:15 1 10 10 (2 Replies)
Discussion started by: pareshkp
2 Replies

2. Shell Programming and Scripting

Change data in one column with data from another file's column

Hello, I have this file outputData: # cat /tmp/outputData __Capacity^6^NBSC01_Licences^L3_functionality_for_ESB_switch __Capacity^2100^NBSC01_Licences^Gb_over_IP __Capacity^1837^NBSC01_Licences^EDGE_BSS_Fnc __Capacity^1816^NBSC01_Licences^GPRS_CS3_and_CS4... (1 Reply)
Discussion started by: nypreH
1 Replies

3. Shell Programming and Scripting

Grep Data Base on Column

HI Want to grep data from column header and match with second file. File A.txt 1 2 3 4 5 6 X1 A L D J Q R X2 B M K P w T X3 C S L P e Y X4 R Z M A r U FileB.txt 1 2 3 4 6 7 (3 Replies)
Discussion started by: pareshkp
3 Replies

4. Shell Programming and Scripting

Add Column base on other Column Data

HI Guys, I want add one extra Column base on 3rd Column . Input :- M204 MS204_154 :vsDataUeMe M204 MS204_154 es:sMeasure 0 M204 MS204_154 es:90ilterCoe 9 M204 MS204_154 es:searchE9090ortTime 40 M204 MS204_154 es:servOrPrioI90HoTimer 4000 M204 MS204_154 es:ueMeajllls154545 TRUE... (5 Replies)
Discussion started by: pareshkp
5 Replies

5. Shell Programming and Scripting

Combine data from two files base on uniq data

File 1 ID Name Po1 Po2 DD134 DD134_4A_1 NN-1 L_0_1 DD134 DD134_4B_1 NN-2 L_1_1 DD134 DD134_4C_1 NN-3 L_2_1 DD142 DD142_4A_1 NN-1 L_0_1 DD142 DD142_4B_1 NN-2 L_1_1 DD142 DD142_4C_1 NN-3 L_2_1 DD142 DD142_3A_1 NN-41 L_3_1 DD142 DD142_3A_1 NN-42 L_3_2 File 2 ( Combination of... (1 Reply)
Discussion started by: pareshkp
1 Replies

6. Shell Programming and Scripting

Add column base on same name of second column

HI Guys, I have input file A.txt 1 AAA 1 BBB 1 1SW 1 2SW 1 3SW 1 BBB 1 PPP 1 1SW 1 PPP 1 1WS 1 1AS 1 P1P 1 AAA (1 Reply)
Discussion started by: pareshkp
1 Replies

7. UNIX for Dummies Questions & Answers

Des/awk for change format and adding integers in a column of data?

Greetings! I need a quick way to change the format in a table of data Here is an example of the input: 10 72 Value=177 VDB=0.0245 Value4=0,0,171,0 10 274 Value=238 VDB=0.0433 Value4=29,0,205,0 10 312 Value=222 VDB=0.0384 Value4=8,0,190,19 10 540 Value=405 VDB=0.0391 Value4=13,30,153,195... (3 Replies)
Discussion started by: Twinklefingers
3 Replies

8. Shell Programming and Scripting

Copy same name data from other file base on column

HI I have input file A.txt X Y Z File B.txt 1 X 10 AAA 11123 2 Y 22 PlD 4563 3 Z 55 PlD 54645 4 Z 66 PlD 15698 5 F 44 PlD 154798 6 C 55 PlD 12554 7 Z 88 PlD 23265 8 C 99 PlD 151654 9 C 11 PlD 21546546 I need New File C.txt (1 Reply)
Discussion started by: pareshkp
1 Replies

9. Shell Programming and Scripting

Change column to row base on column 2

Hi Guys, Input.txt L194 A -118.2 L194 B -115.1 L194 C -118.7 L196 A 0 L196 C 0 L197 A -111.2 L197 B -118.9 L197 C -119.9 L199 A -120.4 L199 B -119.9 ... (2 Replies)
Discussion started by: asavaliya
2 Replies

10. Shell Programming and Scripting

Using loop reading a file,retrieving data from data base.

Hi All, I am having trouble through, I am reading the input from tab delimited file containing several records, e.g. line1 field1 field2 field3 so on.. line2 field1 field2 field3 so on.. .. .. on the basis of certain fields for each record in input file, I have to retrieve... (1 Reply)
Discussion started by: Sonu4lov
1 Replies
Login or Register to Ask a Question