Bash script - How to update header of scripts in one pass - multiline search/replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script - How to update header of scripts in one pass - multiline search/replace
# 1  
Old 08-23-2019
Bash script - How to update header of scripts in one pass - multiline search/replace

Hello.
A find command return a list of file.
For each file
Replace the content starting with the first "§" (of two) ending with last "ɸ" (of two), regardless of the content ( five lines )
by the following content (exactly) :
Code:
§2019_08_23§                      #
#                                                #
#    ¨version:41-0-2¨                      #
#                                                #
#    ɸmodif_version:5-4-3ɸ

Header example :
Code:
#!/bin/bash
#
#######################
#                                                #
#    {config_std}                            #
#                                                #
#    /sys_script/000_COMMON/Bin/some_script
#                                                #
#    INSTALL VERSION                    #
#                                                #
#    §2018_03_23§                         #
#                                                #
#    ¨version:25-0-0¨                     #
#                                                #
#    ɸmodif_version:0-0-1ɸ             #
#                                                #
########################
#
# object
#
########################################################
#
#

I know nothing in perl, awk, and few things in sed.

Note : white-space are tab or space or both and are undefined.


Any help is welcome.
# 2  
Old 08-23-2019
Are those § and ɸ always in the same line? Is the first § at BOL, and ɸ at EOL? Try


Code:
sed -e'/^§/,/ɸ$/ {/ɸ/ rheader' -e'; d}' content

# 3  
Old 08-24-2019
Quote:
Originally Posted by RudiC
Are those § and ɸ always in the same line? Is the first § at BOL, and ɸ at EOL?

To be more clear, on any files return by the command find :
1° The start pattern to search § is generaly at line 11 but should be more or less
2° The start pattern to search § is always on a new line starting with the comment character # which is followed by one ore more space or tab
3°) The end pattern to search ɸ is always the second one on the same line. Some thing like
^#.*ɸ.*ɸ.*#.*$
4°)Between the first pattern § and the last pattern ɸ there is at least 3 line :
Code:
#    §2017_08_23§                      #
#    ¨version:11-0-2¨                  #
#    ɸmodif_version:2-2-1ɸ             #

But it could be more
Code:
#    §2019_01_13§                      #
#                                      #
#                                      #
#    ¨version:1-0-0¨                  #
#                                      #
#                                      #
#    ɸmodif_version:0-0-3ɸ             #

I am looking for something like ( the following syntax is surely bad ) :
Set start and stop address; replace every things between start and stop including start and stop by the multi-line block of text :

Code:
sed '/§.*§ /,/ɸ.*ɸ/{s/.*/§2019_08_24§                      #\
#                                      #\
#    ¨version:3-0-0¨                  #
#                                      #
#    ɸmodif_version:1-1-1ɸ/}' some_file.sh

Or if the substitution text is in a bash variables :
Code:
REPLACE_VAR="$(cat <<-EOF §2019_08_24§                      #\
#                                      #\
#    ¨version:3-0-0¨                  #
#                                      #
#    ɸmodif_version:1-1-1ɸ EOF )"

Then :
Code:
sed '/§.*§ /,/ɸ.*ɸ/{s/.*/'"$REPLACE_VAR"'/}' some_file.sh

Any help is welcome
# 4  
Old 08-24-2019
Did you try the proposal given (based on your non-representative sample in post #1) with your revised data, perhaps with revised search patterns?


1. The #! /bin/bash shebang is pointless on line 11.
2. Not mentioned in your sample.
4. The address range used in the proposal doesn't care for the numner of lines between start and end.
# 5  
Old 08-24-2019
Quote:
Originally Posted by RudiC
Did you try the proposal given (based on your non-representative sample in post #1) with your revised data, perhaps with revised search patterns?
No, because as far as the few I knew about sed :
/^§/ stand for "§" at beginning of line
/ɸ$/ stand for "ɸ" at end of ligne

that is not the case for my file heade



Quote:
Originally Posted by RudiC


1. The #! /bin/bash shebang is pointless on line 11.

The shebang is at line 1 not 11
It is the first line of the header file


Quote:
Originally Posted by RudiC

2. Not mentioned in your sample.

Sorry I don't understand

Quote:
Originally Posted by RudiC



4. The address range used in the proposal doesn't care for the numner of lines between start and end.
Of course. This is the question.

How to replace all the characters ( whichever they are ) from the first "§" (including it) up to the last (including it) with the defined block of text
Code:
§2019_08_24§                      #\
 #                                      #\
 #    ¨version:3-0-0¨                  #\
 #                                      #\
 #    ɸmodif_version:1-1-1ɸ


The number of line lines is useless.
There is a start point : the first "§"
There is an end point : the second "ɸ" ( or any "ɸ" followed by "[[:blank:]]#")


Doing that manually with a text editor :
1) copy (from somewhere)
the new text from the first "§" up to the last "ɸ".

2) Select
In the current file, select the text from the first "§" up to the last "ɸ". Don't care about the number of lines.

3) paste
over the selection paste the text previously copied.


I hope that make things clearer.


Thank you

--- Post updated at 01:02 ---

Code:
sed '/§.*§/,/ɸ.*ɸ/{d}' "$SOME_FILE"

is able to delete the selected text.


But

Code:
sed '/§.*§ /,/ɸ.*ɸ/{s/§.*§.*ɸ.*ɸ/§2019_08_24§                      # \
#                                      # \
#    ¨version:3-0-0¨                  # \
#                                      # \
#    ɸmodif_version:1-1-1ɸ/}' "$SOME_FILE"


Does not make any substitution.


Any help is welcome
# 6  
Old 08-25-2019
Contrary to what I wrote yesterday, it doesn't quite do what I thought.
In fact it delates from the beginning of the line containing /start/ (complete line) until the end of the line containing/stop/ (complete line) .
From my sample that do :
Code:
#!/bin/bash 

