Decision based on field value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Decision based on field value
# 1  
Old 05-07-2011
Decision based on field value

Hi, I need to change the 3rd field value based on the 4th field value using a shell script.
here are the fields;
05:15-23:59; 05:15-21:00; 11:00-18:00; TGSI; TGSummaryInfo_C ; ENTER VALID GAC
05:15-23:59; 05:15-21:00; 11:00-18:00; TGSI; TGSummaryInfo_E ; ENTER VALID GAC
05:15-23:59; 05:15-21:00; 11:00-18:00; TGSI; TGSummaryInfo_W ; ENTER VALID GAC

If 4th field is TGSummaryInfo_C (last character is "C"), i need to change the 3rd filed value from TGSI to TGSI_C; if 4th field is TGSummaryInfo_E
then i need to change 3rd filed value from TGSI to TGSI_E and so on.

Requesting help.

Regards,
Sam

Last edited by sam_bd; 05-07-2011 at 03:12 AM..
# 2  
Old 05-07-2011
Try this

Code:
awk '{split($5,arr,"_");sub(";","_"arr[2]";",$4);print}' file


Last edited by ahamed101; 05-07-2011 at 04:01 AM..
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 05-11-2011
Thanks Ahamed. Sorry for late reply. Was out of town.
Will try out and tell the result ASAP

---------- Post updated at 10:22 AM ---------- Previous update was at 09:03 AM ----------

Hi Ahamad, Thank you. The code worked.Smilie
But it changed other lines too...
for eg.
05:15-23:59; 05:15-21:00; 11:00-18:00; RCMAK_C_C; RetCntMake_C ; <qb:ErrorStatus>S</qb:ErrorStatus>
I want it to change for the above mentioned 3 lines only. Coz the file contains around 30 such entries. Any method to make it possible?

Last edited by sam_bd; 05-11-2011 at 01:58 AM..
# 4  
Old 05-11-2011
Whats the position of these lines?
If they are the first 3 entries then try this
Code:
awk 'NR<=3{split($5,arr,"_");sub(";","_"arr[2]";",$4);print}' file

regards,
Ahamed
# 5  
Old 05-12-2011
Hi Ahamed,
The position of these lines is 16,17,18 in the file.

Regards,
Sam
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. Shell Programming and Scripting

Replacing field based on the value of other field

Hi Team, In the below input file, if I have the value 23,24,25 then for those records 1st field value should get updated from "a" to "b". I also want to pass these values in file as input as it can be done dynamically. Tried awk commands but not getting desired output.Using SunOS 5.10 version.... (14 Replies)
Discussion started by: weknowd
14 Replies

3. Shell Programming and Scripting

awk to adjust coordinates in field based on sequential numbers in another field

I am trying to output a tab-delimited result that uses the data from a tab-delimited file to combine and subtract specific lines. If $4 matches in each line then the first matching sequential $6 value is added to $2, unless the value is 1, then the original $2 is used (like in the case of line... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

awk to update value in field based on another field

In the tab-delimeted input file below I am trying to use awk to update the value in $2 if TYPE=ins in bold, by adding the value of HRUN= in italics. In the below since in line 1 TYPE=ins the 117282541 value in $2 has 6 added because that is the value of HRUN=. Hopefully the awk is a start but I... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. UNIX for Dummies Questions & Answers

Grep based on first field

I want to grep based on first column and print all the columns. for eg. root 12344 /sh root 33389 /bash oracle 87378 /tech/oracle oracle 26367 /tmp/oracle Now I want to grep based on user "root" in first column and print like below. root ... (14 Replies)
Discussion started by: baladelaware73
14 Replies

6. Shell Programming and Scripting

Sorting based on the second field

Oracle Enterprise Linux 6 This is my file. Two fields separated by space $ cat testfile.txt MARCH9 MARCH4 MARCH1 MARCH5 MARCH2 MARCH326 MARCH821 MARCH7 MARCH6 MARCH2 $ $ The following numeric sort, based on the first field's 6th character works as expected. $ $ sort -n -k 1.6... (7 Replies)
Discussion started by: John K
7 Replies

7. Shell Programming and Scripting

Uniq based on first field

Hi New to unix. I want to display only the unrepeated lines from a file using first field. Ex: 1234 uname1 status1 1235 uname2 status2 1234 uname3 status3 1236 uname5 status5 I used sort filename | uniq -u output: 1234 uname1 status1 1235 uname2 status2 1234 uname3 status3 1236... (10 Replies)
Discussion started by: venummca
10 Replies

8. UNIX for Dummies Questions & Answers

awk - Summing a field based on another field

So, I need to do some summing. I have an Apache log file with the following as a typical line: 127.0.0.1 - frank "GET /apache_pb.gif HTTP/1.0" 200 2326 Now, what I'd like to do is a per-minute sum. So, I can have awk tell me the individual minutes, preserving the dates(since this is a... (7 Replies)
Discussion started by: treesloth
7 Replies

9. Shell Programming and Scripting

Find top N values for field X based on field Y's value

I want to find the top N entries for a certain field based on the values of another field. For example if N=3, we want the 3 best values for each entry: Entry1 ||| 100 Entry1 ||| 95 Entry1 ||| 30 Entry1 ||| 80 Entry1 ||| 50 Entry2 ||| 40 Entry2 ||| 20 Entry2 ||| 10 Entry2 ||| 50... (1 Reply)
Discussion started by: FrancoisCN
1 Replies
Login or Register to Ask a Question