-1

I have a file with a repeating block of text (>100 of these blocks!). Each repeating block has the same number of lines and I am interested in replacing NodeNames, N1 N2 N3 N4 N5 with NodeNames, N1 N2 N3 N4 in all the AB_* blocks only. (* here refers to the specific id of a particular AB block).

Here is the format of the file:

DeviceName,  AB_12445
line 2
<empty line)
line 3
line 4
line 5
empty line
line 6
line 7
empty line
NodeNames, N1 N2 N3 N4 N5
line 9


DeviceName,  AB_0483
..
..
..
NodeNames, N1 N2 N3 N4 N5
line 9

The desired output for a particular AB block (e.g., AB_0483) is:

DeviceName,  AB_0483
..
..
..
NodeNames, N1 N2 N3 N4
line 9

I would like to save the entire output containing both the updated blocks as well as the unaffected blocks into a new file.

  • 1
    wow. i just got a negative vote within 20min of posting the question which reduced the number of views and my chances of getting help. Is this how this forum welcomes new users who genuinely ask for help on an important question that impacts their productivity. I did search the forum prior to posting to see if there were answers but did not find anything that helps. – Lebann Dec 27 '16 at 20:17
  • You might consider taking programming questions like this one to Stack Overflow instead. And you might also consider showing some code you wrote attempting to solve the problem yourself. – Michael Vehrs Dec 28 '16 at 10:19
1

based on the comments, I updated the command.

awk '/AB_/{a=1}!a{print;next}a{if($0!~/NodeNames/){print}else{a=0;printf("NodeNames, N1 N2 N3 N4\n")}}' input > output
  • No. I would like to save the entire output containing both the updated AB blocks (>100 blocks or so) as well as the unaffected blocks (the non AB blcoks) into a new file. The new file essentially will be a replica of the original file, except that the NodeNames will be modified in all the the relevant (i.e., AB) blocks. – Lebann Dec 28 '16 at 1:15
  • so, all your AB block NodeNames needs to be changed and saved in different file ? and the unaffected blocks to another file? – Kamaraj Dec 28 '16 at 1:20
  • modified the script. please check now. it modify all the AB block NodeNames to NodeNames, N1 N2 N3 N4 – Kamaraj Dec 28 '16 at 1:35
0

Welcome to StackExchange. This covers it, I believe. Essentially setup a flag variable 'ab' to indicate whether we're in an affected block. And then just replace the offending NodeNames lines as necessary, if in an 'ab' block.

Many other ways to achieve what you're after : the example answer I've given is rather wordy+lengthy. Expect someone else will offer a clever+cryptic one-liner shortly.

$ cat foo.awk
{
 if($1=="DeviceName," && $2 ~ /^AB/) { ab=1 }
 if($1=="DeviceName," && $2 !~ /^AB/) { ab=0 }
 if($0=="NodeNames, N1 N2 N3 N4 N5" && ab==1) {
  $0="NodeNames, N1 N2 N3 N4"
 }
 print $0
}

Example usage:

awk -f foo.awk myfile
  • Thanks, Steve! I would like to print the output of the updated file containing the modification into a new file. Your script prints $0 to the scrren. How do I print the content of print $0 to a new file? I tried using awk -f foo.awk myfile > newfile but that seems to no do the job. – Lebann Dec 28 '16 at 0:56
  • awk -f foo.awk myfile > newfile will do the job, please retry and post details of any failure you see. – steve Dec 28 '16 at 8:26
  • 1
    Many thanks Steve. Indeed ">" redirects the output. I really liked your script as it is easier to read. However, it appears that the code you provided does not cover all the the affected blocks for some reason when I run it on my actual file. I have tried to also use it for a smaller subset and it appears to work for the simpler file. I am not sure if it has to do with the fact that the affected blocks appear throughout the file and not as one contiguous section of the code.Many thanks again for your time! – Lebann Dec 28 '16 at 22:18
0

Here is a sed solution:

 sed '/DeviceName, *AB_12445/, /DeviceName, *AB_/ { /NodeNames/s/ N5// }'

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.