Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04
# 8  
Old 09-13-2012
In that case, try:
Code:
awk '$0~"^[ \t]*"patt"[ \t]*$"{which++;if(which==n)$0=$0 RS repl}1' patt="$pattern" repl="$new_pattern" n=$n data.db

No need to provide for spaces/tabs before/after the pattern.
This User Gave Thanks to elixir_sinari For This Post:
# 9  
Old 09-13-2012
data.db
Code:
on post-fs-data
    # We chown/chmod /data again so because mount is run as root + defaults
    chown system system /data
    chmod 0771 /data

    # Create dump dir and collect dumps.
    # Do this before we mount cache so eventually we can use cache for
    # storing dumps on platforms which do not have a dedicated dump partition.
    # Fake entry for testing
    chmod 0771 /data

replace.sh
Code:
#!/bin/bash
n="1"
pattern="    chmod 0771 /data"
new_pattern="    mkdir /data/system"
awk '$0==patt{which++;if(which==n)$0=$0 RS repl}1' patt="$pattern" repl="$new_pattern" n=$n datadb/data.db >datadb/test.txt

expected output:
Code:
on post-fs-data
    # We chown/chmod /data again so because mount is run as root + defaults
    chown system system /data
    chmod 0771 /data
    mkdir /data/system

    # Create dump dir and collect dumps.
    # Do this before we mount cache so eventually we can use cache for
    # storing dumps on platforms which do not have a dedicated dump partition.
    # Fake entry for testing
    chmod 0771 /data

Works in this way, adding spaces in $pattern. However, these can be inconsistent in data.db...

---------- Post updated at 12:52 PM ---------- Previous update was at 12:46 PM ----------

Quote:
Originally Posted by elixir_sinari
In that case, try:
Code:
awk '$0~"^[ \t]*"patt"[ \t]*$"{which++;if(which==n)$0=$0 RS repl}1' patt="$pattern" repl="$new_pattern" n=$n data.db

No need to provide for spaces/tabs before/after the pattern.
Thank you a lot, you saved my day, and week Smilie
This works perfect

Now, I have another question, what if I want to do the same, but this time replace the pattern whole line by new_pattern and not only insert in a new line after it?

output would be, for n=1

Code:
on post-fs-data
    # We chown/chmod /data again so because mount is run as root + defaults
    chown system system /data
    mkdir /data/system

    # Create dump dir and collect dumps.
    # Do this before we mount cache so eventually we can use cache for
    # storing dumps on platforms which do not have a dedicated dump partition.
    # Fake entry for testing
    chmod 0771 /data

# 10  
Old 09-13-2012
That would need a very small change:
Code:
awk '$0~"^[ \t]*"patt"[ \t]*$"{which++;if(which==n)$0=$0 RS repl}1' patt="$pattern" repl="$new_pattern" n=$n data.db

to
Code:
awk '$0~"^[ \t]*"patt"[ \t]*$"{which++;if(which==n)$0=repl}1' patt="$pattern" repl="$new_pattern" n=$n data.db

Also, I would suggest a small change for performance improvement:
Code:
awk 'which!=n&&$0~"^[ \t]*"patt"[ \t]*$"{which++;if(which==n)$0=repl}1' patt="$pattern" repl="$new_pattern" n=$n data.db

This User Gave Thanks to elixir_sinari For This Post:
# 11  
Old 09-13-2012
Quote:
Originally Posted by elixir_sinari
That would need a very small change:
Code:
awk '$0~"^[ \t]*"patt"[ \t]*$"{which++;if(which==n)$0=$0 RS repl}1' patt="$pattern" repl="$new_pattern" n=$n data.db

to
Code:
awk '$0~"^[ \t]*"patt"[ \t]*$"{which++;if(which==n)$0=repl}1' patt="$pattern" repl="$new_pattern" n=$n data.db

Also, I would suggest a small change for performance improvement:
Code:
awk 'which!=n&&$0~"^[ \t]*"patt"[ \t]*$"{which++;if(which==n)$0=repl}1' patt="$pattern" repl="$new_pattern" n=$n data.db

I am really greatful. Actually, this line was too much for my knowledge
Both the classic and performance ones do work as expected for replace/insert pattern

Now, just for my knowledge (feel free to bypass this, you already helped me a lot):
would you explain the syntax in each part of this line? Smilie
# 12  
Old 09-13-2012
Code:
$0                     --> current input record/line (awk reads one record/line at a time).
~                      --> match operator.
"^[ \t]*"patt"[ \t]*$" --> this becomes ^[ \t]*chmod 0771 /data[ \t]*$ for your pattern. 
                           This means a pattern beginning (^ is the beginning-of-line/record anchor) 
			   with 0 or more (*) of either space and/or tab ([ \t]), followed by the string
			   "chmod 0771 /data" (of course without the quotes), followed by 0 or more of
			   either space and/or tab and then the end-of-line/record ($ is the end of line/record anchor).

which++                --> for such lines, increment the variable "which" by 1 (variables in awk do not need
                           declaration; when referenced for the first time, they are null/zero depending on the context).

if(which==n)           --> check if value of "which" has reached that of "n". If yes (that means the
                           required occurrence), set $0 to the concatenated string of $0, RS (record
			   separator,awk's builtin variable, newline by default) and the replacement string
			   ($0=$0 RS repl).

1                      --> that's a pattern which is always true. So, whether the previous actions changed 
                           $0 or not, print the current line/record.

This User Gave Thanks to elixir_sinari For This Post:
# 13  
Old 09-13-2012
I got it know
Thank you for all the time you spent with my issue

A last (I hope) question

What would be the next 2 commands if I want to insert new_pattern before the pattern

Actual insert command is:
Code:
awk '$0~"^[ \t]*"patt"[ \t]*$"{which++;if(which==n)$0=$0 RS repl}1' patt="$pattern" repl="$new_pattern" n=$n data.db

performance one:
Code:
awk 'which!=n&&$0~"^[ \t]*"patt"[ \t]*$"{which++;if(which==n)$0=$0 RS repl}1' patt="$pattern" repl="$new_pattern" n=$n data.db

# 14  
Old 09-13-2012
Change
Code:
$0=$0 RS repl

to
Code:
$0=repl RS $0

.
This User Gave Thanks to elixir_sinari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to extract and print first occurrence of pattern in each line

I am trying to use awk to extract and print the first ocurrence of NM_ and NP_ with a : before in each line. The input file is tab-delimeted, but the output does not need to be. The below does execute but prints all the lines in the file not just the patterns. Thank you :). file tab-delimeted ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Insert content of file before the first occurrence of a line starts with a pattern in another file

Hi all, I'm new to scripting.. facing some problems while inserting content of a file into another file... I want to insert content of a file (file2) into file1, before first occurrence of "line starts with pattern" in file1 file1 ====== working on linux its unix world working on... (14 Replies)
Discussion started by: Jagadeesh Kumar
14 Replies

3. Shell Programming and Scripting

How to display when nth line match a pattern?

Hi All, I have sample of listing as following Database 2 entry: Database alias = PXRES Database name = PXRES Local database directory = /db2/data1/db2phnx Database release level = d.00 Comment ... (3 Replies)
Discussion started by: ckwan
3 Replies

4. Shell Programming and Scripting

Insert certain field of matched pattern line above pattern

Hello every, I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|" eg > # @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp Pleistocene"|Q # @P... (5 Replies)
Discussion started by: jyu3
5 Replies

5. UNIX for Dummies Questions & Answers

Printing nth and n+1th line after a pattern match

Hi , I want to print the nth and n+1 lines from a file once it gets a pattern match. For eg: aaa bbb ccc ddd gh jjjj If I find a match for bbb then I need to print bbb as well as 3rd and 4th line from the match.. Please help..Is it possible to get a command using sed :) (6 Replies)
Discussion started by: saj
6 Replies

6. Shell Programming and Scripting

Getting filename for Nth line pattern match

Hi, I have many scripts in particular directory. And few of the scripts have exit 0 in second line. Now i wanted to list out the scripts name which has the exit 0 in its second line I tried many options , but i can not get the filename along with the nth line pattern match :mad:. Can anyone... (14 Replies)
Discussion started by: puni
14 Replies

7. UNIX for Dummies Questions & Answers

line number of the i-th occurrence of a pattern

Hi all, is there a simple way to obtain the line number of the i-th occurrence of a pattern? I have OCCURRENCE=`grep -io "${STRING_NAME}" ${1}-${8}${EXT}.out_bis| wc -l` which tells me how many occurency I have. I would like to go through them and determine the line number and assign... (6 Replies)
Discussion started by: f_o_555
6 Replies

8. Shell Programming and Scripting

Count the number of occurrences of a pattern between each occurrence of a different pattern

I need to count the number of occurrences of a pattern, say 'key', between each occurrence of a different pattern, say 'lu'. Here's a portion of the text I'm trying to parse: lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1 p-reserver IID 0xdb registrations: key 4156 4353 0000 0000 ... (3 Replies)
Discussion started by: slipstream
3 Replies

9. Shell Programming and Scripting

Insert two newline after some pattern

Hi, I need to insert two newline characters after matching of a pattern in each line of a file. Eg. If i have a file with contents as follows:- Now, i want output as follows :- i.e., I need to insert two newline characters after the occurance of pattern "</Message>>". Thnx... (1 Reply)
Discussion started by: DTechBuddy
1 Replies

10. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies
Login or Register to Ask a Question