Modify text file if found multiple pattern match for every line.


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Modify text file if found multiple pattern match for every line.
# 8  
Old 08-16-2018
OS: window xp, 7 and 10.
shell :MKS kornshell

sed version , i trying sed --version it not work.

i means the 2 request i'm asking if can split it to 2 difference code line.
# 9  
Old 08-16-2018
From mkssoftware.com:
Quote:
a,bb [label] branches to label. If you omit label, sed branches to the end of the script.
, so the script should work as given unless something weird happened when copying it. Try to add a space between b and ; .
Your request to split can't be satisfied that easy as the two are interwoven.
# 10  
Old 08-16-2018
Here is a multi-line sed script that does substitutions in the first ! IPG block only.
Code:
sed '
  /^! IPG: /{
    a\
\
testplan generation off
    :Loop
    n
    /^! IPG: /b
    /; permanent/bLoop
    /^ *$/bLoop
    s/; /&permanent, /
    tLoop
    s/$/; permanent/
    bLoop
  }
' file

The Unix sed has some shortcomings:
  1. The "label parser" treats a semicolon and trailing spaces as part of the label. A newline is a must. The man page only mentions the maximum length of 7 (or 8) characters.
  2. Before and after a } there must be a semicolon or newline. In some Unix sed versions a semicolon can lead to a wrong behavior; only the newlines are safe, i.e. place the } on a dedicated line. On some systems the man page says "only newlines allowed".
  3. An addtional -e is like a newline. So you can have -e '...;:label' -e '...' and -e '...' -e '}' -e '...'

Last edited by MadeInGermany; 08-16-2018 at 01:09 PM..
# 11  
Old 08-16-2018
Quote:
Originally Posted by MadeInGermany
Here is a multi-line sed script that does substitutions in the first ! IPG block only.
Code:
sed '
  /^! IPG: /{
    a\
\
testplan generation off
    :Loop
    n
    /^! IPG: /b
    /; permanent/bLoop
    /^ *$/bLoop
    s/; /&permanent, /
    tLoop
    s/$/; permanent/
    bLoop
  }
' file

The Unix sed has some shortcomings:
  1. The "label parser" treats a semicolon and trailing spaces as part of the label. A newline is a must. The man page only mentions the maximum length of 7 (or 8) characters.
  2. Before and after a } there must be a semicolon or newline. In some Unix sed versions a semicolon can lead to a wrong behavior; only the newlines are safe, i.e. place the } on a dedicated line. On some systems the man page says "only newlines allowed".
  3. An addtional -e is like a newline. So you can have -e '...;:label' -e '...' and -e '...' -e '}' -e '...'

Thank for reply, below is the output i get.
-testplan generation off is show at every line when it meet IPG, can it just show at 1st meet only?
permenent are not added , it only add after 2nd IPG meet, and it add to board 3 to 3 , can it just add if found "?
-and it not modify the file directly, i need to > to other file name, instate of using same file name.



Code:
!!!!   14    0    1 1533623476   0000                                         
! IPG: Tue Aug 07 14:31:17 2018

testplan generation off
! IPG: board type 00851-1712
boards 2 to 2
test shorts "shorts"
test capacitor "c488"; nulltest !tested in parallel with c480
test capacitor "c588"; nulltest !tested in parallel with c480
test analog powered "sp-p6"; comment
test digital "u423"
! IPG: board type 00851-1715

testplan generation off
boards 3 to 3; permanent
test shorts "shorts"; permanent
test capacitor "c590"; permanent
test capacitor "c591"; permanent, nulltest !tested in parallel with c590
test analog powered "bp-test"; permanent, comment

! IPG: board type 00860-0106
boards 1 to 1
test shorts "shorts"
test pins "pins"
test capacitor ".discharge"
test capacitor "c102"
test analog powered "u511%opamp_o_1"

# 12  
Old 08-17-2018
Did you try to apply the insight that MadeInGermany gave to the proposal in post#5?
# 13  
Old 08-17-2018
In my attempt to substitute only in the first ! IPG block I have not seen that the whole thing repeats with every 2nd ! IPG.
Because you seem to want substitutions in all blocks anyway, you can simply change the b (leave the loop) to bLoop (continue the loop):
Code:
    /^! IPG: /bLoop

# 14  
Old 08-23-2018
Hi Rubic,

i'm trying search web on it , but still unable to future to understand,

Quote:
Originally Posted by MadeInGermany
In my attempt to substitute only in the first ! IPG block I have not seen that the whole thing repeats with every 2nd ! IPG.
Because you seem to want substitutions in all blocks anyway, you can simply change the b (leave the loop) to bLoop (continue the loop):
Code:
    /^! IPG: /bLoop


testplan generation off
! IPG: board type 00851-1712
boards 2 to 2; permanent
test shorts "shorts"; permanent
test pins "pins"; permanent

this still come out.

Thank alot.

sorry that im 0 knowledge on, i trying to figure up what happen on it but unable to found it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

2. Shell Programming and Scripting

Multiple pattern match and print the output in a single line

I need to match two patterns in a log file and need to get the next line of the one of the pattern (out of two patterns) that is matched, finally need to print these three values in a single line. Sample Log: 2013/06/11 14:29:04 <0999> (725102) Processing batch 02_1231324 2013/06/11... (4 Replies)
Discussion started by: rpm120
4 Replies

3. Shell Programming and Scripting

Append text on particular line after pattern found

hi, i have /etc/inittab, I want to add another line after that when i find a pattern "l6:6:wait:/etc/rc.d/rc 6". original l6:6:wait:/etc/rc.d/rc 6 after-change l6:6:wait:/etc/rc.d/rc 6 /sbin/if-pp-to-cng (3 Replies)
Discussion started by: learnbash
3 Replies

4. Shell Programming and Scripting

Displaying text till pattern match found in a line

Hi All, From the below line if we want to display all the text till found pattern dot/. I was trying with the below code but couldn't able to print text before the pattern. it display texts which is found after pattern. awk '/assed/{print;getline;print}' file_name | sed 's/^*. *//' input... (4 Replies)
Discussion started by: Optimus81
4 Replies

5. Shell Programming and Scripting

Modify one line in a plain text file

Hi everyone, I want to know, if there is a way to modify one line in a text file with unix script, with out re-writing all the file. For example, i have this file: CONFIGURATION_1=XXXX CONFIGURATION_2=YYYY CONFIGURATION_3=ZZZZ supose i have a command or function "modify" that... (7 Replies)
Discussion started by: Xedrox
7 Replies

6. Shell Programming and Scripting

To add a new line with specific text after the pattern is found using sed

hi guys, im trying to add the following line in my xml file <dbrollbacksegs <oa_var="s_db_rollback_segs">NOROLLBACK</dbrollbacksegs> when ever i find the following line <dbsharedpool oa_var="s_dbsharedpool_size">300000000</dbsharedpool> I have succedded till adding a new line... (1 Reply)
Discussion started by: smarlaku
1 Replies

7. Shell Programming and Scripting

Modify a file by another file: add new line and variable after string is found

hello, I have problem with writing/adjusting a shell script. I searched forum and unfortunately couldn't write scipt based on the information I found. I never wtire such so it's hard for me and I do need to modify one script immediately. case looks like: 1. 'file' that needs to be modified... (3 Replies)
Discussion started by: bipbip
3 Replies

8. Shell Programming and Scripting

Need help to modify perl script: Text file with line and more than 1 space

Dear Friends, I am beginner in Perl and trying to find the problem in a script. Kindly help me to modify the script. My script is not giving the output for the last field and followed text (LA: Language English). Input file & script as follows: Input file: Thu Mar 19 2:34:14 EDT 2009 STC... (3 Replies)
Discussion started by: srsahu75
3 Replies

9. Shell Programming and Scripting

Modify Specific Line of a Text File

Given a text file, how do you add a line of text after a specific line number? I believe I would want to use "sed" but I am unsure of the syntax. Thank you. Mike (5 Replies)
Discussion started by: msb65
5 Replies

10. Shell Programming and Scripting

Concatenating multiple lines to one line if match pattern

Hi all, I've been working on a script which I have hit a road block now. I have written a script using sed to extract the below data and pumped into another file: Severity............: MAJORWARNING Summary: System temperature is out of normal range. Severity............: MAJORWARNING... (13 Replies)
Discussion started by: phixsius
13 Replies
Login or Register to Ask a Question