LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 08-13-2010, 03:49 PM   #1
gdavis2287
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Rep: Reputation: 0
Find and replace multiple instances


I have a file with the following in it:

:0 local /usr/X11R6/bin/X -nobanner
:1 local /usr/X11R6/bin/X

I need to add "-nolisten tcp" to both of these lines and cannot figure out how to do it. I can get it to do the first one, but not the 2nd and so on.

How do I search the file and loop though it to change them all?

TIA
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 08-13-2010, 04:04 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Would this help:

sed 's%\(:[0-9][0-9]* local /usr/X11R6/bin/X.*\)%\1 -nolisten tcp%' infile

Assuming that the 0 and 1 is the only none unique part (and the -nobanner part). The above accounts for this.

Add the -i switch to replace in place.

Hope this helps.

Last edited by druuna; 08-13-2010 at 04:07 PM. Reason: Added the -nobanner part.
 
1 members found this post helpful.
Old 08-13-2010, 09:05 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '/^:[01]/{$0=$0" -nolisten tcp"}1' file >temp &&mv -f temp file
Code:
sed -i.bak '/^:[01]/s/$/ -nolisten tcp/' file
Code:
#!/bin/bash
while read -r line
do
  case "$line" in
   ":"[01]* ) line="$line -nolisten tcp"
  esac
  echo "$line"
done <"file" > t
mv t file
if you want to search for other numbers besides 0 and 1, use [0-9]

Last edited by ghostdog74; 08-14-2010 at 06:54 AM.
 
Old 08-13-2010, 11:46 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
sed -i 's/:[01].*/& -nolisten tcp/' file
 
Old 08-14-2010, 12:28 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Code:
sed -i '/^:[01] /s/.*/& -nolisten tcp/' file
or more specific:
Code:
sed -i '/^:[01] local \/usr\/X11R6\/bin\/X/s/.*/& -nolisten tcp/' file
in any way you should test it on a different file first
 
Old 08-14-2010, 04:43 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@ghostdog74 / grail / konsolebox: Did you miss the and so on part Your examples only work for 0 and 1 not for any other, higher, numbers.

ghostdog74: replcement part isn't -nobanner but -nolisten tcp

Must have been early (or late, depending)
 
Old 08-14-2010, 04:48 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
@druuna He was not specific on the and so on thing. It could be 1 and so on matches. Not 0 and 1 and so on.
 
Old 08-14-2010, 05:07 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@konsolebox: I'm sure s/he was specific:
Quote:
I can get it to do the first one, but not the 2nd and so on.
Translates to: I can do the 0, but not the 1 and 2 and 3 and ........

One thing that isn't clear from the first post: Are there any other lines in that file that should not be changed, but do start with :<number>??

If so then most of the other answers will fail as well.

Last edited by druuna; 08-14-2010 at 05:10 AM.
 
Old 08-14-2010, 06:07 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
I can get it to do the first one, but not the 2nd and so on.
@druuna There's only one part of what he said that could refer to more matches from 0 to 1 and 2 and so on but that part still could also refer to just more matches of the two lines that he said. Putting that question aside there are two more statements that can tell that he was just referring to the two lines:
Quote:
Originally Posted by gdavis2287 View Post
I have a file with the following in it:
Quote:
Originally Posted by gdavis2287 View Post
I need to add "-nolisten tcp" to both of these lines
Referring to those statements which do you think has the higher probability, does he refer to 0..inf or just matches of the two lines?
 
Old 08-14-2010, 06:23 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

@konsolebox: I'm not trying to make assumptions when it comes to questions asked, but most people tend to give small examples instead of full relevant examples, so sometimes you do need to make an assumption.

The fact that the example only holds 2 lines and that the OP talks about "... the first one, but not the 2nd and so on" contradict each other. The only safe assumption I can make at this point is that there are probably more then the 2 lines shown in the file that needs to be changed and that there could also be other, none-relevant, lines present in that file.

For me the higher probability is that the OP posted a small part of the file(s) s/he needs to change and my answer takes that into account.

Lets wait until the OP comes on line again and see what s/he really wanted/needed.
 
Old 08-14-2010, 11:16 PM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I agree that we can to and fro about this forever. Would the OP please reply and let us out of our misery

I think we need to know:

1. Does the 'and so on' refer to more combinations like ':N' where N is any number?

2. Are there other lines in the file which should not be changed? If so, what do they look like?

If anyone can think of other questions for the OP please add
 
Old 08-16-2010, 07:48 AM   #12
gdavis2287
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
I should have said this is the Xservers file and that might have helped. In this file you can have any number of lines that start X on any given terminal. What is really boils down to is I want to add "-nolisten tcp" to any line that has /usr/X11R6/bin/X I guess. That should address the "and so on"...:N could be 0, 1, 2, 3, 4...

All lines should be changed that have /usr/X11R6/bin/X in it, so I would prefer to just ignore the :N portion of the file if possible because who knows what :N will be...most of the time there will only be one entry in this file, but we all know it's possible to have more.

I should also add if the line already has "-nolisten tcp" I don't want to append it again

So if the file looks like this below, I only need to add it to the one that does not have it.

:0 local /usr/X11R6/bin/X -nolisten tcp
:1 local /usr/X11R6/bin/X


I'm working on testing the solutions people have put in and will update this later today...

Thanks for all the replies.

Last edited by gdavis2287; 08-16-2010 at 08:00 AM.
 
Old 08-16-2010, 07:54 AM   #13
gdavis2287
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
Hi,

Would this help:

sed 's%\(:[0-9][0-9]* local /usr/X11R6/bin/X.*\)%\1 -nolisten tcp%' infile

Assuming that the 0 and 1 is the only none unique part (and the -nobanner part). The above accounts for this.

Add the -i switch to replace in place.

Hope this helps.
If I do this and if the line already has "-nolisten tcp" it adds it again....so this did not seem to work for me.

Last edited by gdavis2287; 08-16-2010 at 07:56 AM.
 
Old 08-16-2010, 08:12 AM   #14
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

This will fix the possible double -nolisten tcp issue:

sed '/-nolisten tcp/!{s%\(:[0-9][0-9]* local /usr/X11R6/bin/X.*\)%\1 -nolisten tcp%}' infile

Example run:
Code:
$ cat infile
:0 local /usr/X11R6/bin/X -nobanner
:1 local /usr/X11R6/bin/X
:10 local /usr/X11R6/bin/X -nolisten tcp
:11 local /usr/X11R6/bin/X -nobanner -nolisten tcp
:100 local /usr/X11R6/bin/X -nolisten tcp -nobanner
:1000 local /usr/X11R6/bin/X

$ sed '/-nolisten tcp/!{s%\(:[0-9][0-9]* local /usr/X11R6/bin/X.*\)%\1 -nolisten tcp%}' infile
:0 local /usr/X11R6/bin/X -nobanner -nolisten tcp
:1 local /usr/X11R6/bin/X -nolisten tcp
:10 local /usr/X11R6/bin/X -nolisten tcp
:11 local /usr/X11R6/bin/X -nobanner -nolisten tcp
:100 local /usr/X11R6/bin/X -nolisten tcp -nobanner
:1000 local /usr/X11R6/bin/X -nolisten tcp
Hope this helps.

Last edited by druuna; 08-16-2010 at 08:49 AM. Reason: fixed typo
 
1 members found this post helpful.
Old 08-16-2010, 08:13 AM   #15
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
If that's the case, druuna was right. Here's my solution for that.

Code:
sed '/:[0-9]\+ local \/usr\/X11R6\/bin\/X/{ / -nolisten tcp/!s/.*/& -nolisten tcp/; }' file
 
  


Reply



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] Find and replace in multiple files in ssh whitelinux Linux - Server 1 07-03-2010 04:43 PM
Find & Replace string in multiple files Rudy Linux - General 14 04-15-2010 08:10 AM
Sed / Replace multiline, multiple instances jkmaster Programming 8 01-28-2010 09:00 AM
Find and replace text in multiple file Bad_Bob Linux - Software 9 05-08-2008 02:31 AM
file renaming question--replace multiple instances David the H. Linux - General 4 01-01-2008 12:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:10 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
Open Source Consulting | Domain Registration