Programming :: Replace 2nd Occurrence Of A String In A File - Sed Or Awk?

Apr 5, 2010

I know how to replace a particular instance (say 3rd one) of a word in a line using sed based on the sed one-liners. However I would like to replace a particular instance of a word in the entire file.

For example, here is a file:

Code:
John
Betty
Jack
Ron
Jack
Paul

So now I would like to replace the second instance of Jack (in red color) with "Rob" (for example). Not quite sure how to do that? I tried couple of things from here but they did not work.

View 10 Replies


ADVERTISEMENT

Programming :: Replace Occurrence Of A String In Many Files In One Command?

Oct 30, 2010

I used a command Code:

grep -r err_sys ./
its output is
Code:
./tcpclitime.c:err_sys("socket error");
./tcpclitime.c:err_sys("connect error");
./tcpclitime.c:err_sys("write error");
./tcpclitime.c:err_sys("shutdown error");
./tcpclitime.c:err_sys("read error, n = %d", n);
[Code].....

Is it possible some how to replace all the occurrences of word err_sys by printf.

View 1 Replies View Related

Programming :: Count The Number Of Occurrence Characters In A String ?

Jan 26, 2011

How do I count the number of occurrence characters in a string in an efficeint way ?

How do I reverse the words in a string with out using temp variables if possible . (if not possible use temp variables ?

Why do I specify dependedcies for header files in the Makefile ?

View 2 Replies View Related

Programming :: Replace A String Within A File ?

Feb 28, 2009

I've found some scripts that replaces a string in a file but it's not quite working for me.

And I'm trying to replace a tag in an xml file that looks like this

So I ran a command line like this perl -w -i -p -e "s/xmlns="'http://mydomain.org/replacethese.xsd'">/>/g" testfile.xml

And I get a error output

Search pattern not terminated at -e line 1.

Found the script from this blog [url]

View 7 Replies View Related

Programming :: Replace String In File Using Perl Or Sed

Jun 30, 2011

I have question about replacing a string in a file.

How can I replace the printerb's 10.1.1.1 to something else(10.1.1.2, for example) without replace printera's 10.1.1.1 accidentally?

I have tried perl -e -pi "s/10.1.1.1/10.1.1.2/g" /etc/hosts. but, perl replace both 10.1.1.1 to 10.1.1.2.

View 13 Replies View Related

Programming :: Perl Find File And Then Replace String In File

Jul 28, 2009

I have script that I'm working on that updates a username in all the files that are called blah.inc for my framework. since i host a bunch of these web apps i need to do it to all of them. so I need to figure out how to update these files automagically with out me watching it to call vim every time. heres what I have so far

Code:

This finds the files but now i need to figure out how to do s/bob/fred/g on those files.

View 5 Replies View Related

Programming :: Text File Replace Occurrences Of Three Character String ZZZ With Quotation Mark

Mar 30, 2010

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.

View 9 Replies View Related

Programming :: Find And Replace A String In A File Using Perl Command From Bash Script?

Feb 14, 2011

I wanted to find and replace a string from a perl file. I have written a script in bash which runs the following command.

perl -pi -e "s/$findstring/$replacestring/" testfile
where as $findstring = print F_WC_TMP"$line
";
and $replaceString = $line = join ' ', split ' ', $line; print F_WC_TMP"$line
";

But when I am running the above command, i think it is replacing the $findstring with the above mentioned string and hence it contains a $line, it is looking for the variable $line and not finding the exact string. I am confused about how to search for a string that contains $ in it and replace it with another $string.

View 5 Replies View Related

Programming :: Using Sed Replace Line Containing Particular String By A Single String?

Oct 16, 2010

I have a set of lines as ahown below:

Leon went to school
Leon came back from school..
Leon had dinner...

I have to replace the line containing "dinner" by a single string LUNCH...

View 2 Replies View Related

Software :: Replace A Text String In A File With A Random String - With Sed ?

Sep 2, 2010

I have a line in a text file that has 40 random characters within a tag and i want to change the characters to a new set of 40 random characters (alphanumeric a-z 0-9 etc)

The line in the text file looks like this:

Quote:

How would i go about doing that?

Also second question same as the above but how would i remove them instead of replacing them?

View 14 Replies View Related

Fedora :: Replace A String By A String In Text File?

Apr 26, 2011

If I have a word in a text file and I need to replace it by another word (for example, i need to replace abc by fff) so what is the command I can type it?

I am using vi editor.

View 3 Replies View Related

Programming :: Replace The Last Character String?

Jan 19, 2011

how to replace last character string. For example

$>export T1=abcde
$>export T2=xyz

how to get result abcdxyz?

View 10 Replies View Related

Programming :: Replace All Digits With Symbol In String?

Aug 29, 2010

I am trying to replacee all digits with a symbol (say, big U here) in a string by

[code]...

The result become 'UsUoUmUeUtUhUiUnUgU' instead of 'somethingUUU' as expected. Looks like my string contains some 'hidden digits' in between the letters. Does anyone have an idea about that?

View 2 Replies View Related

Programming :: Replace String By Incrementing Number

May 30, 2011

Say I have a file as

[code]....

It would be great, if i could issue a command (Vi/Bash/awk, ...) in order to obtain

[Code]...

So, the "replace string" should be replaced by another string containing the counter of replacement.

View 7 Replies View Related

General :: Using Sed To Replace String With Special Characters In XML File?

Apr 29, 2011

I am having difficulty getting sed to replace a string of text in an XML file, despite the fact that I have no trouble using grep to find that same string. Since the new string and old string to be replaced contain a lot of special characters, I thought it best to store them in variables as opposed to using a slew of backslashes:

OLD_STRING='<property name="webServiceHost">${jboss.bind.address}</property>'
NEW_STRING='<!--<property name="webServiceHost">${jboss.bind.address}</property>-->'

[Code]....

View 2 Replies View Related

General :: Replace A String In Multiple File & Sub Directories?

Aug 31, 2010

Need to replace the following string :

By

In multiple .exp files and in sub directories.

View 5 Replies View Related

General :: Replace Nth Occurence Of String In Each Line Of A Text File?

May 20, 2010

I have large text files with space delimited strings (2-5). The strings can contain "'" or "-". I'd like to replace say the second space with a pipe. What's the best way to go?
Using sed I was thinking of this:

sed -r 's/(^[a-z'-]+ [a-z'-]+) /1|/' filename.txt

View 2 Replies View Related

General :: Replace A String In A File Using A Script With Path Containig /

Feb 26, 2010

I need to replace a string in a file(startup.sh) using a script(parser.sh). After running parser.sh startup.sh should be filled with nfs path like /home/vimal etc but im getting error since path contains /. how to remove this.

View 3 Replies View Related

General :: Replace String With Empty String Of Dir Path ?

Apr 2, 2011

I want to replace a string of directory path in a string to empty:

Code:

But this doesnt seem to give me the desired thing:

Code:

This gives the desired outcome, but its specific, i need a variable in the sed not a string. And if I replace STRING="/mnt/sda1/record/$dd/" then I cant use it for something else, cause its has all the weird backslashes now.

View 3 Replies View Related

Programming :: Copy String A To String B And Change String B With Toupper() And Count The Chars?

Oct 22, 2010

copy string a to string b and change string b with toupper() and count the chars

View 3 Replies View Related

General :: Awk Gsub() Command - String (column) Manipulation - Replace The Value Of The $1 Column In The Awk Print String?

Mar 7, 2010

i use this script to get the time and date of back and fourth transactions for a particular execution id. I use a substr command on the 5th column to to cut the milli seconds off the time value. - otherwise the times would look like 08:30:04.235

grep <executionID> <auditfile> | awk '{ print $1, $2, $3, $4, substr($5,1,8}
FIX -> Mon 3/1/2010 08:30:04
FIX <- Mon 3/1/2010 08:32:36
FIX <- Mon 3/1/2010 08:35:08

[code].....

anyhow - i append two sed commands to further clarify the direction of the message.

awk '{ print $1, $2, $3, $4, substr($5,1,8} | sed -e 's/->/ ->IN/g' | sed -e 's/<-/<-OUT/g'
FIX -> IN Mon 3/1/2010 08:30:04
FIX <- OUT Mon 3/1/2010 08:32:36

[code]....

I tried using an awk gsub () command within the string instead of the two seds, but it did not work:

awk '{ print gsub(/<regex>/, <replace with>,$1), $2, $3, $4, substr($5,1,8}

the sed works ok, but it would be cooler to make the replacement within the awk command:

gsub(/->/,-> IN, $1)

Is there a way where i could replace the value of the $1 column in the awk print string?

View 1 Replies View Related

Software :: Best Software To Replace A String In A Big Text File?

May 22, 2011

I have a big text file (350mb) and i need to replace some strings in it.

The VIM is too slow. Also I don't need to open file and view its contents. I just need to give the command to replace it for faster processing.

Which software would be the best for this purpose?

View 7 Replies View Related

General :: String Replace In Filenames - Files Already Contain The String "Soundtrack"

Jun 11, 2011

Moving right along, I have a folder of MP3 files containing various Movie sound tracks and scores. I'm using Audio Tag Tool to tag all the files at once with an "Artist" of "Soundtrack", and to inherit the "Title" tag from the file name. After that, I will rename all the files (Using Audio Tag Tool -- awesome program, btw) with the format "<Artist> - <Title>.mp3"

The problem, is many of my files already contain the string "Soundtrack", which would be redundant. I happen to be a perfectionist, so I'm unable to ignore it and move on. Hence my question to you fine folk: I want to delete all instances of "soundtrack" (-i case irrelevant) in the filenames before I go through the above steps. But, its not quite that simple. This is a sample of some of the file names:

[Code]....

View 3 Replies View Related

Programming :: Awk Command - Search Pattern To Look For Last Occurrence

May 11, 2011

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.

View 14 Replies View Related

Programming :: Add Time Of Occurrence For Mouse Event In Xinput?

Jan 24, 2011

i need to have the time of occurence for mouse event in xinput,right now i run

Code:
xinput test <device_id> >log.txt
(for finding device_id using xinput list)

[code]...

View 4 Replies View Related

Programming :: Variable String File Base - Output File Doest Not Exclude The Values In Grep -av

May 1, 2011

My script.

This is may script:

Code:

Problem: Output file doest not exclude the values in grep -av

View 3 Replies View Related

Programming :: Using File As Replace In Sed

Mar 1, 2011

I have a file, "outfile" what I wish to insert into a file called template where it finds "OVERRIDES". I am getting an error.

Code:

However I can do something like this and it works fine

Code:

I am guessing its because of the file size or multiple lines.

A sample of the outfile text is below

Code:

View 9 Replies View Related

Programming :: Substitute Every Occurrence Of "( S" To "(S" In File

Mar 11, 2011

I want to substitute every occurance of "( S" to "(S" in my file (the quotes aren't included in my file. Its just there to specify what my pattern is)

This is my attempt to do it and the error message I get.

Code:
denish@ubuntu:~/Desktop/tgrep files$ sed 's/^( (S$/(S/g' test > test2
sed: -e expression #1, char 16: Unmatched ( or (

The "( )" has a different purpose in sed. So how exactly do I specify a ( in sed as a pattern and not as the special function?

View 7 Replies View Related

Programming :: Replace Part Of A File With Another?

Jan 8, 2010

There are a few things I was wondering about (using tools available in bash):How to insert a file at at the specified location of another.How to copy a portion of a file between two lines matching a regex to another file (and/or making sed only work between two lines matching a regex)How would you do this?

View 2 Replies View Related

Programming :: Finding Only String (Already There) In File

May 10, 2010

I have a file in which I have string called "already there" in some line of it. I want that if the string is present enter some loop..

like
if(( grep "already there" $file is true ))
then
--
--
fi

But the thing is when I make grep it gives the whole line in which this string is present and gives some error: if[ grep "already there" file ]: command not found. I am very new to shell scripting.

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved