LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2019, 10:34 AM   #1
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Rep: Reputation: Disabled
Question How to replace a code block with multiple lines in a file using bash script?


Hello

How can i replace a code block with multiple lines in a file using bash script?

example:

in file test.php replace this:

Code:
        <li>
            <a href="{{ route('playlists.index') }}">
                <i class="{{ config('other.font-awesome') }} fa-list-ol" style=" font-size: 18px; color: #ffffff;"></i>
                <span class="menu-text">Playlists</span>
                <span class="selected"></span>
            </a>
        </li>
with:

Code:
        <li>
            <a href="{{ blabla('dokam.something') }}">
                <i class="{{ power('test.font-awesome') }} fa-list-ol" style=" font-size: 20px; color: #ffffff;"></i>
                <span class="test2-text">Playlists</span>
                <span class="sel"></span>
            </a>
        </li>
Checking on the net i found a few info but not many for multi line and didn't manage to get that working ...

Thank you
 
Old 11-22-2019, 03:39 AM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,779

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
replace.sed:
Code:
|<a href="{{ route('playlists.index') }}">\| {
  :L
  N
  |</a>\|!bL
  s|.*||
  i\
            <a href="{{ blabla('dokam.something') }}">\
                <i class="{{ power('test.font-awesome') }} fa-list-ol" style=" font-size: 20px; color: #ffffff;"></i>\
                <span class="test2-text">Playlists</span>\
                <span class="sel"></span>\
            </a>
}
Code:
sed -f replace.sed -i.bak test.php

Last edited by MadeInGermany; 11-22-2019 at 03:42 AM.
 
Old 11-22-2019, 03:42 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Something like the following sed might get you started:
Code:
sed -n '/<li>/,/<\/li>/p' file
Obviously there will be multiple 'li' tags in your code so you will need to find what is unique about this particular one prior to the change
 
Old 11-22-2019, 04:24 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,118

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
This could turn seriously ugly - maybe straight away, maybe some time down the track. So many potential corner cases ...
 
Old 11-22-2019, 04:54 AM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
this works with the sample

Code:
sed -E '{
  s/route(..)playlists.index/blabla\1dokam.something/
  s/config(..)other(.+ font-size:) 18px/power\1test\2 20px/
  s/menu-text/test2-text/
  s/selected/sel/
}' 


#
obviously that is hardcoded, and largely worthless

if you provide detail a better solution may be available
I doubt that will happen
you appear to have not learned anything from your recent threads

It is getting quite tiresome to be honest.
 
1 members found this post helpful.
Old 11-22-2019, 06:40 AM   #6
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
Thank you !
 
Old 11-22-2019, 11:27 AM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
@Firerat - Once you have chosen to participate in a thread, please make your points without expressions of bitterness or being scornful of other members. Avoid the fatigue by simply avoiding threads which cause you to feel that way. If you choose to participate, do so respectfully.

From the LQ Rules:
Quote:
Challenge others' points of view and opinions, but do so respectfully and thoughtfully ... without insult and personal attack.
If you think the behavior of others is unacceptable, use the Report button to bring it to attention of the forum moderators.
 
Old 11-22-2019, 11:41 AM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by astrogeek View Post
@Firerat - Once you have chosen to participate in a thread, please make your points without expressions of bitterness or being scornful of other members. Avoid the fatigue by simply avoiding threads which cause you to feel that way. If you choose to participate, do so respectfully.

From the LQ Rules:


If you think the behavior of others is unacceptable, use the Report button to bring it to attention of the forum moderators.
I have reported your post, as I think your behaviour towards my post was unacceptable.
 
Old 11-22-2019, 11:58 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,879
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Firerat View Post
I have reported your post, as I think your behaviour towards my post was unacceptable.
The quote below is your post where you made comments to the OP that their threads are getting tiresome and that you question whether or not they've learned anything. That is not discussing technical topics respectfully, and instead is conducting a personal attack.

The correct choice there would be to act polite. If you feel an OP does not follow advice and persists with this pattern you may certainly report their continued behavior for evaluation, you may choose to not provide any help for their thread questions, or you may choose to inquire more information from them, and if they fail to follow up adequately, then there's not much you, or anyone could do to provide assistance for them.
Quote:
Originally Posted by Firerat View Post
this works with the sample
Code:
...
obviously that is hardcoded, and largely worthless

if you provide detail a better solution may be available
I doubt that will happen
you appear to have not learned anything from your recent threads

It is getting quite tiresome to be honest.
 
Old 11-23-2019, 05:00 AM   #10
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by rtmistler View Post
The quote below is your post where you made comments to the OP that their threads are getting tiresome and that you question whether or not they've learned anything. That is not discussing technical topics respectfully, and instead is conducting a personal attack.
I reported this post
Quote:
Originally Posted by astrogeek View Post
@Firerat - Once you have chosen to participate in a thread, please make your points without expressions of bitterness or being scornful of other members. Avoid the fatigue by simply avoiding threads which cause you to feel that way. If you choose to participate, do so respectfully.

From the LQ Rules:

If you think the behavior of others is unacceptable, use the Report button to bring it to attention of the forum moderators.
You have failed to deal with the Reported Post

@Astrogreek is projecting their own feelings upon my post.
using an extremely loose interpretation of "the rules"
also miss representing the "rule" by not quoting in full
Quote:
Challenge others' points of view and opinions, but do so respectfully and thoughtfully ... without insult and personal attack. Differing opinions is one of the things that make this site great.
I doubt that will happen # my opinion
you appear to have not learned anything from your recent threads # my opinion
It is getting quite tiresome to be honest. # my opinion


Quote:
Originally Posted by rtmistler View Post

The correct choice there would be to act polite.
Please explain in what way it was not polite.
Quote:
Originally Posted by rtmistler View Post
If you feel an OP does not follow advice and persists with this pattern you may certainly report their continued behavior for evaluation,
That would be petty
Although I do note that both yourself and Astrogeek do seem to embrace pettiness whilst failing to moderate with moderation.

I reported @astrogeek's post as I'm getting fed up with their pattern of bulling
You have failed to address that.
And not for the first time you simply join in on the bullying.

Quote:
Originally Posted by rtmistler View Post
you may choose to not provide any help for their thread questions, or you may choose to inquire more information from them, and if they fail to follow up adequately, then there's not much you, or anyone could do to provide assistance for them.
I did, and as predicted they just clicked on the "Did you find this post helpful?" link
and posted "Thank You!"

Go on, check the database
It was long before @Astrogeek resumed their bulling campaign in this thread.

It would appear that the only one "offended" was the one who projected their own bitterness over my expression of frustration.

they should have followed this rule
Quote:
Do not post if you do not have anything constructive to say in the post.
since Both you and Astrogeek have taken this thread off topic
I will get it back on track


sample.txt
Code:
   <li>
        <a href="{{ route('playlists1.index') }}">
            <i class="{{ config('other.font-awesome') }} fa-list-ol" style=" font-size: 18px; color: #ffffff;"></i>
            <span class="menu-text">Playlists1</span>
            <span class="selected"></span>
        </a>
    </li>
    <li>
        <a href="{{ route('playlists2.index') }}">
            <i class="{{ config('other.font-awesome') }} fa-list-ol" style=" font-size: 18px; color: #ffffff;"></i>
            <span class="menu-text">Playlists2</span>
            <span class="selected"></span>
        </a>
    </li>
    <li>
        <a href="{{ route('playlists.index') }}">
            <i class="{{ config('other.font-awesome') }} fa-list-ol" style=" font-size: 18px; color: #ffffff;"></i>
            <span class="menu-text">Playlists2</span>
            <span class="selected"></span>
        </a>
    </li>

This is much more targeted

Code:
#!/bin/bash

# Arrays, [0]="Original" [1]="new"

   Route=( "playlists2.index" "1dokam.index" )
  Config=( "other.font-awesome" "power.font-awesome" )
FontSize=( "18" "20" )
MenuText=( "Playlists2" "1dokam" )

# real script would use -i.BackupSuffix  
sed -E '/^[[:blank:]]+<a href=.+route.+'${Route[0]}'/,/^[[:blank:]]+<[/]a>/ {
  s/^([[:blank:]]+<a href.+route.+)'${Route[0]}'/\1'${Route[1]}'/
  s/^([[:blank:]]+<i class.+config.+)'${Config[0]}'/\1'${Config[1]}'/
  s/^([[:blank:]]+<i class.+config.+ font-size: )'${FontSize[0]}'(px)/\1'${FontSize[1]}'\2/
  s/^([[:blank:]]+<span.+menu-text.+)'${MenuText[0]}'/\1'${MenuText[1]}'/
}' ./sample.txt


#
I have used Arrays for the original/new
but
orignialRoute=
newRoute=
could be used

FontSize[0]="[0-9]{2}"
FontSize[1]="20"

means any font-size is replaced by 20

Last edited by Firerat; 11-23-2019 at 05:03 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
loop replace specific lines from a file with lines from another file . dr.x Programming 7 09-02-2019 07:18 AM
Bash - Replace multiple lines in file based on user input from php? nicedreams Programming 9 04-22-2015 09:11 AM
[SOLVED] Perl: how to replace blank lines in a file with given lines from another karamaz0v Programming 8 04-19-2012 06:48 AM
replace several lines in a file with other lines in another file if condition yara Linux - General 12 10-27-2009 03:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:12 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