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
4
  • 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
    Commented Sep 12, 2017 at 11:36
  • Have you checked for possible carriage return \r line endings ?? Commented Sep 12, 2017 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
    Commented Sep 12, 2017 at 13:18
  • 1
    You need to accept Ghoti solution as an answer to your question with the green tick. Commented Sep 16, 2017 at 16:25

3 Answers 3

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
  • 1
    Would be better as s/;0$/;active/;s/;1$/;inactive/' to avoid replacing foo;10 with foo;1active Commented Sep 12, 2017 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
4
  • 1
    +1 Nice approach! You can make these together FS=OFS=';' Commented Sep 12, 2017 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. Commented Sep 12, 2017 at 5:41
  • @αғsнιη Thanks! Chaining FS and OFS together is allowed only within a BEGIN block , not "externally" after the main code.... Commented Sep 12, 2017 at 8:49
  • @dave_thompson_085 Yeap, good point. Fixed now. Commented Sep 12, 2017 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .