3

I have a file that should have "OK" at the end of a certain line

the line that should be modified is the first line not ending with "OK"

I can grep the file to get the line to be modified using the following code

grep -vnm 1 OK '/content2'

i need a help in add the pattern OK to the end of this line

Example:

if the file content has the following lines

aaaaaaaaaaaaaa OK
bbbbbbbbbbbbbb OK  
cccccccccccccc  
dddddddddddddd
eeeeeeeeeeeeee OK

i need to add OK to the line contains the cccccccccccc only

  • 1
    sample of input & output needed. – αғsнιη Sep 3 '14 at 19:23
  • @KasiyA added example to the question – Fat Mind Sep 3 '14 at 19:29
  • 2
    You say that 'the line that should be modified is the next line to the last one ends with "OK"'. In this case the last one that ends with OK is the last line - eeeeeeeeeeeeee OK. So why would you add to cccccccccccccc? – muru Sep 3 '14 at 19:50
  • 2
    @KasiyA No need to be so abrasive. The question might have been formulated somewhat more clearly (notably to remove the ambiguity of what to do in the edge case of there being no line ending in OK), but it explicitly excludes your interpretation of adding OK to the first non-OK line as well as to every subsequent line, as it explicitly states "a certain line [...] only". – zwets Sep 3 '14 at 22:06
4

You could use the following awk program:

awk -v s=" OK" '/OK$/ { print; } !/OK$/ { print $0 s; s=""; }'

It works like this:

/OK$/ { print; }

prints out any line ending in OK as is, whereas

!/OK$/ { print $0 s; s=""; }

prints out any line not ending in OK with the value of variable s appended.

Variable s initially is the string ' OK'. It changes to the empty string after the first encounter of a line not ending in OK.

Update

Even more concisely:

awk -v s=" OK" '!/OK$/ { print $0 s; s=""; } /OK$/'

eliminates the { print; } action for the /OK$/ condition, as { print; } is the default action.

Correction

Looking at the question again, it does not say the OK should be added to the first line not having OK, but instead to the first line not having an OK which follows a line that does have an OK. So, we must keep a variable, c that flags that at least one OK line has been seen, and a variable d that flags that we are done:

awk ' /OK$/ { if (!d) c=1; } 
     !/OK$/ { if (c) d=1; } 
     c && d { s=" OK"; c=0; }
     { print $0 s; s=""; }'
| improve this answer | |
  • Would the downvoter please comment on what the issue is with this answer? – zwets Sep 3 '14 at 21:22
  • great! and if there were more sections, /OK$/ { print; s="OK";} do the job, isn't it? – Lety Sep 3 '14 at 22:03
  • 1
    I think the downvote might have been because on the sample text, it doesn't work correctly because the edit added several spaces to the end of two lines (probably unintentionally). If you remove those spaces it works nicely, although it does add an extra space to the end of the ddddd line, not sure if that is an issue. – Jason Conti Sep 3 '14 at 22:31
  • Ah, that is a consequence of the comma in the print command. It inserts a space. This can be fixed by using print $0s; instead of print $0,s;, and setting s=" OK" at the start. Will fix. – zwets Sep 3 '14 at 22:40
  • @Letizia Yes, that's pretty. Made me realise one ambiguity though: the question specifies that OK should be added to the first line not having OK after a line having OK. The solution as it is will add OK to the first line not having OK, regardless of whether an OK line occurred before. Adding fix. – zwets Sep 3 '14 at 22:48
-1
sed -ri 's/.*cccccccccccccc */cccccccccccccc OK/g' filename

where filename is the name of your input file name.

| improve this answer | |
  • 1
    ccccc is only for example it can be any thing ,the condition for the line that should be changed (it's the first line that dos not contain OK at the end of it) – Fat Mind Sep 3 '14 at 19:43
  • Only the first line or all lines that don't have OK ? – Harris Sep 3 '14 at 20:13
  • only the first line – Fat Mind Sep 5 '14 at 8:46

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.