0

I have a file composed by lines like these ones:

199240050;0180209199240050;0
199240241;0180209199240241;0
199240207;0180209199240207;0
199240400;0180209199240400;0

I should replace last number with "Active" if it is "0", Or "Inactive" if it is "1".

I've tried with sed but the occurrence of ";0" in the middle of the line gets changed too.

It should be used in a linux bash script.


I tried the solution given by DopeGhoti but it fails:

$ sed 's/0$/active/;s/1$/inactive/'  myLines.txt
800600346                078136521                       active

While the format of the input file should not change.

The second solution appends ";active" at the end, without replacing:

$ awk -F\; 'BEGIN {OFS=";"} { if( $3 == 1 ) { print $1,$2,"inactive" } else { print $1,$2,"active" } }'  myLines.txt
800010654                0295445503                     0;;active

And George Vasiliou's also fails:

$ awk '$NF?$NF="active":$NF="inactive"' FS=';' OFS=';' myLines.txt
active
active
active
active
  • 2
    None of these will change the format. Are you sure the example you show correctly represents your file? Could you have created the file on Windows? – terdon Sep 12 '17 at 11:36
  • Have you checked for possible carriage return \r line endings ?? – George Vasiliou Sep 12 '17 at 11:54
  • My fault, the input file format had been overwritten after several tries and I ignored it, I'm really sorry for that. The solution with sed given by SopeGhoti works greatly !!! Many thanks to everyone ! – mary Sep 12 '17 at 13:18
  • 1
    You need to accept Ghoti solution as an answer to your question with the green tick. – George Vasiliou Sep 16 '17 at 16:25
8

For your given data in a file called input:

$ awk -F\; 'BEGIN {OFS=";"} { if( $3 == 1 ) { print $1,$2,"inactive" } else { print $1,$2,"active" } }' input
199240050;0180209199240050;active
199240241;0180209199240241;active
199240207;0180209199240207;active
199240400;0180209199240400;active

Alternatively, with sed:

$ sed 's/\;0$/active/;s/\;1$/inactive/' input
199240050;0180209199240050;active
199240241;0180209199240241;active
199240207;0180209199240207;active
199240400;0180209199240400;active
  • 1
    Would be better as s/;0$/;active/;s/;1$/;inactive/' to avoid replacing foo;10 with foo;1active – Stéphane Chazelas Sep 12 '17 at 11:46
4

One more awk, more idiomatic :

$ awk '$NF?$NF="inactive":$NF="active"' FS=';' OFS=';' file

Last field $NF is evaluated . If found zero then Last Field $NF is changed to "active" , otherwise is changed to "inactive".

Print is omitted since will be performed by default.

Alternative , even more idiomatic by using ternary operation in variable assignment :

$ awk '$NF=($NF?"inactive":"active")' FS=';' OFS=';' file
  • 1
    +1 Nice approach! You can make these together FS=OFS=';' – αғsнιη Sep 12 '17 at 2:51
  • 1
    This is reversed from the Q: $3 (implicitly !=0 and in fact ==1) should be "Inactive". @αғsнιη: that works in the script (especially in BEGIN{ }) but not as pseudo-file commandline arguments. – dave_thompson_085 Sep 12 '17 at 5:41
  • @αғsнιη Thanks! Chaining FS and OFS together is allowed only within a BEGIN block , not "externally" after the main code.... – George Vasiliou Sep 12 '17 at 8:49
  • @dave_thompson_085 Yeap, good point. Fixed now. – George Vasiliou Sep 12 '17 at 11:03
0

With perl:

perl -pe 's/\d+(?!.*\d)/$& ? "Inactive" : "Active"/e' < file

Replaces the first sequence of digits that is not followed by any digit with "Inactive" if it's 0 or "Active" otherwise.

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.