Programming :: S/ Command Won't Replace Two Occurrences Of Pattern On Same Line
Dec 15, 2009
Here's the actual line of code, which exists in a bash script:
Code:
I want to replace instances like this:
Code:
with this:
Code:
Using this:
Code:
Which works great when there's only ONE of the pattern on the line. But in a case like the "actual line" I posted first, where there are two patterns, separated by a slash, only ONE gets replaced
Watch:
Code:
Why? There must be (among many other things) something I'm not knowing about sed, that's causing this.
-- I'm currently using the ~ (tilde) as the separator in the sed command. It doesn't matter, I've used / ~ and % with no difference.
-- As a test, I tried putting a different character(s) in the middle of the original pattern instead of the / but that made no difference.
-- I've come up with various similar but slightly different regexs that will do this replacement, but they all have had this same result.
-- I tried the sed single-quoted, double-quoted, and unquoted; the latter fails to execute, and the formers both work as described here: wrong.
I want to search a file for a particular pattern and if pattern found replace the line with new text. i am using awk 'match($0,"pattern") != 0 {print $0} ' filename to check if the pattern exists.how do i get the line number of the pattern and delete that line and replace the line with my new text?
I'm trying to process a postscript file and I want to change alternate pages by finding the string '%%EndPageSetup' (and then adding a line after it).
I've tried using sed with labels and a branching program to ignore the odd occurrences and process the even ones. In the code below I tried searching for the first and ignoring it by jumping to next and processing it and then to the next odd page and so on.
My logic is obviously flawed here as it doesn't work but I've tried the following:
Code: #!/bin/bash sed -e '/%%EndPageSetup/{ b even :odd /%%EndPageSetup/ b even :even s/%%EndPageSetup/%%EndPageSetup++/ b odd}' filename.ps > newfile.ps
I want to use SED to do the following: In a text file replace any occurrences of the three character string ZZZ with a quotation mark "and. replace all occurrences of a comma with a semi-colon. It is the S/ / / command which is stumping me on the first issue...inparticular how to get the replace string to be quote.
I want to read from the file and check for the pattern, if the line has some word like <string>: then string should be copied into buffer. Afterwards, I want to insert the same <string> with some word in the next line of the file. use sed command to perform the above mentioned operations?
I want to list all the files that don't have a copy with the same filename with -1 somewhere in it. So, in the example above, the results would be 3.png.
NB: the file and its copy with "-1" in it will be the same filesize, if that helps.
if the given pattern exists in the file with the very next line starting and endingwith the same pattern , delete the line that starts and ends with the given pattern.So upon running on this file
hai people<PATTERN> we had <PATTERN>a lot of fun<PATTERN> writing scripts
I have a question about sed programming, actually a one-liner for which I cannot find a solution, right now. I need to delete a line matching a specific pattern only if it is the last line. In practice, I would put together the following:
At the moment I have a flat file which is being used by a few people. I want a script to remotely change the file, so I can start logging who is doing what.At this point here is one requirement I am trying to develop. We have text blocks who pretty much look like.I hope this is somewhat clear. I try to find $param for the right $workflow and change that. Can you help me to find $$var3 and change that?
I would like to insert prior to the word "three" all items from this second file with the following contents:
Code:
four three two one
Now my issue is, and I have been using both sed and awk currently, that after the second line of the new file is read there will of course now be 2 copies of the word "three" but I would like to only insert the final 2 words, ie "two" and "one" prior to only the first occurrence of the word "three" so final file will look like:
Code:
one two four
[code]....
So here there is now only one of each word from the second file joined to make the new file. For simple code I have tried something like the following:
Code:
while read line do awk -v n=$line '!f && /three/{print n;f++}1' file1 > tmp_file mv tmp_file file1 done < file2
Now this works but seems very clumsy to me. There is obviously a better sed and / or awk out there.
I have several (vhdl) files containing a pattern with newline characters that I need to replace by another pattern that also contains newline characters.
I start with something like:
Code:
I want to replace it by something like:
Code:
(I need to paste some lines)
As I need to do this (very) often I want to use a shell script.
I am trying to create a shell script, on taking a input file as parameter, which need to do 3 things
1) create a copy of existing file.
2) add a new line to the copied file.
3) strip off all the absolute paths inside the copied file
The first 2 points are straight forward. but i am finding it difficult to acheive the 3rd point. myself not very good with awk and sed. but gave it a shot in vain. For example, the input script consists of below,
Any solution using awk/sed/regexp or other standard linux utility (this is for a mix of RH versions)? I am dealing with some very large application log files. I want to see everything that has been written to the log since the last application restart.
For an example take a log file like this: Code: # cat test.log 1 msg 2 msg 3 restart 1 4 msg 5 restart 2 6 msg
The following command is close to what I want: Code: # awk '/restart/,G' test.log 3 restart 1 4 msg 5 restart 2 6 msg
But the awk command grabs the first restart not the last. If it was working the way I wanted I would see something like this: Code: # awk '/restart/,G' test.log 5 restart 2 6 msg
So, I need something in that search pattern that says look for the last occurrence. I know how to do this with a pipe line - I could reverse the file and then do a similar awk and reverse back, or I could find the number associated with the last restart and then use that in the awk search. But these just take too long because the file is too big.
I'm writing a script that edits a Maya ascii file. Inside the .MA(maya ascii file) there is a line defaultRenderGlobals. My script is supposed to find this line and according to what options they manipulate will update the lines below defaultRenderGlobals. I've got that working... but... the issue i'm having is that defaultRenderGlobals is only made when a the maya scene is made into a batch render. I want my script to manually addefaultRenderGlobals line into the .ma file if its not there and add the certain lines below it.RenderGlobals is already there I want to just manipulate whats below it. Hope this makes sense
I have a jar, and I need to replace a class in it, at this moment, I can only open it with "archive manager" and then drag and drop the new compiled class into the jar, but I think this is really boring, if I can do with with just a command ?
I want to know the Perl command to replace a string by pointing the line number. I know how to replace a string without pointing a line number but I am in need to replace only the two matching string in a file
I'm pretty sure this is doable from the command line, but my CLI skills have degraded a lot since my pre-Y2K admin days. The goal is to search all the files in the directory for a very long string of text and replace it with another string of text. The text being searched for is my Google Adsense code (which will be stripped from my website) and it will be replaced with a placeholder so I can easily tack something else in there in the future.
Seeing how I have that long snip of code on about 100 pages, automating the process would make life easier. If I was searching for a single word, I can see ways to do this. If I paste the code I'm searching for into a text file, is there a way to: find (contents of oldstring.txt) and replace with (contents of newstring.txt)?
I have to enhance the behaviour of a backup script written in perl. I don't need to change it, what I need to do is to create a bash script that does some checks like file name and file size, execute the backup script then check if the backup files match the original files.Here's how I try to do it:
- read the files from the original files folder - store them in an array - search in the array the files that have a specific file extension - store the file names that match the search pattern (I know the backup script skips some files so I can hardcode the search pattern) - run the backup script - read the files from the backup folder - store them in an array - compare the original files name and size stored in an array with those from the backup folder - send a report email
I need to replace a line in the lvm.conf file from: filter = [ "r|/dev/.*/by-path/.*|", "r|/dev/.*/by-id/.*|", "a/.*/" ] to: filter = [ "a|/dev/cciss/c0d0.*|", "a|/dev/disk/by-id/.*|", "r|.*|" ] And I was hoping there was a way to use sed or awk to do this in a script (as I need to repeat this on several machines).
All of the examples I've found don't see to work with all of the special characters in the line that I need to replace. Then I need to add a line after the above edit like this: types = [ "device-mapper", 253 ]
I need a command to search a string in a file and then to convert the next string in the same line from hexadecimal to binary. I was able to put everything in capitals. The original file can be as such:
E 2 C 1 794 T ffff E 2 C 1 787
It is not always FFFF! I am trying to do this in a file at once, not reading line by line (using while).
I would like to have a script that will do a search in an XML file for a specific block of XML code and replace it with a different block of XML code using the Windows build of GNU Sed 4.1.5.
Here's the text I want to find:
Code:
And here's what I want to replace it with:
Code:
There is however a lot of stuff in there that would mess up sed, and the one example from the sed FAQ (from the Sourceforge site) that looked promising and easy enough to figure out (4.23.3. Try to use a block of "literal strings") fails with this error message: sed: file blockrep.sed line 18: unterminated `s' command
Anyone got any ideas, or even an alternate program that can do this? (Preferably one that I can automate.)
I am bouncing a file across platforms (windows->solaris->mainframe), and the file is starting out with a "special" character (the registered trademark "circle R") in some of the records. This character is not in the EBCDIC character set on the MF, so it is unrecognizable. The MF developer I am working with asked if it is possible to replace the character with a specific hex value (AF) before it gets to the MF.
I was putzing around with sed, tr, etc. on the ksh command line, hoping to find an easy way to get one of them to substitute hex instead of ASCII. I have found that the usual shell utilities recognize the trademark character, so homing in on what to replace is solved. But I cannot get anything to actually substitute in the hex sequence I want. E.g. I was thinking something like...
>cat special_file | sed 's/R/AF/g'
But my version of sed does not seem to have hex "editing" capability.
Does any one know what syntax i could use to allow me to replace all instances at the beginning of a line with ones.
Before :
Code: ---------------------------------------------------------------------- Logical device information ---------------------------------------------------------------------- Logical device number 0 Logical device name : RAID1Mirror RAID level : 1 Status of logical device : Optimal
After
Code: ---------------------------------------------------------------------- Logical device information ---------------------------------------------------------------------- Logical device number 0 111Logical device name : RAID1Mirror 1111RAID level : 1 11111Status of logical device : Optimal
I am beginner in this business of shell script and I have no idea how to do the following: I would like to replace the lines of my file that contains 'CFL=' by 'CFL=0.5'. Note that I want to replace the full line meaning