Replace a string in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a string in a file
# 8  
Old 09-23-2011
@ahamed:Can u please explain me the concept behind your methodology as i am new to shell script

---------- Post updated at 12:21 PM ---------- Previous update was at 12:10 PM ----------

Hi,
Please explain me the concept behind this as i am new to shell script
# 9  
Old 09-23-2011
Code:
sed -e 's/|G|/:|/g' -e 's/||/|/g' input_file > new_file

s/search/replace/globally

's/|G|/:|/g' search for |G| and replace with :| wherever you find it

's/||/|/g' search for || and replace with | wherever you find it

> new_file write into new file

Actually this command alone will do the job
Code:
sed 's/|G|/:|/g' input_file > new_file

Hope this helps.

--ahamed
# 10  
Old 09-23-2011
But if "G" is the first or last field?

I think vgersh99 had all the bases covered. It just looks a bit more intimidated with "#" as separators.

Code:
sed 's/^G|/:|/;s/|G|/|:|/g;s/|G$/|:/'

# 11  
Old 09-24-2011
Replace a string

Thanks for all your help!!But i came to know that data in the file can come as 12345|1|1111|131|678|134121|1|A|BCDEF

Now i want to replace "1" which is highlighted above with "2" then how it can be done??The final output must look like:
12345|1|1111|131|678|134121|2|A|BCDEF

One more thing though "1" can come at any index, this is sure that at 7th index "1" will definetly come and i want to replace this "1" which is present at 7th index with "2"..I tried the following:
sed 's/`cut -d"|" -f7`/"2"/g' but the problem is cut command will extract "1" from 7th index and then sed command is replacing all 1's in a file with 2's
Please help Smilie

Last edited by sam_14189; 09-24-2011 at 01:50 AM..
# 12  
Old 09-24-2011
You want to change the second occurence of |1| to |2| right?
You can't keep on changing the requirements. You will first need to find a pattern.

val = which value you want to replace
newval = value to be replaced with
need = which occurence

Code:
awk -F"|" -v val="1" -v newval=2 -v need=2 '{ for(i=1;i<=NF;i++){if($i==val){ocur++; if(ocur==need){$i=newval}} v=v"|"$i }}
 END{sub(/^\|/,"",v);print v}' input_file

--ahamed
# 13  
Old 09-24-2011
Replace a string

@ahamed:Appreciate your help and sorry about changing the requirements every now and then. Your solution seems to be perfect just that it is not fixed that always i want to replace 2nd occurrence of "1" to "2". The pattern in the file can be like this also:
1|1|1|2|11123|2|1|4|5|6
3333|456|1|345|1111|1|1|6|45|
1234|4|11|1|1|345|1|4|5
So it is not fixed that second occurrence of "1" is to be replaced,thing which is fixed is "1" present at 7th index is to be replaced.
# 14  
Old 09-24-2011
so it looks like you want replace the last occurrence?

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

3. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

4. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

5. Shell Programming and Scripting

How to read file, and replace certain string with another string?

Hi all, the value in the following file is just an example. It could be a different value/network addresses. Here is my example of initial output in a file name net.txt Initial Output, net.txt The goal is to produce the following format which is to convert from CIDR to Netmask... (6 Replies)
Discussion started by: type8code0
6 Replies

6. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

7. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

8. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

9. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

10. Shell Programming and Scripting

How To Replace A String In File With A String Containing Windows File Path

Hi, I have a file with the following contents # Lines that start with a # are comments. # # Calling TOAD like this will perform a comparison from command line : # # "C:\Program Files\Quest Software\Toad for Oracle 9.6\toad.exe" -c... (2 Replies)
Discussion started by: rajan_san
2 Replies
Login or Register to Ask a Question