How to replace partial of string in file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace partial of string in file?
# 1  
Old 06-20-2013
How to replace partial of string in file?

Hi Guys,

I need replace part of string in a file.
for example:
Code:
ABC=123
CDE=122
DEF=456
ABC=123
DED=333
ABC=123

I need replace the value after ABC=, highlighted in red. I want to get following result;
Code:
ABC=456
CDE=122
DEF=456
ABC=456
DED=333
ABC=456

Anybody can help me this.

Thanks in advance.
# 2  
Old 06-20-2013
For example

Code:
sed 's/ABC=.*/ABC=456/' inputfile

# 3  
Old 06-20-2013
Code:
sed 's/\(ABC=\).*/\1456/g' file

ABC=456
CDE=122
DEF=456
ABC=456
DED=333
ABC=456

This User Gave Thanks to in2nix4life For This Post:
# 4  
Old 06-20-2013
Quote:
Originally Posted by in2nix4life
Code:
sed 's/\(ABC=\).*/\1456/g' file

ABC=456
CDE=122
DEF=456
ABC=456
DED=333
ABC=456

This code works. Thanks
Smilie
# 5  
Old 06-20-2013
Be aware that since none of the solutions anchor their regular expression, if your real data can contain keys/names that are substrings of others, they might modify more lines than intended.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 6  
Old 06-20-2013
Using Perl one-liner
Code:
%perl -pe "s/ABC=\d{3}/ABC=456/g" file

Code:
ABC=456
CDE=122
DEF=456
ABC=456
DED=333
ABC=456

This User Gave Thanks to Fundix For This Post:
# 7  
Old 06-20-2013
Thanks for you guys reply my question. all methods are work.
I have one more question. I need get value from another file and replace the current file.
I tried
Code:
sed 's/\(ABC=\).*/\`cat oldfile`/g' file

it didn't work.
How can I fix this?

Thanks in advance.
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

Partial Match and Replace

Hi, I have a tab delimited text file like this one. I need to do a partial match of a particular cell and then replace matches with an empty cell. So here is a sample: Smith FordMustang ChevroletCamaro Miller FordFiesta Jones KiaSorrento Davis ChevroletCamaro Johnson ToyotaHighlander I... (4 Replies)
Discussion started by: mikey11415
4 Replies

4. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

5. Shell Programming and Scripting

How to replace partial string

I have a list of strings in file: 10 10 AAA 120 13 BBBBB 23 11 CCCCC 11 32 DDDDDD I want to replace first column of the text such as: 10, 129, 23, 11 with 11, 22, 33, 44. I can do line by line, but just not sure how to replace partial string without... (1 Reply)
Discussion started by: ford99
1 Replies

6. 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

7. Shell Programming and Scripting

How to use sed to replace space partial

source "PUT 810 712 0001 ILC AK4 00 0 00 00" It needs to be changed to "PUT,810,712,0001,ILC,AK4 00 0 00 00" Thanks in advance. (6 Replies)
Discussion started by: janex
6 Replies

8. Shell Programming and Scripting

perl get partial string of a string

Hi All, I have: $d = "12.02222222222"; $d =~ s/(.*).(.*)/$1/e; The output should be just 12. Please guide me my expression wrong. Thanks (5 Replies)
Discussion started by: jimmy_y
5 Replies

9. Shell Programming and Scripting

get partial numbers from a string

Hi Everyone, I have: $val="QQ3_1899_CD4". The output will be: 1899. I did $val =~ /(\d+)/g; the output is 318994, then i use substr to get those 1899. This is not efficient. Is any simple way, like just one line can do? Thanks (1 Reply)
Discussion started by: jimmy_y
1 Replies

10. UNIX for Advanced & Expert Users

delete line from file if successful partial string found

Id like to delete a line from a file using (preferably a single line unix command) if it contains a certain string pattern. If line contains "abcdef" then delete that line. Help greatly appreciated. (7 Replies)
Discussion started by: cronjob78
7 Replies
Login or Register to Ask a Question

Featured Tech Videos