简体   繁体   中英

Uncomment a block of text with awk or sed

Assuming I have a text file like this example:

Lorem ipsum dolor sit amet, nullam euismod tractatos id mel, has integre ornatus 
feugait ad. In eum enim putent fierent. Quo melius persecuti conceptam eu, ne probo 
autem inciderint quo, ius et atqui diceret. Causae prompta corpora ex ius. Atqui 
aperiri in duo, ex pro reque utinam.

Sea ne prima falli petentium. Ut has ancillae omnesque lucilius, vim ex alia 
audire contentiones. Error possit singulis at his. Ne purto soleat ius, detracto 
sententiae mel ne. Te eos regione detracto, eam quas accumsan detracto an.

  # Lorem ipsum dolor sit amet, nullam euismod tractatos id mel, has integre ornatus 
  # feugait ad. In eum enim putent fierent. Quo melius persecuti conceptam eu, ne probo 
  # autem inciderint quo, ius et atqui diceret. Causae prompta corpora ex ius. Atqui 
  # aperiri in duo, ex pro reque utinam.

  # Comment
  # At debet expetenda sed, sed te case ceteros adolescens. Ad sea facer minim tempor, 
  # eam facilisi definitiones ei, vix vidit erant dissentias et. Eum fierent scaevola 
  # suscipiantur eu. Eum essent platonem interesset ex, ut idque vidisse nam, labores 
  # intellegam comprehensam eos et. Eu eum appetere sententiae percipitur, ad eam hinc 
  # impetus sententiae, pro duis consetetur reprehendunt in. Id percipit iracundia 
  # abhorreant est.

  # Sea ne prima falli petentium. Ut has ancillae omnesque lucilius, vim ex alia 
  # audire contentiones. Error possit singulis at his. Ne purto soleat ius, detracto 
  # sententiae mel ne. Te eos regione detracto, eam quas accumsan detracto an.

Lorem ipsum dolor sit amet, nullam euismod tractatos id mel, has integre ornatus 
feugait ad. In eum enim putent fierent. Quo melius persecuti conceptam eu, ne probo 
autem inciderint quo, ius et atqui diceret. Causae prompta corpora ex ius. Atqui 
aperiri in duo, ex pro reque utinam.

Sea ne prima falli petentium. Ut has ancillae omnesque lucilius, vim ex alia 
audire contentiones. Error possit singulis at his. Ne purto soleat ius, detracto 
sententiae mel ne. Te eos regione detracto, eam quas accumsan detracto an.

I want to use either AWK or SED to uncomment the next 3 lines after the line # Comment so it ends up looking like this:

  # Comment
  At debet expetenda sed, sed te case ceteros adolescens. Ad sea facer minim tempor, 
  eam facilisi definitiones ei, vix vidit erant dissentias et. Eum fierent scaevola 
  suscipiantur eu. Eum essent platonem interesset ex, ut idque vidisse nam, labores 
  # intellegam comprehensam eos et. Eu eum appetere sententiae percipitur, ad eam hinc 
  # impetus sententiae, pro duis consetetur reprehendunt in. Id percipit iracundia 
  # abhorreant est.

Please explain your solution so I can learn and understand. My Knowledge of AWK and SED is still at the beginner level.

awk counts lines in its NR variable.

awk '/# Comment/ {n=NR}
     n && NR-n && NR-n<=3 {sub("# ?","")}
     {print}'

n=NR captures the number of the line where # Comment occurs

Zero is equivalent to false, and non-zero to true in awk, so in the conditional: 1) n prevents uncommenting from starting at the beginning of file, 2) NR-n prevents uncommenting from starting on the # Comment line, and 3) NR-n<=3 defines the lines where uncommenting does occur.

Function sub is awk's once-only string substitution. The string to be substituted with "", ie removed, is the comment symbol # followed by zero or one spaces -- ? is the regular expression quantifier for "optional" (zero or one).

Here's my attempt using GNU sed (this probably won't work on other versions of sed):

sed '/^  # Comment$/,+3 { s/^  # /  /; s/^  Comment$/  # Comment/ }'

We match all lines starting with a line equal to " # Comment" and the next 3 lines after (the +3 part is a GNU extension, my manual says).

In those lines, we replace a leading " # " by just two spaces, " " , thus uncommenting the line.

However, this also affects the starting line, which you didn't want to uncomment. So we fix it up after: If the resulting line is " Comment" , we add the "# " back in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM