0

This is related to another question that I recently asked.

I have a text file which has multiple sections. Part of the file looks like this.

3. line 3
4. line 4

## Screenshots ##

1. line 1
2. line 2
3. line 3
4. line 4

## Changelog ##

3. line 3
4. line 4

I want to transform this file, such that all the lines of the file are present in the transformed file and the lines in the screenshot section are passed through another command, before being added to this file.

So the transformed file might look like

3. line 3
4. line 4

## Screenshots ##

1. modified 1
2. modified 2
3. modified 3
4. modified 4

## Changelog ##

3. line 3
4. line 4

I have the following command which prints only the lines in the screenshot section. (From here)

awk '/^## Screenshot/{p=1;print;next} p&&/^##/{p=0};p' readme.md 

I tested my command by piping to the output of the above command and it works.

But now I want to print the other lines of the file untouched as well. How to do that?

Community
  • 1
  • 1
Sudar
  • 18,954
  • 30
  • 85
  • 131

1 Answers1

1

If you can easily modify the text as you want in awk (eg, convert "line" to "modified"), just do it directly:

awk '/^## Screenshot/{p=1} p{gsub( "line", "modified")} p&&/^##/{p=0}; 1' readme.md

If it is not feasible to do the replacement in awk for some reason, you can simply have awk break up the output and then join:

awk '/^## Screenshot/{f="file2"} 
    f=="file2" && /^##/{f="file3"} 
    {print > f}' f=file1 readme.md
... # Now perform the desired filter on file2
cat file1 file2 file3 > final_output  # Concatenate results
William Pursell
  • 204,365
  • 48
  • 270
  • 300