sed

Perform action on line range in sed/awk

How can I extract certain variables from a specific range of lines in sed/awk? Example: I want to exctract the host and port from .tnsnames.ora from this section that starts at line 105. DB_CONNECTION= (description= (address= (protocol=tcp) (host=127.0.0.1) (port=1234) ) (connect_data= ...

how to split lines in unix log file output

Hello, I'd like to be able to parse date and times out of a log file. Currently they're in the following format: "02/Jun/2009:14:38:50" but i'd like to separate them in different columns using something available from a linux command line so that the resulting output looks as follows: "02/Jun/2009" "14:38:50" could someone please sh...

sed line range, all but the last line

You can specify a range of lines to operate on. For example, to operate on all lines, (which is of course the default): sed -e "1,$ s/a/b/" But I need to operate on all but the last line. You apparently can't use arithmetic expressions: sed -e "1,$-1 s/a/b/" (I am using cygwin in this case, if it make a difference) ...

Split remaining fields in sed/awk

I have the line: MAVEN_OPTS=-XX:MaxPermSize=128m -Xms128m -Xmx768m and want to convert it to (add quotation marks on field 2->n): export MAVEN_OPTS="-XX:MaxPermSize=128m -Xms128m -Xmx768m" ...

Safe rm -rf function in shell script

This question is similar to http://stackoverflow.com/questions/373156/what-is-the-safest-way-to-empty-a-directory-in-nix I'm writing bash script which defines several path constants and will use them for file and directory manipulation (copying, renaming and deleting). Often it will be necessary to do something like: rm -rf "/${PATH1}"...

Locate postion then make a change using sed.

This script uses sed to change all "" to "new stuff". How would one change just the "" after the yyy: using sed or anything else? cat >sample.txt <<EOF xxx: "" yyy: "" } EOF sed --expression='s/""/"new stuff"/' sample.txt ...

sed to replace random numbers

Hello, i need to let sed replace some url in alot of file each file has the following http://www.expample.com/file.php?id=xxxxxxx where xxxxx consist of random numbers , random depth in each file like file 1 _h**p://www.expample.com/file.php?id=xx file 2 _h**p://www.expample.com/file.php?id=xxxxxxxx etc thanks in advance ...

Using bash command line, how to add "import package.name.*;" to many java files?

I'm thinking of using find or grep to collect the files, and maybe sed to make the change, but what to do with the output? Or would it be better to use "argdo" in vim? Note: this question is asking for command line solutions, not IDE's. Answers and comments suggesting IDE's will be calmly, politely and serenely flagged. :-) ...

Match single character between Start string and End string

Hello. I can't seem to understand regular expression at all. How can I match a character which resides between a START and END string. For Example #START-EDIT #ValueA=0 #ValueB=1 #END-EDIT I want to match any # which is between the #START-EDIT and #END-EDIT. Specifically I want to use sed to replace the matches # values with not...

sed whole word search and replace

Hi, How do I search and replace whole words using sed? Doing sed -i 's/[oldtext]/[newtext]/g' will also replace partial matches of [oldtext] which I don't want it to do. Thanks, Kenneth ...

sed to replace random site urls in ifrrame

Hello, my problem there is alot of pages infacted with iframe each one of them have different url or different id here is example <iframe src="http://xxxxxx.xxxx/xxxx.xxx" width=xxx height=xxx style="visibility: hidden"><iframe> or <iframe src="http://xxxxxx.xxxx/xxxx.xxx?xxx=xxxx" width=xxx height=xxx style="visibility: hidden">...

Substituting text in a file within a Perl script

I am using webmin and I am trying to change some settings in a file. I am having problems if the person uses any weird characters that might trip up sed or Perl using the following code: &execute_command("sed -i 's/^$Pref.*\$/$Pref \"$in{$Pref}\"/g' $DIR/pserver.prefs.cache"); Where execute_command is a webmin function to basically ru...

What is the actual Perl code that will mimic -pi inside the program?

This seems redundant, running perl from a Perl script itself. my $Pref = "&*())(*&^%$#@!"; system("perl -pi -e 's|^SERVERNAME.*\$|SERVERNAME \"\Q$Pref\E\"|g' pserver.prefs"); What is the actual Perl code that will mimic -pi? I just want something that would work like sed on Perl, just as simple as can be. Based on Todd Gardner's s...

Get a list of delimited filenames from a text file

Hi, I'm really new to Bash, so this could sound silly to most of you. I'm trying to get a list of some filenames from a text file. Tried to do this with sed and awk, but couldn't get it to work with my limited knowledge. This is a sample file content: <?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 13.0.1, SVG ...

Escaping the '\' character in the replacement string in a sed expression

I am trying to take a line of text like 13) Check for orphaned Path entries and change it to (I want the bash color codes to colorize the output, not display on the screen) \033[32m*\033[0m Check for orphaned Path entries with bash color codes to colorize the asterisk to make it stand out more. I have a sed command that does m...

sed: Replace part of a line.

How can one replace a part of a line with sed? The line DBSERVERNAME xxx should be replaced to: DBSERVERNAME yyy The value xxx can vary and there are two tabs between dbservername and the value. This name-value pair is one of many from a configuration file. I tried with the following backreference: echo "DBSERVERNAME ...

Duplicate files with similar file names in UNIX

I have a set of files in a single directory named: OneThing-x.extension OneThing-y.extension OneThing-z.extension What UNIX command (find, cp, xargs, sed ?) do I use to COPY these into AnotherThing-x.extension AnotherThing-y.extension AnotherThing-z.extension such that x ->copy-> x, and y ->copy -> y I have the find .. part of the...

sed to replace TTLs in a tinydns zonefile?

I'm working to replace a large set of TTLs inside some tinydns zonefiles. Regardless of what the TTL is currently set to, I want it to become 900. I'm looking for some sed magic, and I'm almost there it seems. I have about 50 hostnames to replace the TTL, and the zones are sporadically assigned. The lines in the zonefiles don't need ...

Inserting a line to a known block of text

I define a 'block' of text as all lines between start of file, newline or end of file: block1 block2 block3 anotherblock4 anotherblock5 anotherblock6 lastblock7 lastblock8 Any text can occupy a block - it is unknown what lines are there. I tried to write a shell script to insert a new line at the 2nd block, but since sed doesn't lik...

Non greedy regex matching in sed?

I'm trying to use sed to clean up lines of URLs to extract just the domain.. e.g., from: http://www.suepearson.co.uk/product/174/71/3816/ I want: http://www.suepearson.co.uk/ (either with or without the trainling slash, it doesn't matter) I have tried: sed 's|\(http:\/\/.*?\/\).*|\1|' and (escaping the non greedy quantifier) ...