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:
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:
Result is pattern will not be replaced at all
and to fix issue 1, I tried the following with and without the -i:
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!
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:
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:
The output test.txt file has exactly same contents as original data.db, binary matching. So, the patetrn was not replaced
---------- Post updated at 12:21 PM ---------- Previous update was at 12:20 PM ----------
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)