How to replace the 10th column?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace the 10th column?
# 1  
Old 03-15-2015
Wrench How to replace the 10th column?

how to replace the 10th colum? Each line begins similarly, but they all ends variously.


Code:
Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,Col16,Col17
2015/03/14 15:00:04,2015/03/14 15:00:04,0,Server05,Omega-God,Beta_G,101011247,1,1,24,7,0,,339083645,1,1,
2015/03/14 15:13:48,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,5,1,8,7,0,,319207661,1,1,103510841
2015/03/14 15:14:55,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,6,1,24,7,0,,319207661,1,1,
2015/03/14 15:29:56,2015/03/14 09:40:38,0,Server03,Omega-God,Beta_G,101011247,7,1,24,7,0,,319207661,1,1,
2015/03/14 15:50:53,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,8,1,8,7,0,,319207661,1,1,103510841
2015/03/14 18:50:58,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,12,0,0,0,1,1,319207661,1,1,103510841
2015/03/14 18:51:03,2015/03/14 09:40:38,1,Server01,101011247,Server04,Omega-God,1,0,0,0,1,1,347955199,1,1,
2015/03/14 18:51:11,2015/03/14 15:05:19,1,Server04,101011247,Server04,Omega-God,1,0,0,0,1,1,347963352,1,1,

anyway, I want the above text file to look like this:
if field 10 = 24 replace witch Keyword1
if field 10 = 8 replace witch Keyword2
if field 10 = 0 replace witch Keyword3


Code:
Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,Col16,Col17
2015/03/14 15:00:04,2015/03/14 15:00:04,0,Server05,Omega-God,Beta_G,101011247,1,1,Keyword1,7,0,,339083645,1,1,
2015/03/14 15:13:48,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,5,1,Keyword2,7,0,,319207661,1,1,103510841
2015/03/14 15:14:55,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,6,1,Keyword1,7,0,,319207661,1,1,
2015/03/14 15:29:56,2015/03/14 09:40:38,0,Server03,Omega-God,Beta_G,101011247,7,1,Keyword1,7,0,,319207661,1,1,
2015/03/14 15:50:53,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,8,1,Keyword2,7,0,,319207661,1,1,103510841
2015/03/14 18:50:58,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,12,0,Keyword3,0,1,1,319207661,1,1,103510841
2015/03/14 18:51:03,2015/03/14 09:40:38,1,Server01,101011247,Server04,Omega-God,1,0,Keyword3,0,1,1,347955199,1,1,
2015/03/14 18:51:11,2015/03/14 15:05:19,1,Server04,101011247,Server04,Omega-God,1,0,Keyword3,0,1,1,347963352,1,1,

Any ideas? I searched and found extensive sed/awk tricks..but this (I think) is simpler.
# 2  
Old 03-15-2015
Hi, try:
Code:
awk -F, 'BEGIN{OFS=FS;A[0]="keyword3";A[8]="keyword2";A[24]="keyword1"};A[$10] ? $10=A[$10] : 1' input.txt

Regards.
This User Gave Thanks to disedorgue For This Post:
# 3  
Old 03-15-2015
Try:
Code:
awk '$10==24{$10="keyword1"} $10==8{$10="keyword2"} $10==0{$10="keyword3"}1' FS=, OFS=, file

Code:
Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,Col16,Col17
2015/03/14 15:00:04,2015/03/14 15:00:04,0,Server05,Omega-God,Beta_G,101011247,1,1,keyword1,7,0,,339083645,1,1,
2015/03/14 15:13:48,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,5,1,keyword2,7,0,,319207661,1,1,103510841
2015/03/14 15:14:55,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,6,1,keyword1,7,0,,319207661,1,1,
2015/03/14 15:29:56,2015/03/14 09:40:38,0,Server03,Omega-God,Beta_G,101011247,7,1,keyword1,7,0,,319207661,1,1,
2015/03/14 15:50:53,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,8,1,keyword2,7,0,,319207661,1,1,103510841
2015/03/14 18:50:58,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,12,0,keyword3,0,1,1,319207661,1,1,103510841
2015/03/14 18:51:03,2015/03/14 09:40:38,1,Server01,101011247,Server04,Omega-God,1,0,keyword3,0,1,1,347955199,1,1,
2015/03/14 18:51:11,2015/03/14 15:05:19,1,Server04,101011247,Server04,Omega-God,1,0,keyword3,0,1,1,347963352,1,1,


Last edited by Scrutinizer; 03-15-2015 at 11:11 AM.. Reason: Added output
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-15-2015
awk -f oo.awk myFile, where oo.awk is:
Code:
BEGIN {
  FS=OFS=","
  f2r=10
  split("24,8,0",fA,FS)
  split("Keyword1,Keyword2,Keyword3", fAr,FS)
}
FNR>1{
  for(i=1; i in fA;i++)
    if ($f2r == fA[i]) {
      $f2r=fAr[i]
      break
   }
}
1

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 03-15-2015
Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk '$10==24{$10="keyword1"} $10==8{$10="keyword2"} $10==0{$10="keyword3"}1' FS=, OFS=, file

Here, it should not be value "keyword1" is equal to "8" by example...

