LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-16-2010, 08:16 AM   #16
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193

I would probably go with awk for this then:
Code:
 awk '"/usr/X11R6/bin/X" && !/-nolisten tcp/{$0=$0" -nolisten tcp"}1' file
 
1 members found this post helpful.
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 08-16-2010, 08:32 AM   #17
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
Quote:
Originally Posted by grail View Post
I would probably go with awk for this then:
Code:
 awk '"/usr/X11R6/bin/X" && !/-nolisten tcp/{$0=$0" -nolisten tcp"}1' file
Nice, short awk solution!
 
Old 08-16-2010, 09:16 AM   #18
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by grail View Post
I would probably go with awk for this then:
Code:
 awk '"/usr/X11R6/bin/X" && !/-nolisten tcp/{$0=$0" -nolisten tcp"}1' file
Can I ask what's the 1 for? I'm sort of tired to search manuals.
 
Old 08-16-2010, 09:21 AM   #19
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Quote:
Can I ask what's the 1 for? I'm sort of tired to search manuals.
Sure, always like when I can pass something on

The default action of any true statement is to print, hence 1 is true so print which also equals print $0
 
2 members found this post helpful.
Old 08-16-2010, 09:43 AM   #20
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
So 1 was actually not directly related to the leading expressions. I was mislead on that. Thanks.
 
Old 08-16-2010, 12:15 PM   #21
gdavis2287
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
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.

Works like a charm! One more question. If I wanted to say cat the file and report that there was an entry that was missing the "-nolisten tcp" would a "while read" be best for that?
 
Old 08-16-2010, 12:31 PM   #22
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
You can try this.
Code:
grep ':[0-9]\+ local /usr/X11R6/bin/X' | grep -v -F '-nolisten tcp' >/dev/null && echo 'an entry is missing "-nolisten tcp\"
I wonder when 'while read' started to get popular.. Everything was 'for VAR in ...' back then.
 
Old 08-16-2010, 12:32 PM   #23
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,

You want this?

sed -n '/-nolisten tcp/!p' infile
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 -n '/-nolisten tcp/!p' infile 
:0 local /usr/X11R6/bin/X -nobanner
:1 local /usr/X11R6/bin/X
:1000 local /usr/X11R6/bin/X
If the /usr/X11R6/bin/X also needs to be part of it:
sed -n '/\/usr\/X11R6\/bin\/X.*-nolisten tcp/!p' infile

Hope this helps.
 
Old 08-16-2010, 12:40 PM   #24
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
finally also an awk style

Code:
awk '/:[0-9]+ local \/usr\/X11R6\/bin\/X/ && !/-nolisten tcp/{ exit (1); }' file || echo 'an entry is missing \"-nolisten tcp\"'
edit: i guess we can put everything inside as one
Code:
awk '/:[0-9]+ local \/usr\/X11R6\/bin\/X/ && !/-nolisten tcp/{ print "an entry is missing -nolisten tcp."; exit; }'

Last edited by konsolebox; 08-16-2010 at 12:50 PM.
 
  


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 03:05 PM.

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