shell - bash loop through file replace string -


i have file called file.txt contains following:

123 223 lane,id,s_id_sample_id 1,3_range.single_try,n76 2,44_range.single_try,n77 3,92_out_range.double_try,n79 

i loop through file , following:

begin line after 'lane' split using comma , take second column (id) take id column , split on underscore, search , replace dots , underscores 'x' except last 2 underscores. not search , replace last underscore (e.g. double_try).

so end with:

123 223 lane,id,s_id_sample_id 1,3xrange_single_try,n76 2,44xrange_single_try,n77 3,92xoutxrange_double_try,n79 

this have done:

while ifs=',' read -r f1 f2;  sed -e 's/_/x/g;s/\./x/g;s/'  echo "$f1,$f2"  done < "$file" > output mv output $file 

the problem how can specify ignore last 2 underscores?

this works first replacing last 2 dots or underscores '@', replacing remaining dots , underscores 'x', , finally, replacing '@' characters underscores:

ifs=',' while read -r f1 f2 f3;    f2=$(sed 's/[._]\([^._]\+\)[._]\([^._]\+\)$/@\1@\2/;s/[._]/x/g;s/@/_/g' <<< "$f2")   echo -n "$f1"   [[ -n $f2 ]] && echo -n ",$f2"   [[ -n $f3 ]] && echo -n ",$f3"   echo done < "$file" > output mv output "$file" 

if '@' occur in input data, may want use different character. can reasonably sure won't occur in input do.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -