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
# 1  
Old 09-13-2012
Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi,

I am getting crazy after days on looking at it:
Bash in Ubuntu 12.04.1

I want to do this:
pattern="system /path1/file1 file1"
new_pattern=" data /path2/file2 file2"
file to edit: data.db

- I need to search in the file data.db for the nth occurrence of pattern
- pattern must match a complete line
- insert after pattern a new line with new_pattern

Sed in my installed Ubuntu does not seem to accept /c or /a commands, only -i !!

So, I tried this:
Code:
sed "s%system /path1/file1 file1%&\n    data /path2/file2 file2%" -i data.db

Which works except:
1- all occurrences of pattern will be replaced in data.db, not only the nth one
2- pattern will be matched even if it is not ending with a new line

So, tried to add \n at end of pattern to fix issue 2:
Code:
sed "s%system /path1/file1 file1\n%&\n    data /path2/file2 file2%" -i data.db

Result is pattern will not be replaced at all

and to fix issue 1, I tried the following with and without the -i:
Code:
sed "0,%system /path1/file1 file1%s%system /path1/file1 file1%&\n    data /path2/file2 file2%" [-i] data.db

No output error, but pattern is not matched

Any help please even if it is with another command
Difficulty seems to be caused by the / and spaces in my patterns!
# 2  
Old 09-13-2012
Code:
pattern="system /path1/file1 file1"
new_pattern=" data /path2/file2 file2"
n=3
awk '$0==patt{which++;if(which==n)$0=$0 RS repl}1' patt="$pattern" repl="$new_pattern" n=$n data.db

Replace value of n as per your requirement.
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 09-13-2012
Thank you but it did not work
I just made n=1
Output was the content of my data.db file in terminal
and the file data.db was not edited
Smilie
# 4  
Old 09-13-2012
data.db will not be edited by that command (as opposed to that done by the -i switch of sed). You'll need to redirect the output of that command in a temporary file and then, rename that temporary file to data.db. Was the first occurrence of that pattern replaced by the replacement string in the terminal output?

It would help to post a representative sample of data.db. Also, mention the output produced by that command on this sample and the expected output.

Last edited by elixir_sinari; 09-13-2012 at 07:16 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 09-13-2012
Quote:
Originally Posted by elixir_sinari
data.db will not be edited by that command (as opposed to that done by the -i switch of sed). You'll need to redirect the output of that command in a temporary file and then, rename that temporary file to data.db. Was the first occurrence of that pattern replaced by the replacement string in the terminal output?

It would help to post a representative sample of data.db. Also, mention the output produced by that command on this sample and the expected output.
Ah, I see
No way to see in terminal as data.db contents are too long
I redirected to a text file with this command:
Code:
pattern="system /path1/file1 file1"
new_pattern=" data /path2/file2 file2"
n=3
awk '$0==patt{which++;if(which==n)$0=$0 RS repl}1' patt="$pattern" repl="$new_pattern" n=$n data.db >test.txt

The output test.txt file has exactly same contents as original data.db, binary matching. So, the patetrn was not replaced Smilie

---------- Post updated at 12:21 PM ---------- Previous update was at 12:20 PM ----------

Ok, I will post details of files in code
# 6  
Old 09-13-2012
For your info:
Code:
$ cat data.db
HHOOIOIOI
system /path1/file1 file1
          system /path1/file1 file1
/system/path
system /path1/file1 file1
dfdfdf
dfdffffffffffff
dfdf
system /path1/file1 file1

$ pattern="system /path1/file1 file1"
$ new_pattern=" data /path2/file2 file2"
$ n=1

$ awk '$0==patt{which++;if(which==n)$0=$0 RS repl}1' patt="$pattern" repl="$new_pattern" n=$n data.db
HHOOIOIOI
system /path1/file1 file1
 data /path2/file2 file2
          system /path1/file1 file1
/system/path
system /path1/file1 file1
dfdfdf
dfdffffffffffff
dfdf
system /path1/file1 file1

Is your actual pattern surrounded by blanks on either side? Do you want to match such lines?
This User Gave Thanks to elixir_sinari For This Post:
# 7  
Old 09-13-2012
You got it right

pattern can sometimes be surrounded by white spaces
pattern=" system /path1/file1 file1 "
but sometimes only:

pattern="system /path1/file1 file1"

and sometimes a tab space
Code:
pattern="	system /path1/file1 file1"

correcting the spaces in $pattern fixed it, many thanks Smilie

However, it would be great if I can match those before/after spaces and tab

I am posting a file sample...
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