LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-24-2021, 06:22 AM   #16
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 525

Original Poster
Rep: Reputation: 95

Quote:
Originally Posted by Turbocapitalist View Post
Neat.



Sed scripts, like all scripting languages, can be annotated:

Code:
#!/usr/bin/sed -nf 

/3/!{                   # if line does not contain a 3 
        p;              # print
        d;              # clear and jump to the beginning
};

:loop;                  # land here

N;                      # append next line to pattern space

/7/{                    # if the line contains a 7
        s//&\nFOOBAR/p; # & means the found pattern, in this case the line
        d;              # clear and jump to the beginning
};

$!bloop;                # if not at the end, then jump to marker above
The -f is important.

I know but I prefer to have it as a one liner.
I use a long sed with -i and several -e to have it modified in one shot.
 
Old 12-24-2021, 07:09 AM   #17
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Well, while this is no INI file, the structure is sufficiently similar, so crudini may actually help in this case, too.
Code:
$ cat postgresql.conf
# This is a comment
log_connections = yes
log_destination = 'syslog'
search_path = '"$user", public'
shared_buffers = 128MB
$ crudini --set --output=- postgresql.conf '' log_connections no
# This is a comment
log_connections = no
log_destination = 'syslog'
search_path = '"$user", public'
shared_buffers = 128MB
$ crudini --set --output=- postgresql.conf '' datestyle "'iso, mdy'"
# This is a comment
log_connections = yes
log_destination = 'syslog'
search_path = '"$user", public'
shared_buffers = 128MB
datestyle = 'iso, mdy'
OTOH, Augeas is packaged for EL8 and provides a lens for posgresql.conf. I'd look into doing this with augtool. There are also Python and Ruby bindings in python3-augeas and ruby-augeas, respectively. And it's supported by Puppet, too.

To give you a perspective on Augeas in RHEL, here is the relevant discussion at Red Hat Customer Portal.

TBH, now I'm perplexed just as pan64 was above.
Quote:
Originally Posted by pan64 View Post
I don't understand it at all. That range specification is completely pointless here. (ok, the real code can be different)
Code:
sed "/$i/ i FFOOBBAARR"
With postgresql.conf being a flat configuration file without any nested structures, I don't see the point in matching a range inside it. Matching just a line would do the same.

Last edited by shruggy; 12-25-2021 at 05:28 AM.
 
Old 12-26-2021, 03:19 AM   #18
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,800

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
In standard (default print) mode you can use
b
rather than
{p;d;}

Because sed does not have variables you must use a loop.
Another attempt (yet untested, and using standard sed code):
Code:
sed '
/3/!b
:L
n
/7/!bL
i\
FOOBAR
'

Last edited by MadeInGermany; 12-26-2021 at 03:22 AM.
 
1 members found this post helpful.
  


Reply

Tags
insertion, last line, range, sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] insert line in file every 1000th line via sed or in vi tiny.deluxe Programming 6 01-11-2018 04:51 AM
Use SED to insert textfile before a line, using a COMBINED search wilsoncpu Linux - Newbie 2 09-27-2014 07:17 AM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 09:14 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration