0

Here is the file I need to deal with.

ENR1;203023;0000;H606626;001505;EL;00;1234;0000100;00;00;201346100;H6022418;
ENR6;000218;130155;000;00;201443;000000000;000000000;000000000;000000000;
ENR1;203357;0000;H606626;001653;EL;01;2546;0000800;00;00;201346100;H6022418;
ENR6;000218;130155;000;00;201503;000000000;000000000;000000000;000000000;
ENR6;001811;150316;000;00;201624;000000000;000000000;000000000;000000000;
ENR1;203385;0000;H606625;000615;EL;00;5861;0000100;00;00;201346100;H6022418;

I must substitute field 7 of each line. If the value is 00, then I must set it to 01. But only for lines whose first field is ENR1!

awk -F';' -v OFS=';' 'NR>=1{sub("00","01",$7)}1' file > result

This works but also remplace field $7 with lines beginning with ENR6.

How adding the condition (only lines starting with ENR1?)

1

I would try

awk -F';' -v OFS=';' '$7 == "00" && $1 == "ENR1" { $7 = "01" } {print} ' file > result

where

  • $7 == "00" && $1 == "ENR1" { $7 = "01" } only applied to line starting with ENR1, having seventh field equel to 00
  • $7 = "01" set 7th field to 01
  • { print } print every line, including ENR1's after change.

  • you may wish to keep NR>=1 test if real file has a one-line header.

1
  • The check there isn't needed imho. The "shorter" version would be something as: awk -F';' -vOFS=";" '/ENR1/{$7="01"}1' filename – Valentin Bajrami Jul 7 '17 at 13:06
0

Short sed approach:

sed -E 's/^(ENR1;([^;]+;){5})00;/\101;/' file

The output:

ENR1;203023;0000;H606626;001505;EL;01;1234;0000100;00;00;201346100;H6022418;
ENR6;000218;130155;000;00;201443;000000000;000000000;000000000;000000000;
ENR1;203357;0000;H606626;001653;EL;01;2546;0000800;00;00;201346100;H6022418;
ENR6;000218;130155;000;00;201503;000000000;000000000;000000000;000000000;
ENR6;001811;150316;000;00;201624;000000000;000000000;000000000;000000000;
ENR1;203385;0000;H606625;000615;EL;01;5861;0000100;00;00;201346100;H6022418;

  • ^(ENR1;([^;]+;){5})00; - captures lines starting with ENR1 followed by 5 fields ([^;]+;){5} and the 7th field 00;
1
  • -E parameter seems not to be recognized by UNIX (AIX) ... ? – Kiko Netzone Jul 9 '17 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.