bash - SED incorrectly replaces only the first instance of a pattern on a line -
hello: have tab separated data of form customer-item description-purchase price-category
e.g. a.out contains:
1\t400 bananas\t3.00\tfruit 2\t60 oranges\t0.00\tfruit 3\tnull\t3.0\tfruit 4\tcarrots\tnull\tfruit 5\tnull\tnull\tfruit
i'm attempting rid of null fields. can't rely on simple replacement of string "null" may substring; attempting
sed -i 's:\tnull\t:\t\t:g' a.out when this, end with
1\t400 bananas\t3.00\tfruit 2\t60 oranges\t0.00\tfruit 3\t\t3.0\tfruit 4\tcarrots\t\tfruit 5.\t\tnull\tfruit
what's wrong here #5 has suffered replacement of first instance of search string on each line.
if run sed command twice, end result want:
1\t400 bananas\t3.00\tfruit 2\t60 oranges\t0.00\tfruit 3\t\t3.0\tfruit 4\tcarrots\t\tfruit 5.\t\t\tfruit
where can see line 5 has both of nulls removed don't understand why i'm suffering this?
awk -f'\t' -v ofs='\t' '{ (i = 1; <= nf; ++i) { if ($i == "null") { $i = ""; } } print }' test.txt the straightforward solution use \t field separator , loop on of fields looking exact match of "null". no substringing.
here's same thing 1 liner:
awk -f'\t' -v ofs='\t' '{for(i=1;i<=nf;++i) if($i=="null") $i=""} 1' test.txt
Comments
Post a Comment