Add Column base on other Column Data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add Column base on other Column Data
# 1  
Old 10-28-2015
Add Column base on other Column Data

HI Guys,

I want add one extra Column base on 3rd Column .

Input :-

Code:
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
M204	MS204_154	:vsDataUeMe	
M204	MS204_154	:vsDataReport	
M204	MS204_154	es:hysteresisA1Prim	10
M204	MS204_154	es:timeToTriggerA1Prim	640
M204	MS204_154	:vsDataReport	
M204	MS204_154	:vsDataRep	
M204	MS204_154	es:a1ThrdRrSc	-113
M204	MS204_154	es:tmoigeASec	640
M204	MS204_154	:vsDataRep

Output :-

Code:
M204	MS204_154	:vsDataUeMe	:vsDataUeMe	
M204	MS204_154	:vsDataUeMe	es:sMeasure	0
M204	MS204_154	:vsDataUeMe	es:90ilterCoe	9
M204	MS204_154	:vsDataUeMe	es:searchE9090ortTime	40
M204	MS204_154	:vsDataUeMe	es:servOrPrioI90HoTimer	4000
M204	MS204_154	:vsDataUeMe	es:ueMeajllls154545	TRUE
M204	MS204_154	:vsDataUeMe	:vsDataUeMe	
M204	MS204_154	:vsDataReport	:vsDataReport	
M204	MS204_154	:vsDataReport	es:hysteresisA1Prim	10
M204	MS204_154	:vsDataReport	es:timeToTriggerA1Prim	640
M204	MS204_154	:vsDataReport	:vsDataReport	
M204	MS204_154	:vsDataRep	:vsDataRep	
M204	MS204_154	:vsDataRep	es:a1ThrdRrSc	-113
M204	MS204_154	:vsDataRep	es:tmoigeASec	640
M204	MS204_154	:vsDataRep	:vsDataRep

3rd Column have string which have same name, which i want add before 3rd Column.
# 2  
Old 10-28-2015
Hi, try:
Code:
awk 'NF==3{s=$3}{$3=s OFS $3}1' OFS='\t' file

# 3  
Old 10-28-2015
Thanks .....its working ...

if any row have 4th column blank then it giving me wrong output.

Input :-

Code:
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
M204	MS204_154	:vsDataUeMe	
M204	MS204_154	:vsDataReport	
M204	MS204_154	es:hysteresisA1Prim	
M204	MS204_154	es:timeToTriggerA1Prim	
M204	MS204_154	:vsDataReport	
M204	MS204_154	:vsDataRep	
M204	MS204_154	es:a1ThrdRrSc	-113
M204	MS204_154	es:tmoigeASec	640
M204	MS204_154	:vsDataRep

Code:
M204	MS204_154	:vsDataUeMe	:vsDataUeMe	
M204	MS204_154	:vsDataUeMe	es:sMeasure	0
M204	MS204_154	:vsDataUeMe	es:90ilterCoe	9
M204	MS204_154	:vsDataUeMe	es:searchE9090ortTime	40
M204	MS204_154	:vsDataUeMe	es:servOrPrioI90HoTimer	4000
M204	MS204_154	:vsDataUeMe	es:ueMeajllls154545	TRUE
M204	MS204_154	:vsDataUeMe	:vsDataUeMe	
M204	MS204_154	:vsDataReport	:vsDataReport	
M204	MS204_154	hysteresisA1Prim	es:hysteresisA1Prim	
M204	MS204_154	hysteresisA1Prim	es:timeToTriggerA1Prim	
M204	MS204_154	:vsDataReport	:vsDataReport	
M204	MS204_154	:vsDataRep	:vsDataRep	
M204	MS204_154	:vsDataRep	es:a1ThrdRrSc	-113
M204	MS204_154	:vsDataRep	es:tmoigeASec	640
M204	MS204_154	:vsDataRep	:vsDataRep

That's Wrong .. I want only that data which have "vsData" String
# 4  
Old 10-28-2015
OK, try:
Code:
awk '$3~/^:/{s=$3}{$3=s OFS $3}1' OFS='\t' file

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 10-28-2015
Perfect !!!!!!!!
# 6  
Old 10-28-2015
This would really compare strings and not rely on field 3's structure:
Code:
awk '
!(REP)          {REP = $3
                 SKIP = 1
                }
                {TMP = $3
                 $3 = REP OFS $3
                }
!SKIP &&
TMP == REP      {REP = ""
                }
                {SKIP = 0
                }
1
' OFS="\t" file
M204    MS204_154    :vsDataUeMe    :vsDataUeMe
M204    MS204_154    :vsDataUeMe    es:sMeasure    0
M204    MS204_154    :vsDataUeMe    es:90ilterCoe    9
M204    MS204_154    :vsDataUeMe    es:searchE9090ortTime    40
M204    MS204_154    :vsDataUeMe    es:servOrPrioI90HoTimer    4000
M204    MS204_154    :vsDataUeMe    es:ueMeajllls154545    TRUE
M204    MS204_154    :vsDataUeMe    :vsDataUeMe
M204    MS204_154    :vsDataReport    :vsDataReport
M204    MS204_154    :vsDataReport    es:hysteresisA1Prim    10
M204    MS204_154    :vsDataReport    es:timeToTriggerA1Prim    640
M204    MS204_154    :vsDataReport    :vsDataReport
M204    MS204_154    :vsDataRep    :vsDataRep
M204    MS204_154    :vsDataRep    es:a1ThrdRrSc    -113
M204    MS204_154    :vsDataRep    es:tmoigeASec    640
M204    MS204_154    :vsDataRep    :vsDataRep

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to add a column of another data from another column?

Hola Como hacer: C:\System\SystemRun.4gl C:\System\SystemRunPrint.4gl C:\System\SystemViews.4gl Resultado: SystemRun.4gl C:\System\SystemRun.4gl SystemRunPrint.4gl C:\System\SystemRunPrint.4gl SystemViews.4gl C:\System\SystemViews.4gl... (0 Replies)
Discussion started by: tlaloc
0 Replies

2. UNIX for Beginners Questions & Answers

How to insert data into black column( Secound Column ) in excel (.XLSX) file using shell script?

Source Code of the original script is down below please run the script and try to solve this problem this is my data and I want it column wise 2019-03-20 13:00:00:000 2019-03-20 15:00:00:000 1 Operating System LAB 0 1 1 1 1 1 1 1 1 1 0 1 (5 Replies)
Discussion started by: Shubham1182
5 Replies

3. 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

4. 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

5. Shell Programming and Scripting

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 :- 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 ... (4 Replies)
Discussion started by: pareshkp
4 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. 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

8. 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

9. Shell Programming and Scripting

Compare 2 files and match column data and align data from 3 column

Hello experts, Please help me in achieving this in an easier way possible. I have 2 csv files with following data: File1 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012... (5 Replies)
Discussion started by: asnandhakumar
5 Replies

10. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies
Login or Register to Ask a Question