Regards.
This User Gave Thanks to disedorgue For This Post:
# 6  
Old 03-15-2015
I don't know what you mean Disedorgue. My script does not compare "keyword1" to "8"
I added the output of the command in my post, which is what the OP required.
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 03-15-2015
Imagine that the keyword1 is "8" , keyword2 is "5" and keyword3 is "6":
Code:
$ awk '$10==24{$10="8"} $10==8{$10="5"} $10==0{$10="6"}1' FS=, OFS=, input.txt 
Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,Col16,Col17
2015/03/14 15:00:04,2015/03/14 15:00:04,0,Server05,Omega-God,Beta_G,101011247,1,1,5,7,0,,339083645,1,1,
2015/03/14 15:13:48,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,5,1,5,7,0,,319207661,1,1,103510841
2015/03/14 15:14:55,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,6,1,5,7,0,,319207661,1,1,
2015/03/14 15:29:56,2015/03/14 09:40:38,0,Server03,Omega-God,Beta_G,101011247,7,1,5,7,0,,319207661,1,1,
2015/03/14 15:50:53,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,8,1,5,7,0,,319207661,1,1,103510841
2015/03/14 18:50:58,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,12,0,6,0,1,1,319207661,1,1,103510841
2015/03/14 18:51:03,2015/03/14 09:40:38,1,Server01,101011247,Server04,Omega-God,1,0,6,0,1,1,347955199,1,1,
2015/03/14 18:51:11,2015/03/14 15:05:19,1,Server04,101011247,Server04,Omega-God,1,0,6,0,1,1,347963352,1,1,

Code:
$ awk -F, 'BEGIN{OFS=FS;A[0]="6";A[8]="5";A[24]="8"};A[$10] ? $10=A[$10] : 1' input.txt 
Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,Col16,Col17
2015/03/14 15:00:04,2015/03/14 15:00:04,0,Server05,Omega-God,Beta_G,101011247,1,1,8,7,0,,339083645,1,1,
2015/03/14 15:13:48,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,5,1,5,7,0,,319207661,1,1,103510841
2015/03/14 15:14:55,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,6,1,8,7,0,,319207661,1,1,
2015/03/14 15:29:56,2015/03/14 09:40:38,0,Server03,Omega-God,Beta_G,101011247,7,1,8,7,0,,319207661,1,1,
2015/03/14 15:50:53,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,8,1,5,7,0,,319207661,1,1,103510841
2015/03/14 18:50:58,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,12,0,6,0,1,1,319207661,1,1,103510841
2015/03/14 18:51:03,2015/03/14 09:40:38,1,Server01,101011247,Server04,Omega-God,1,0,6,0,1,1,347955199,1,1,
2015/03/14 18:51:11,2015/03/14 15:05:19,1,Server04,101011247,Server04,Omega-God,1,0,6,0,1,1,347963352,1,1,

Regards.
This User Gave Thanks to disedorgue For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. Shell Programming and Scripting

Change 10th column formatting.

Hi Experts, I created a powershell script to fetch user details from active directory and export it to a csv/excel. "abc","bcd","devloper;admin" "bcd","dca","tester;QA;admin" So here user abc is member of devloper and admin group and user bcd is a member of tester,QA and admin... (6 Replies)
Discussion started by: shekhar_4_u
6 Replies

3. Shell Programming and Scripting

Get extract and replace column with link in a column where it exists

hi i have sample data a,b,c,d,e,g h http://mysite.xyx z,b,d,f,e,s t http://123124# a,b,c,i,m,nothing d,i,j,e,w,nothing output expected is a,b,c,d,e,http://mysite.xyx z,b,d,f,e,http://123124# a,b,c,i,m,nothing d,i,j,e,w,nothing i can get only links using grep -o 'http.*' i... (8 Replies)
Discussion started by: zozoo
8 Replies

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

5. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

6. UNIX and Linux Applications

Replace string in unix from 10th column onwards

Hi All, I need to replace the last 19 bytes of the following string My_Org_Testing_20090102_231124.txt (Text_Date_Time.txt). I would like to derive the current time using "date +%Y%m%d_%H%M%S.txt" and replace the last 19 bytes of the above string I would appreciate if someone could... (3 Replies)
Discussion started by: rpk2008
3 Replies

7. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies

8. UNIX for Advanced & Expert Users

replace a column values with the first value in column

Hi All, I have a file which has data in following format: "Body_Model","2/1/2007","2/1/2007" "CSCH74","0","61" "CSCS74","0","647" "CSCX74","0","3" "CSYH74","0","299" "CSYS74","0","2514" "CSYX74","0","3" "Body_Model","3/1/2007","3/1/2007" "CSCH74","0","88" "CSCS74","0","489"... (3 Replies)
Discussion started by: sumeet
3 Replies

9. Shell Programming and Scripting

Replace 10th Field by 2

Hello, I need some help ....i have a file with many number of fields i want to replace the 10th field of the file by number 2 .Also the delimeter of the file is | . Pls suggest and help.. Regards, (2 Replies)
Discussion started by: PradeepRed
2 Replies

10. Shell Programming and Scripting

Replace 10th column with a new column--- Terriblly hurry

Hi Can any one tell me how to replace the 10th column in a file(comma delimted) with a new file with a single column. Can any one Help me out of the please as soon as possible as i am in a terribley hurry!!!!!! Many THanks, (2 Replies)
Discussion started by: ahmedwaseem2000
2 Replies
Login or Register to Ask a Question