[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Rewriting file from bash
From: |
Davide Brini |
Subject: |
Re: Rewriting file from bash |
Date: |
Sun, 3 Oct 2021 17:27:36 +0200 |
On Sun, 3 Oct 2021 17:16:04 +0200, Julius Hamilton
<juliushamilton100@gmail.com> wrote:
> Hey,
>
> How could I print the nth region of text of a file that is between blank
> newline regions, at the command line? So for example:
>
> file.txt:
>
> line1
>
> line2
> line3
>
> line4
>
>
>
> $ region[2]
>
> would return
>
> line2
> line3
>
> I presume this would be with grep, sed and/or awk.
>
> How could I then write a replacement for that region, from the command
> line (i.e. not using Vim or something)? Something like:
>
> $ region [2] “this is the replacement line”
Not really a bash question, but here's a way with awk:
awk -v RS= -v repl="replacement\nlines" -v n=2 '
NR == n { $0 = repl }
{ print sep $0; sep = "\n"}
' file.txt
where the meaning of the "repl" and "n" varuables should be apparent.
--
D.