LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-29-2010, 01:50 AM   #1
bbachu
LQ Newbie
 
Registered: Dec 2010
Posts: 5

Rep: Reputation: 0
Need help in replacing set of characters in a specific line using sed or awk


I would like to replace 'xxxx' with 'yyyy' which is in a file xyz.csproj
not sure of what 'xxxx' is, it can be 3055, 4056, 7089 etc. I know it always appears at line # 5 and at character 50.

<Reference Include="System.Management" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Utilities, Version=4.4.0.xxxx, Culture=neutral, PublicKeyToken=c944cec9fb0a3d4a, processorArchitecture=x86" />

Any help is highly appreciated.
 
Old 12-29-2010, 02:46 AM   #2
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Distribution: Mageia Studio-13.37 Kubuntu.
Posts: 3,325
Blog Entries: 33

Rep: Reputation: 199Reputation: 199
Quote:
Hi, Welcome to LQ!

LQ has a fantastic search function that may save you time waiting for an answer to a popular question.

With over 4 million posts to search it's possible the answer has been given.
That looks similar to a tute i read lately,
IBM developer works, here's a link to section 4. (The file is available for download as a pdf file.)
http://www.ibm.com/developerworks/ai.../section4.html
Quote:
Editing files with sed

sed is short for stream editor. The traditional, modern-day definition of a text editor is an interactive application that can be used to create and edit text files. sed is also a text editor, but it is a command-line utility instead of an interactive utility, which makes it an extremely powerful tool for batch editing. sed is commonly used in UNIX shell scripts to filter large sets of text files. In the first part of the tutorial, you used a small test file that discussed golf. To demonstrate the advanced capability of the sed editor, you'll use a small snippet of code that a developer might want to change in a batch process.

Copy and paste this text into a file named sed.txt:

system "echo 'project:$project' >> logfile";
system "echo 'version:$version' >> logfile";
system "echo 'optionalid:$optionalid' >> logfile";
system "echo 'nodes:$nodes' >> logfile";
system "echo 'threads:$threads' >> logfile";
see also, "Using grep, sed, and awk "
more in this series.
http://www.ibm.com/developerworks/vi...for+a+new+user


related stuff,
http://www.regular-expressions.info/tutorial.html
http://www.ibm.com/developerworks/li...ry/l-sed1.html (1 of 3)
http://www.linuxtopia.org/online_boo...de/sedawk.html

Cheers, Glenn
 
Old 12-29-2010, 02:55 AM   #3
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291
Quote:
Originally Posted by bbachu View Post
I would like to replace 'xxxx' with 'yyyy' which is in a file xyz.csproj
not sure of what 'xxxx' is, it can be 3055, 4056, 7089 etc. I know it always appears at line # 5 and at character 50.

<Reference Include="System.Management" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Utilities, Version=4.4.0.xxxx, Culture=neutral, PublicKeyToken=c944cec9fb0a3d4a, processorArchitecture=x86" />

Any help is highly appreciated.
Hello,

This one should do the trick:
Code:
sed -e '<linenumber>s/^\(.\{XX\}\).\(.\{4\}\)/\1<8888>/' <filename>
where XX is the position after which the change should take place and 4 is the number of characters to change. Taking your line <Reference Include="Utilities, Version=4.4.0.xxxx, Culture (say linenumber 4 in file named test, the following would replace xxxx with 8888.
Code:
sed -e '4s/^\(.\{45\}\).\(.\{4\}\)/\18888/' test
Hope that helps.

Kind regards,

Eric

Last edited by EricTRA; 12-29-2010 at 02:57 AM. Reason: Clarified a bit
 
