Replace with condition


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace with condition
# 1  
Old 05-17-2013
Replace with condition

I have file input
Code:
a,b,b,b,ee,b3012dasda,53012,203,1,END,START
b,c,b,b,ee,sdasdad,57483,203,,END,START
a,b,b,b,ee,b3012dasda,53012,203,,END,START
b,c,b,b,ee,sdasdad,57483,203,1,END,START

I want every ",," in $9 replace with ,0,

the output should be

Code:
a,b,b,b,ee,b3012dasda,53012,203,1,END,START
b,c,b,b,ee,sdasdad,57483,203,0,END,START
a,b,b,b,ee,b3012dasda,53012,203,0,END,START
b,c,b,b,ee,sdasdad,57483,203,1,END,START

# 2  
Old 05-17-2013
try
Code:
 
awk -F, '!$9{$9=0}{OFS=","}1' filename

# 3  
Old 05-17-2013
Hi vidyadhar85 ,

Would you please mind explaining the relevance of the trailing last '1' digit?
I understand the relevant of defining the delimiter and defining a variable value into the ninth column if none is present, but what is the purpose of the '1' ??

awk -F, '!$9{$9=0}{OFS=","}1' filename

Thanks
# 4  
Old 05-17-2013
To conserve space, programmers normally use 1 instead of print to print each record. Either one will work.

1 == true. In awk when true the default action is to print record.
Code:
awk 1 file

is same as:
Code:
awk '{print}' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a text and if condition matches then replace it

Need a script that can find text in a file and replace it accordingly. This is the file I have: while IFS=',' read -r f1 f2 f3 do { nohup /home/testuser/dbmaintenance/sys_offline_maintenance.sh $f1 $f2 $f3 > $f2.out & } done < "/home/testuser/dbmaintenance/week1offlineserver.txt" In... (4 Replies)
Discussion started by: singhhe
4 Replies

2. UNIX for Beginners Questions & Answers

awk replace cells with NaN/delete if condition

Hello, I will like to delete/replace $3 with NaN. condition $3>-2000 (file1.dat) to produce out.dat. I want to retain the structure of the table. I use this code, this output only $3. Any idea on how to modify this code. Thank you. awk -v OFS='' '{for(i=1; i<=NF; i++) if ($i > -2000 || $i ==" >... (4 Replies)
Discussion started by: geomarine
4 Replies

3. Shell Programming and Scripting

awk to replace a specific field in certain condition

Hi, I have a file like below PRUM,67016800 ,CC ,C1,67016800 , ,Y,Y,2 ,CK,BX,FOX ,00000001,EA,00000001,20141120 00:00:00, ,N,Y,Y,CK ABCDEF... (7 Replies)
Discussion started by: mady135
7 Replies

4. Shell Programming and Scripting

Replace Char in XML Base on Condition

HI All Here is my Input file A. I want to add extra char in the line base on below condition. 1> if Below second line is <xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType> then <xn:VsDataContainer id= will be <xn:VsDataContainer_id_1= 2 > if Below second line is... (4 Replies)
Discussion started by: asavaliya
4 Replies

5. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

6. Emergency UNIX and Linux Support

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (3 Replies)
Discussion started by: nithins007
3 Replies

7. Shell Programming and Scripting

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (6 Replies)
Discussion started by: nithins007
6 Replies

8. Shell Programming and Scripting

Replace text inside XML file based on condition

Hi All, I want to change the name as SEQ_13 ie., <Property Name="Name">SEQ_13</Property> when the Stage Type is PxSequentialFile ie., <Property Name="StageType">PxSequentialFile</Property> :wall: Input.XML <Main> <Record Identifier="V0S13" Type="CustomStage" Readonly="0">... (3 Replies)
Discussion started by: kmsekhar
3 Replies

9. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

10. Shell Programming and Scripting

script to replace numbers on lines according to condition on the same line

hello everyone my file contains many records, the following is a sample: BEGIN ASX1500000050002010120000000308450201012000177 ASX1100002000000201012000000038450201012000220 ASX1600100005000201012000000038450020101200177 ASX1900100006000201067000000058450020101200177... (2 Replies)
Discussion started by: neemoze
2 Replies
Login or Register to Ask a Question