Sponsored Content
Top Forums Shell Programming and Scripting Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04 Post 302700191 by Phil3759 on Thursday 13th of September 2012 05:42:00 AM
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!
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 01:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy