The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed pattern matching ravi_rn Shell Programming and Scripting 4 04-20-2009 07:24 AM
counting the lines matching a pattern, in between two pattern, and generate a tab d.chauliac Shell Programming and Scripting 4 03-19-2009 01:30 PM
search a pattern and if pattern found insert new pattern at the begining pitagi Shell Programming and Scripting 7 02-12-2009 10:27 PM
comment/delete a particular pattern starting from second line of the matching pattern imas Shell Programming and Scripting 4 10-13-2008 02:37 AM
pattern matching larryase UNIX for Dummies Questions & Answers 3 11-22-2004 06:54 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-07-2009
RobertBerrie RobertBerrie is offline
Registered User
  
 

Join Date: May 2009
Posts: 1
sed - matching pattern one but not pattern two

All,

I have the following file:
--------------------------------------
#
# /etc/pam.d/common-password - password-related modules common to all services
#
# This file is included from other service-specific PAM config files,
# and should contain a list of modules that define the services to be
# used to change user passwords. The default is pam_unix2 in combination
# with pam_pwcheck.
# The "nullok" option allows users to change an empty password, else
# empty passwords are treated as locked accounts.
#
# To enable Blowfish or MD5 passwords, you should edit
# /etc/default/passwd.
#
# Alternate strength checking for passwords should be configured
# in /etc/security/pam_pwcheck.conf.
#
# pam_make can be used to rebuild NIS maps after password change.
#
password required pam_passwdqc.so retry=5 ask_oldauthok min=disabled,8,8,8,8 passphrase=0 random=0 force=everyone
password required pam_pwcheck.so nullok
password required pam_unix2.so nullok use_authtok
#password required pam_make.so /var/yp
--------------------------------------------------------------------

What I would like to do is generate a sed command that will:
Add "use_first_pass" to a line if the line contains "pam_unix2.so" but the line does *not* contain "use_first_pass".
The remainer of the file should be left alone.

I can generate the logic with:
a regex of:
(.*pam_unix2\.so.*) (?!.*use_first_pass.*)

I am having trouble translating that into a sed expression.

Thanks,
-Robert
  #2 (permalink)  
Old 05-07-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,307
If awk is allowed:

Code:
awk '/pam_unix2.so/ && !/use_first_pass/{$0=$0 FS "use_first_pass"}1' file
  #3 (permalink)  
Old 05-07-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,533
if you can use Python
Code:
for line in open("file"):
    line=line.strip()
    if "pam_unix2.so" in line and not "use_first_pass" in line:
        print "use_first_pass "  + line
    else:
        print line
or inplace version
Code:
import fileinput
for line in fileinput.FileInput("file",inplace=1):
    line=line.strip()
    if "pam_unix2.so" in line and not "use_first_pass" in line:
        print "use_first_pass "  + line
    else:
        print line
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:33 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0