# ####################### 

#                                                # 

#    {config_std}                            # 

#                                                # 

#    /sys_script/000_COMMON/Bin/some_script 

#                                                # 

#    INSTALL VERSION                    # 

#                                                # 

#                                                # 

######################## 

# 

# object 

# 

######################################################## 

# 

#

but I was extecting


Code:
#!/bin/bash 

# ####################### 

#                                                # 

#    {config_std}                            # 

#                                                # 

#    /sys_script/000_COMMON/Bin/some_script 

#                                                #
#    INSTALL VERSION                    # 

#                                                # 

#     §2018_03_23§........ 
 ..................................
 ...................................
...................................
......ɸmodif_version:0-0-1ɸ              #
#                                                #

#                                                # 
######################## 

# 

# object 

# 

######################################################## 

# 

#

The expected deleted text is in red.
# 7  
Old 08-25-2019
The best I have found is :
Code:
sed 's/§.*§/§2019_08_25§/    ;    s/¨.*¨/¨version:34-1-9¨/    ;    s/ɸ.*ɸ/ɸmodif_version:9-7-3ɸ/' "$SOME_FILE"

But that does not convert :
Code:
#!/bin/bash 
# 
####################### 
#                                        # 
#    {config_std}                        # 
#                                        # 
#    /sys_script/000_COMMON/Bin/some_script 
#                                        # 
#    INSTALL VERSION                     # 
#                                        # 
#    §2018_03_23§                        # 
#                                        # 
#                                        # 
#    ¨version:25-0-0¨                    # 
#                                        # 
#                                        # 
#                                        # 
#                                        # 
#    ɸmodif_version:0-0-1ɸ               # 
#                                        # 
######################## 
# 
# object 
# 
######################################################## 
# 
#

to
Code:
#!/bin/bash 
# 
####################### 
#                                        #
#    {config_std}                        # 
#                                        #
#    /sys_script/000_COMMON/Bin/some_script 
#                                        #
#    INSTALL VERSION                     # 
#                                        #
#    §2019_08_25§                        # 
#                                        #
#    ¨version:34-1-9¨                    # 
#                                        #
#    ɸmodif_version:9-7-3ɸ               # 
#                                        #
######################## 
# 
# object 
# 
######################################################## 
# 
#

Which should be nice for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. Shell Programming and Scripting

Pass arguments to bash script

myscript.sh #!/bin/bash ARGA=$1 if ; then echo "${ARGA}:Confirmed" else echo "${ARGA}:Unconfirmed" fi when I run the above script from the command line, i run it as: ./myscript.sh jsmith now some times, i need to runn it this way: (8 Replies)
Discussion started by: SkySmart
8 Replies

3. UNIX and Linux Applications

Script to delete few rows from a file and then update header

HJKL1Name00014300010800000418828124201 L201207022012070228XAM 00000000031795404 001372339540000000000000000000000 COOLTV KEYA Zx00 xI-50352202553 00000000 00000000 G000000000000 00000000 ... (10 Replies)
Discussion started by: mirwasim
10 Replies

4. Shell Programming and Scripting

Can BASH handle mathematical operations and do a Search & Replace?

Hello, I have a bunch of xml file that needs to have edits made and I was wondering if a BASH script could handle it. I would like the script to look within my xml files and replace all integers greater than 5px with a value that is 25% smaller. For example, 100px = 75px. Since the integers... (12 Replies)
Discussion started by: jl487
12 Replies

5. Shell Programming and Scripting

Search Replace and Update a file

hi, I am stuck at a place. Please help me out. Here is what i need to do. Search for a pattern in a propertyfile and change only at one occurance. I have these statements and assignment as a part of the propertyfile #Note : The Address should be replaced with actual address dynamically. ... (7 Replies)
Discussion started by: raghu_shekar
7 Replies

6. Shell Programming and Scripting

Bash sed search and replace question

I have several files that I need to modify using sed. I know how to do that, but now a new requirement has come up. Now, I need to make changes to all lines that don't start with certain strings. For example, I need to change all lines except for lines that start with "a", "hello there",... (3 Replies)
Discussion started by: RickS
3 Replies

7. Shell Programming and Scripting

bash search and replace

Hello all, I would like to replace some text that are forwarded in standard output from a script, then save the replaced text to a file. The text i would like to replace is in the form of: 1 some text 1.1 other text 1.2 more text 1.2.1 still more text i would like to replace 1 some... (5 Replies)
Discussion started by: dolphin06
5 Replies

8. Shell Programming and Scripting

awk script to update header record

I am using HP UX and think this may be done with awk but bot sure. I have a file with a several header records and undeneath many detail records I need to put in the header record the number of detail records above this header record and number of detail records below this header record Header... (5 Replies)
Discussion started by: klut
5 Replies

9. Shell Programming and Scripting

How to pass passwords to bash scripts?

I'm finding the following command very tedious to type in all the time, so I created a one line bash script called mount.bash with the following contents: mount -t cifs //mark/C\$ -o unc=//mark\\C$,ip=10.1.1.33,user=Administrator,password=$1 /mnt/mark I don't like the fact that I have to put... (5 Replies)
Discussion started by: siegfried
5 Replies

10. Shell Programming and Scripting

search and replace by bash

Hello everyone! I have a file: text.txt and also a list of replacement: replace.txt (tabbed) is isn\'t Mac Windows will won\'t \.\n \! <--- ! plus 2 spaces I want a script which will change text.txt as I made a script: #!/bin/bash echo Please input the file of... (2 Replies)
Discussion started by: Euler04
2 Replies
Login or Register to Ask a Question