1 members found this post helpful.
Old 12-29-2010, 05:34 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Instead of line 5 and char 50 ( I assume there are supposed to be 4 spaces at the beginning) would not always be the Version you are looking to edit?
This would be a much simpler substitution. (no offense to Eric's correctly working solution of course)
 
1 members found this post helpful.
Old 12-29-2010, 05:40 AM   #5
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291
Quote:
Originally Posted by grail View Post
Instead of line 5 and char 50 ( I assume there are supposed to be 4 spaces at the beginning) would not always be the Version you are looking to edit?
This would be a much simpler substitution. (no offense to Eric's correctly working solution of course)
Hi grail,

That would be a far easier solution of course. As you mentioned spaces at the beginning of the line (due to the formatting inside the file), I didn't take that in account. I just copied the line posted and took it from there. That's what happens when a user doesn't use CODE tags

Kind regards,

Eric
 
Old 12-30-2010, 01:09 AM   #6
bbachu
LQ Newbie
 
Registered: Dec 2010
Posts: 5

Original Poster
Rep: Reputation: 0
As grail suggests, can you help me with the script to replace n characters after searching for a string in a file.
eg: <Reference Include="Utilities, Version=4.4.0.xxxx, Culture...>
I search for "Version=4.4.0." and replace 4 characters from there 'xxxx' with 'yyyy'
 
Old 12-30-2010, 02:20 AM   #7
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291
Quote:
Originally Posted by bbachu View Post
As grail suggests, can you help me with the script to replace n characters after searching for a string in a file.
eg: <Reference Include="Utilities, Version=4.4.0.xxxx, Culture...>
I search for "Version=4.4.0." and replace 4 characters from there 'xxxx' with 'yyyy'
Hello,

That would be easier since you don't have to count any characters to get to the position.
Code:
sed -e 's/\(Version=4.4.0.\)\(.\{4\}\)/\18888/' <filename>
The above command will search for the string Version=4.4.0. (the first part between parenthesis) and remember it, then count any four characters from there and substitute those four characters by 8888. The '\1' is the search pattern found and remembered. This command will substitute ANY instance found in the document, so if you have more than one instance ALL will be processed (notice I deleted the linenumber in front of s/? If you are sure about the linenumber in any document you can put it back again).

Have a look at this great sed manual from Grymoire. You can learn a lot from it about how to do 'magic' with sed.

Kind regards,

Eric

Last edited by EricTRA; 12-30-2010 at 02:21 AM.
 
1 members found this post helpful.
Old 12-31-2010, 06:10 AM   #8
bbachu
LQ Newbie
 
Registered: Dec 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for your help Grail and Eric.
 
Old 12-31-2010, 06:12 AM   #9
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291
Hello,

You're welcome, that's what LQ is all about. Helping each other out. If you consider your problem solved then please mark it as such using the Thread Tools.

Kind regards,

Eric
 
1 members found this post helpful.
Old 12-31-2010, 08:37 AM   #10
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
EricTRA,

You may know of these already, you may even have decided against using them; but, if they work in the shell you are using, the following could make your sed scripts easier to read:
  • "-e" is often unnecessary
  • "-r" eliminates many instances of '\'
  • "s,,," works as well as "s///"
So your very nice,
Code:
sed -e 's/\(Version=4.4.0.\)\(.\{4\}\)/\18888/' <filename>
would become:
Code:
sed -r 's,(Version=4.4.0.)(.{4}),\18888,' <filename>

If you didn't guess, I hate "kung fu" regexes, the ones that look like David Carradine & Chuck Norris sparring:
sed 's/\/\/\/\//\/\//' vs sed 's,////,//,'

Last edited by archtoad6; 12-31-2010 at 08:38 AM.
 
1 members found this post helpful.
Old 12-31-2010, 11:54 AM   #11
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291
Quote:
Originally Posted by archtoad6 View Post
EricTRA,

You may know of these already, you may even have decided against using them; but, if they work in the shell you are using, the following could make your sed scripts easier to read:
  • "-e" is often unnecessary
  • "-r" eliminates many instances of '\'
  • "s,,," works as well as "s///"
So your very nice,
Code:
sed -e 's/\(Version=4.4.0.\)\(.\{4\}\)/\18888/' <filename>
would become:
Code:
sed -r 's,(Version=4.4.0.)(.{4}),\18888,' <filename>

If you didn't guess, I hate "kung fu" regexes, the ones that look like David Carradine & Chuck Norris sparring:
sed 's/\/\/\/\//\/\//' vs sed 's,////,//,'
Hello archtoad6,

Thank you for pointing those out. As it happens I'm as most Linux users learning more and more every day. So much to learn and so little time... The -r option does work in my shell(s) as it seems but I didn't know that one yet, still in the process of learning. Again, thank you for the clarification and making my life easier, since you're saving me time I can dedicate more to learning

Kind regards,

Eric

PS: Unlike you I LOVE martial arts... but that wasn't the reason for using \/ structures.

Last edited by EricTRA; 12-31-2010 at 11:55 AM.
 
Old 01-01-2011, 02:09 AM   #12
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Here is another variation on theme, assuming Version only appears the once:
Code:
sed '/Version/s/\.[^.]*,/.3456,/' file
As usual you could also throw awk at it but then you would need to rename a temp file as well??
Code:
awk '/Version/{sub(/[^.]*,/,"3456,",$3)}1' file
 
Old 01-01-2011, 11:25 AM   #13
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
Edit: I folded the long lines in my "Code:" blocks -- they were driving my browser crazy. The folds are indicated by the trailing backslashes. Sorry if this makes the post seem less useful.


Here I set up a test string, check it, & pipe it through grail's sed line above:
Code:
$ X='<Reference Include="Utilities, Version=4.4.0.xxxx, Culture=neutral, PublicKeyToken=c944cec9fb0a3d4a, \
processorArchitecture=x86" />'
$ echo $X
<Reference Include="Utilities, Version=4.4.0.xxxx, Culture=neutral, PublicKeyToken=c944cec9fb0a3d4a, \
processorArchitecture=x86" />

$ echo $X  | sed '/Version/s#\.[^.]*,#.3456,#'
<Reference Include="Utilities, Version=4.4.0.3456, processorArchitecture=x86" />
It doesn't work because regexes are greedy & the * extends not to the nearest ',', but to the farthest one w/o encountering a '.'

However, adding a ',' will fix the problem:
Code:
$ echo $X  | sed '/Version/s![.][^.,]*,!.3456,!'
<Reference Include="Utilities, Version=4.4.0.3456, Culture=neutral, PublicKeyToken=c944cec9fb0a3d4a, \
processorArchitecture=x86" />
Notes
  • sed doesn't need "-r" here.
  • Since the search pattern contains ',', I have showcased '#' & '!' as alternate delimiters.
  • I also demonstrated '[.]' as an alternative to '\.'.


"Code:" blocks
BTW, bbachu, if you want to learn about "Code:" blocks as suggested above, they are explained here.

Last edited by archtoad6; 01-01-2011 at 11:55 AM. Reason: fold long lines in "Code:" blocks
 
1 members found this post helpful.
Old 01-01-2011, 12:07 PM   #14
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Sorry that was my bad ... I had the other comma in but must have copied the wrong test case back to here Nice pick up archtoad6
 
Old 01-03-2011, 12:44 AM   #15
bbachu
LQ Newbie
 
Registered: Dec 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks to all for your replies. My problem is solved now.
I'm delighted with the responses. Thanks again to all.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script to parse a file to get a set of line between a specific characters venkatrg Linux - Newbie 5 12-24-2010 07:55 AM
[SOLVED] SED and Replacing Specific Line bridrod Linux - Newbie 6 08-24-2009 01:28 PM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 10:00 AM


All times are GMT -5. The time now is 01:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration