sed

Replace a line in a text file

Hello I've a text file with a line default_color acolor and I want to replace this line with default_color anothercolor I don't know the first color and the second is contained in a variable. How can I do it in bash ? Thank you ...

Count the number of occurrences of a string using sed?

I have a file which contains "title" written in it many times. How can I find the number of times "title" is written in that file using the sed command provided that "title" is the first string in a line? e.g. # title title title should output the count = 2 because in first line title is not the first string. Update I used awk to f...

sed: using search pattern in output

I'd like to use the search pattern in the output part of a sed command. For instance, given the following sed command: sed '/HOSTNAME=/cHOSTNAME=fred' /etc/sysconfig/network I'd like to replace the second instance of "HOSTNAME=" with some escape sequence that references the search term. Something like this: # this doesn't actually ...

Find and replace in xml file using sed

Hello, I need to find and replace the value of the specific xml element. The conditions are as follows: the value of element enabled must be changed from 0 to 1; enabled must be the child of an somenode element My test xml looks like this: <somenode name="node1"> <some></some> <enabled>0</enabled> <some></some> </somenod...

sed: delete using a different delimiter

Fairly certain am missing something obvious! $ cat test.txt 00 30 * * * /opt/timebomb.sh >> timebomb.log 01 30 * * * /opt/reincarnate.sh >> reincarnation.log $ sed ':timebomb:d' test.txt 00 30 * * * /opt/timebomb.sh >> timebomb.log 01 30 * * * /opt/reincarnate.sh >> reincarnation.log whereas $ sed '/timebomb/d' test.txt 01 ...

GREP: How to search for a value but at the same time exclude some matches

I need a way to simplify this command: grep 'SEARCHTERM' server.log | grep -v 'PHHIABFFH' | grep -v 'Stats' It should find all the lines including SEARCHTERM but exclude if one of the SEARCHTERM lines includes PHHIABFFH or Stats. ...

How can I change spaces to underscores and lowercase everything?

I have a text file which contains: Cycle code Cycle month Cycle year Event type ID Event ID Network start time I want to change this text so that when ever there is a space, I want to replace it with a _. And after that, I want the characters to lower case letter like below: cycle_code cycle_month cycle_year event_type_id event_id ne...

Sed creates un-deleteable files in Windows

I'm trying to run the following command in Windows Server 2003 but sed creates a pile of files that I can't delete from the command line inside the current directory. for /R %f in (*.*) do "C:\Program Files\gnuwin32\bin\sed.exe" -i "s/bad/good/g" "%f" Does anyone have any suggestions? Mysteriously enough, I'm able to delete the files u...

Comment out N lines with sed/awk

How can I comment out lines from a certain pattern and N lines onwards? int var1; int var2; int var3; int var4; int var5; I want to comment out 3 lines including var2 (and not according to their content!): int var1; // int var2; // int var3; // int var4; int var5; ...

How to make this sed script faster?

I have inherited this sed script snippet that attempts to remove certain empty spaces: s/[\s\t]*|/|/g s/|[\s\t]*/|/g s/[\s] *$//g s/^|/null|/g that operates on a file that is around 1Gb large. This script runs for 2 hours on our unix server. Any ideas how to speed it up? Notes that the \s stands for a space and \t stands for a tab, ...

deleting brackets in a file using sed

Can anyone help me in deleting brackets in a file ? here is my script .. #!/bin/bash for file in fftw is_c mpi_tile pmb tau xhpl do for state in C0 C1 C2 C4 do printf "${file}_${state}_v1.xls" sed -e 's/\(//' ${file}_${state}_v1.xls sed -e 's/\)//' ${file}_${state}_v1.xls awk '{sum+=$3} ; END {print " ", su...

Sed: Replace all whitespace with a line break/paragraph mark to make a word list.

I am trying to vocab list for a greek text we are translating in class, I want to be able to replace every space or tab character with a paragraph mark, so that every word appears on it's own line. Can anyone give me the sed command, and explain what it is that I'm doing? I'm still trying to figure sed out. ...

How to get $this->translate('Content') => Content in a Textfile

I am looking for a shell script which scans a direcotry and all its subdirectories for .php and .phtml files. Within these files, I am looking for $this->translate('') statements (also $this->view->translate('')) and I want to save the content of these statements in a textfile. The problem is, that there are several different types of t...

Makefile rule to check svnversion result

Trying to make a makefile rule to check that svnversion gave a proper result. Normally, it should return something like one of the following: 1023:1055M 1056 However, it can get an error like: svn: This client is too old to work with working copy '.'; please get a newer Subversion client So here is my version of the rule based on ...

Easiest way to extract the urls from an html page using sed or awk only.

I want to extract the URL from within the anchor tags of an html file. This needs to be done in BASH using SED/AWK. No perl please. What is the easiest way to do this? Thanks a lot. ...

Insert double quotes multiple times into string

Hi Folks I have inherited a flat html file with a few hundred lines similar to this: <blink> <td class="pagetxt bordercolor="#666666 width="203 colspan="3 height="20> </blink> So far I have not been able to work out a sed way of inserting the closing double quotes for each element. Probably needs something other than sed to do this. C...

Return a regex match in a BASH script, instead of replacing it

I just want to match some text in a BASH script, i’v tried using sed, but I can’t seem to make it just output the match instead of replacing it with something. echo -E "TestT100String" | sed 's/[0-9]+/dontReplace/g' Which will output: TestTdontReplaceString Which isn’t what I want, I want it to output: 100 Ideally I would want it to...

Replace serial in zone files with sed

Hi, I need to replace the serials in my zone files, and I thought the easiest would be to use sed to change this. I have the following line to test. @ IN SOA ns1.domain.com. webdev.domain.com. ( 2006080401 ; serial 8H ; refresh 2H ; retry 1W ; expire 4h) ; minimum ttl NS ns1.domain.com. NS ns...

Convert stdin to one file per line

I would like to write a small script using the standard linux shell scripting tools (sed, awk, etc.) to create one output file per line of input. For example, given the following input file 'input': line1 line2 line3 I would like to run the command. $ cat input | my_prog Where 'my_prog' is a script that creates three output files, ...

Remove Leading Whitespace from File

My shell has a call to 'fortune' in my .login file, to provide me with a little message of the day. However, some of the fortunes begin with one leading whitespace line, some begin with two, and some don't have any leading whitespace lines at all. This bugs me. I sat down to wrapper fortune with my own shell script, which would remove a...