awk to replace a specific field in certain condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to replace a specific field in certain condition
# 1  
Old 11-24-2014
awk to replace a specific field in certain condition

Hi,

I have a file like below
Code:
PRUM,67016800          ,CC ,C1,67016800          ,  ,Y,Y,2 ,CK,BX,FOX                                     ,00000001,EA,00000001,20141120 00:00:00,               ,N,Y,Y,CK ABCDEF ,IN,000000047.0000,000000000.7500,000000005.7500,CI,000000202.6875,LB,000000000.6000,000000000.6000,20705032020722,                   ,A,                 ,20141120 00:00:00,   ,GS1 128   ,1.0000,                              ,09975284
PRUM,504608X           ,CC ,C1,504608X           , N ,Y,Y,2 ,CK,BX,FOX                                     ,00000005,EA,00000005,20141120 00:00:00,                 ,N,Y,Y,CK ABCDEF ,IN,000000010.2500,000000003.5000,000000004.2500,CI,000000152.4688,LB,000000000.5500,000000000.5500,20705032010150,                   ,A,                 ,20141120 00:00:00,   ,GS1 128   ,1.0000,                              ,09975172

Now, I would need to replace filed 6 value with "00" if it has null/blank value (like line 1) or leave it as is (like line 2)

Can anyone help me on this?
# 2  
Old 11-24-2014
Code:
awk -F, '$6=="" {$6="00"} {print}'  infile >newfile

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 11-24-2014
If I read the requirements correctly I think the request was for something more like:
Code:
awk -F, '$6 ~ "^[[:blank:]]*$" {$6="00"} {print}' OFS=, infile >newfile


Last edited by Don Cragun; 11-24-2014 at 11:33 PM.. Reason: Also need OFS
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 11-24-2014
Field 6 is not really null/blank. It is actually 2 spaces. Below regex should match null/blank field 6 as well as field 6 with one or more consecutive spaces.
Code:
awk 'BEGIN {FS=OFS=","} $6 ~ /^ *$/ { $6 = "00" }1' file

# 5  
Old 11-24-2014
thanks a lot! Now, I need to filter out those lines that has field 17(which is a date field) either blank or less than equal to system date.

Appreciate your help.
# 6  
Old 11-24-2014
Quote:
Originally Posted by mady135
thanks a lot! Now, I need to filter out those lines that has field 17(which is a date field) either blank or less than equal to system date.

Appreciate your help.
So, with how we showed you to handle field #6, what have you tried to adapt what we showed you to work on your new problem?
# 7  
Old 11-25-2014
Quote:
Originally Posted by Don Cragun
So, with how we showed you to handle field #6, what have you tried to adapt what we showed you to work on your new problem?
Yes, I adopted the below one
Code:
awk 'BEGIN{FS=",";OFS=","} ($6=="  ") {$6="00"} {print $0}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 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

awk to change contents of field based on condition in same file

In the awk below I am trying to copy the entire contents of $6 there may be multiple values seperated by a ;, to $8, if $8 is . (lines 1 and 3 are examples). If that condition $8 is not . (line2 is an example) then that line is skipped and printed as is. The awk does execute but prints the output... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

awk to count condition in field

I am trying to confirm the counts from another code and tried the below awk, but the syntax is incorrect. Basically, outputting the counts of each condition in $8. Thank you :) awk '$8==/TYPE=snp/ /TYPE=ins/ /TYPE=del/ {count++} END{print count}'... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

Print specific field when condition met

Hi All, Seeking for your assistance to print all the specific field when the condition met. Ex: file1.txt 1|203|3|31243|5341|6452|623|22|00|01 3|45345|123214|6534|3423|6565|643|343|232|10 if field 1 = 1 and field 3 = 3 and field 5 = 5341 and field 6 = 6452 it will print from $1 to $10.... (2 Replies)
Discussion started by: znesotomayor
2 Replies

5. UNIX for Dummies Questions & Answers

How to replace and remove few junk characters from a specific field?

I would like to remove all characters starting with "%" and ending with ")" in the 4th field - please help!! 1412007819.864 /device/services/heartbeatxx 204 0.547%!i(int=0) 0.434 0.112 1412007819.866 /device/services/heartbeatxx 204 0.547%!i(int=1) 0.423 0.123... (10 Replies)
Discussion started by: snemuk14
10 Replies

6. Shell Programming and Scripting

How to print with awk specific field different from specific character?

Hello, i need help with awk. I have this file: cat number DirB port 67 er_enc_out 0 er_bad_os 0 DirB port 71 er_enc_out 56 er_bad_os 0 DirB port 74 er_enc_out 0 er_bad_os 0 DirB port 75 ... (4 Replies)
Discussion started by: elilmal
4 Replies

7. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

8. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

9. Shell Programming and Scripting

Help with Awk finding and replacing a field based on a condition

Hi everybody, I'm trying to replace the $98 field with "T" if the last field (108th) is T I've tried awk 'BEGIN{OFS=FS="|"} {if ($108=="T")sub($98,"T"); print}' test.txt but that doesn't do anything also tried awk 'BEGIN{OFS=FS="|"}{ /*T.$/ sub($98,"T")} { print}' test.txt but... (2 Replies)
Discussion started by: jghi123
2 Replies

10. Shell Programming and Scripting

awk how to replace specific field with new value

I need to replace specific field (x) in a table with new value (y): Input: 1 2 3 4 5 x 6 7 8 9 0 0 Output: 1 2 3 4 5 y 6 7 8 9 0 0 I have no idea how to do this. (10 Replies)
Discussion started by: setepo
10 Replies
Login or Register to Ask a Question