1

I have a text file with 4 columns. I need to modify the file so that the first sequence of 1's in the 4th column remain 1, but all other values in the 4th column be changed to 0.

I have tried the following awk command with multiple if statements but the variable fat doesn't seem to be updating properly.

`cat sample_data.txt`

72  29  16  0   <br>
73  30  16  0   <br>
74  31  16  0   <br>
75  32  16  1   <br>
76  33  16  1   <br>
77  34  16  1   <br>
78  35  16  0   <br>
79  36  16  0   <br>
80  37  16  0   <br>
81  38  16  0   <br>
82  39  16  0   <br>
83  40  16  0   <br>
84  41  16  0   <br>
85  42  16  0.55    <br>
86  43  16  0.57    <br>
87  44  16  0.41    <br>
88  45  16  0.58    <br>
89  46  16  1   <br>
90  47  16  1   <br>
91  48  16  1   <br>
92  49  16  1   <br>
93  50  16  0.59    <br>
94  51  16  0.52    <br>
95  52  16  0.43    <br>


`awk -v fat=1 '{if($4<1 && fat=1) {print $1,$2,$3,0;} else if($4=1 && fat=1) {fat=2;print $1,$2,$3,1;} else if($4=1 && fat=2) {fat=2;print $1,$2,$3,1;} else if($4<1 && fat=2) {fat=3;print $1,$2,$3,0} else if($4<1 && fat=3) {fat=3;print $1,$2,$3,0;} else if($4=1 && fat=3) {fat=3;print $1,$2,$3,0;}}' sample_data.txt`

I want this output:

72  29  16  0   <br>
73  30  16  0   <br>
74  31  16  0   <br>
75  32  16  1   <br>
76  33  16  1   <br>
77  34  16  1   <br>
78  35  16  0   <br>
79  36  16  0   <br>
80  37  16  0   <br>
81  38  16  0   <br>
82  39  16  0   <br>
83  40  16  0   <br>
84  41  16  0   <br>
85  42  16  0   <br>
86  43  16  0   <br>
87  44  16  0   <br>
88  45  16  0   <br>
89  46  16  0   <br>
90  47  16  0   <br>
91  48  16  0   <br>
92  49  16  0   <br>
93  50  16  0   <br>
94  51  16  0   <br>
95  52  16  0   <br>
1
  • edit your question to format your code and sample input/output properly by simply indenting them by 4 spaces. The editors {} button will do it for you. Commented Apr 18, 2019 at 4:24

2 Answers 2

2

wrt your code:

if($4<1 && fat=1)

fat=1 is an assignment, an equivalency test would be fat==1 (double equals) instead.

But anyway, here's how to do what you appear to want with a simple FSM:

$ awk '(state==0) && ($4==1){state=1} (state==1) && ($4!=1){state=2} state==2{$4=0} 1' file
72 29 16 0
73 30 16 0
74 31 16 0
75 32 16 1
76 33 16 1
77 34 16 1
78 35 16 0
79 36 16 0
80 37 16 0
81 38 16 0
82 39 16 0
83 40 16 0
84 41 16 0
85 42 16 0
86 43 16 0
87 44 16 0
88 45 16 0
89 46 16 0
90 47 16 0
91 48 16 0
92 49 16 0
93 50 16 0
94 51 16 0
95 52 16 0
Sign up to request clarification or add additional context in comments.

2 Comments

This seems to work fine! I think I understand most of it, but could you explain the the 1 at the end?
It's a true condition and so it causes awk to perform it's default action which is to print the current record. It's just idiomatic shorthand for {print}.
0

tried on gnu awk:

awk -vf=1 '$4==1&&(f||s){f=0;s=1;$4=0;print;next} {s=0}1' sample_data.txt

1 Comment

While this command may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.