Sponsored Content
Top Forums Shell Programming and Scripting Sed/grep: check if line exists, if not add line? Post 303013311 by MadeInGermany on Monday 19th of February 2018 12:36:39 PM
Old 02-19-2018
I think
grep -Fxq "$searchString"
is true if $searchString is found.
How can the sed run if $searchString is not found?

Last edited by MadeInGermany; 02-19-2018 at 02:40 PM.. Reason: typo
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

grep a string in a line using sed

Hi, I'm trying to grep a string in a line using sed. My original data looks like this: MRTG$ grep -i "System" $file <H1>Traffic Analysis for 15 -- sERITHC3602.t-mobile.co.uk</H1> <TABLE> <TR><TD>System:</TD> <TD>sERITHC3602 in </TD></TR> <TR><TD>Maintainer:</TD> <TD></TD></TR>... (4 Replies)
Discussion started by: viadisky
4 Replies

2. Shell Programming and Scripting

SED help (remove line::parse again::add line)

Aloha! I have just over 1k of users that have permissions that they shouldn't under our system. I need to parse a provided list of usernames, check their permissions file, and strip the permissions that they are not allowed to have. If upon the permissions strip they are left with no permissions,... (6 Replies)
Discussion started by: Malumake
6 Replies

3. UNIX for Dummies Questions & Answers

Need to serach if a new line character exists on the last line in a file

I have a file in which I need to search if a new line character exists on the last line in the file. Please let me know how can I achieve it using Unix commands? (10 Replies)
Discussion started by: sunilbm78
10 Replies

4. Shell Programming and Scripting

how to get line number of different words if exists in same line

I have some txt files. I have to create another text file which contains the portion starting from the format "Date Sex Address" to the end of the file. While using grep -n on Date it also gives me the previous line containg Date. and also Date may be DATE in some files. My file is like this... (10 Replies)
Discussion started by: Amiya Rath
10 Replies

5. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

6. Shell Programming and Scripting

GREP/SED - get string in a line

Hi, I simply try to get a string in a line but I do smth. wrong. Hopfefully you can help me ;) tried smth like: ggrep -Eo " /folder1/folder2/folder3/* end" get_info_file > temp.file I played a bit around but could not specify the end string command... So this is the... (9 Replies)
Discussion started by: unknown7
9 Replies

7. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

8. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

9. Shell Programming and Scripting

awk or sed or grep filter a line and/or between strings

Hi, I have multiple files on a directory with the following content: blahblah blahblah hostname server1 blahblah blahblah ---BEGIN--- aaa bbb ccc ddd ---END--- blahblah blahblah blahblah I would like to filter all the files with awk or sed or something else so I can get below... (6 Replies)
Discussion started by: bayupw
6 Replies

10. Shell Programming and Scripting

sed or awk grep, that will only get the line with more characters.

Is there a command for sed and awk that will only sort the line with more characters? #cat file 123 12345 12 asdgjljhhho bac ss Output: asdgjljhhho #cat file2 11.2 12345.00 21.222 12345678.10 (2 Replies)
Discussion started by: invinzin21
2 Replies
Net::LDAP::Examples(3)					User Contributed Perl Documentation				    Net::LDAP::Examples(3)

NAME
Net::LDAP::Examples - PERL LDAP by Example DESCRIPTION
The following examples are of course PERL code, found to work with the Net::LDAP modules. The intent of this document is to give the reader a cut and paste jump start to getting an LDAP application working. Below you will find snippets of code that should work as-is with only a small amount of work to correct any variable assignments and LDAP specifics, e.g. Distinguished Name Syntax, related to the user's own implementation. The Standard Operating Proceedure that is followed here is: 1 Package - use Net::LDAP 2 Initialization - new 3 Binding - bind 4 Operation - add modify moddn search 4.1 Processing - displaying data from a search 5 Error - displaying error information 6 Unbinding - unbind Look to each of these for a snippet of code to meet your needs. What is not covered in these examples at this time: abandon and compare methods callback subroutines CODE
PACKAGE - Definitions use Net::LDAP; INITIALIZING $ldap = Net::LDAP->new ( "yourLDAPhost.yourCompany.com" ) or die "$@"; BINDING $mesg = $ldap->bind ( version => 3 ); # use for searches $mesg = $ldap->bind ( "$userToAuthenticate", password => "$passwd", version => 3 ); # use for changes/edits # see your LDAP administrator for information concerning the # user authentication setup at your site. OPERATION - Generating a SEARCH sub LDAPsearch { my ($ldap,$searchString,$attrs,$base) = @_; # if they don't pass a base... set it for them if (!$base ) { $base = "o=mycompany, c=mycountry"; } # if they don't pass an array of attributes... # set up something for them if (!$attrs ) { $attrs = [ 'cn','mail' ]; } my $result = $ldap->search ( base => "$base", scope => "sub", filter => "$searchString", attrs => $attrs ); } my @Attrs = ( ); # request all available attributes # to be returned. my $result = LDAPsearch ( $ldap, "sn=*", @Attrs ); PROCESSING - Displaying SEARCH Results #------------ # # Accessing the data as if in a structure # i.e. Using the "as_struct" method # my $href = $result->as_struct; # get an array of the DN names my @arrayOfDNs = keys %$href; # use DN hashes # process each DN using it as a key foreach ( @arrayOfDNs ) { print $_, " "; my $valref = $$href{$_}; # get an array of the attribute names # passed for this one DN. my @arrayOfAttrs = sort keys %$valref; #use Attr hashes my $attrName; foreach $attrName (@arrayOfAttrs) { # skip any binary data: yuck! next if ( $attrName =~ /;binary$/ ); # get the attribute value (pointer) using the # attribute name as the hash my $attrVal = @$valref{$attrName}; print " $attrName: @$attrVal "; } print "#------------------------------- "; # End of that DN } # # end of as_struct method # #-------- #------------ # # handle each of the results independently # ... i.e. using the walk through method # my @entries = $result->entries; my $entr; foreach $entr ( @entries ) { print "DN: ", $entr->dn, " "; my $attr; foreach $attr ( sort $entr->attributes ) { # skip binary we can't handle next if ( $attr =~ /;binary$/ ); print " $attr : ", $entr->get_value ( $attr ) ," "; } print "#------------------------------- "; } # # end of walk through method #------------ OPERATION - Modifying entries # # Modify # # for each of the modifies below you'll need to supply # a full DN (Distinguished Name) for the $dn variable. # example: # cn=Jo User,ou=person,o=mycompany,c=mycountry # # I would recommend doing a search (listed above) # then use the dn returned to populate the $dn variable. # # Do we only have one result returned from the search? if ( $result->count != 1 ) { exit; } # Nope.. exit my $dn = $entries[0]->dn; # yes.. get the DN ####################################### # # MODIFY using a HASH # my %ReplaceHash = ( keyword => "x", proxy => "x" ); my $result = LDAPmodifyUsingHash ( $ldap, $dn, \%ReplaceHash ); sub LDAPmodifyUsingHash { my ($ldap, $dn, $whatToChange ) = @_; my $result = $ldap->modify ( $dn, replace => { %$whatToChange } ); return $result; } ####################################### # # MODIFY using a ARRAY List # my @ReplaceArrayList = [ 'keyword', "xxxxxxxxxx", 'proxy' , "yyyyyyyyyy" ]; my $result = LDAPmodifyUsingArrayList ( $ldap, $dn, @ReplaceArrayList ); sub LDAPmodifyUsingArrayList { my ($ldap, $dn, $whatToChange ) = @_; my $result = $ldap->modify ( $dn, changes => [ replace => @$whatToChange ] ); return $result; } ####################################### # # MODIFY using a ARRAY # my @ReplaceArray = ( 'keyword', "xxxxxxxxxx" , 'proxy' , "yyyyyyyyyy" ); my $result = LDAPmodifyUsingArray ( $ldap, $dn, @ReplaceArray ); sub LDAPmodifyUsingArray { my ($ldap, $dn, $whatToChange ) = @_; my $result = $ldap->modify ( $dn, changes => [ replace => [ @$whatToChange ] ] ); return $result; } ####################################### # # MODIFY an existing record using 'Changes' # (or combination of add/delete/replace) # my @whatToChange; my @ReplaceArray; my @DeleteArray; my @AddArray; push @AddArray, 'cn', "me myself"; push @ReplaceArray, 'sn', '!@#$%^&*()__+Hello THere'; push @ReplaceArray, 'cn', "me myself I"; push @DeleteArray, 'cn', "me myself"; if ( $#ReplaceArray > 0 ) { push @whatToChange, 'replace'; push @whatToChange, @ReplaceArray; } if ( $#DeleteArray > 0 ) { push @whatToChange, 'delete'; push @whatToChange, @DeleteArray; } if ( $#AddArray > 0 ) { push @whatToChange, 'add'; push @whatToChange, @AddArray; } $result = LDAPmodify ( $ldap, $dn, @whatToChange ); sub LDAPmodify { my ($ldap, $dn, $whatToChange) = @_; my $result = $ldap->modify ( $dn, changes => [ @$whatToChange ] ); return $result; } OPERATION - Changing the RDN my $newRDN = "cn=Joseph User"; my $result = LDAPrdnChange ( $ldap, $dn, $newRDN, "archive" ); sub LDAPrdnChange { my ($ldap,$dn,$whatToChange,$action) = @_; my $branch; # # if the archive action is selected, move this # entry to another place in the directory. # if ( $action =~ /archive/i ) { $branch = "ou=newbranch, o=mycompany, c=mycountry"; } # # use the 'deleteoldrdn' to keep from getting # multivalues in the NAMING attribute. # in most cases that would be the 'CN' attribute # my $result = $ldap->moddn ( $dn, newrdn => $whatToChange, deleteoldrdn => '1', newsuperior => $branch ); return $result; } OPERATION - Adding a new Record my $DNbranch = "ou=bailiwick, o=mycompany, c=mycountry"; # # check with your Directory Schema or Administrator # for the correct objectClass... I'm sure it'll be different # my $CreateArray = [ objectClass => [ "top", "person", "organizationalPerson", "inetOrgPerson" ], cn => "Jane User", uid => "0000001", sn => "User", mail => "JaneUser@mycompany.com" ]; # # create the new DN to look like this # " cn=Jo User + uid=0000001 , ou=bailiwick, o=mycompany, c=mycountry " # # NOTE: this DN MUST be changed to meet your implementation # my $NewDN = "@$CreateArray[2]=". "@$CreateArray[3]+". "@$CreateArray[4]=". "@$CreateArray[5],". $DNbranch; LDAPentryCreate($ldap, $NewDN, $CreateArray); # # CreateArray is a reference to an anonymous array # you have to dereference it in the subroutine it's # passed to. # sub LDAPentryCreate { my ($ldap, $dn, $whatToCreate) = @_; my $result = $ldap->add ( $dn, attrs => [ @$whatToCreate ] ); return $result; } ERROR - Retrieving and Displaying ERROR information if ( $result->code ) { # # if we've got an error... record it # LDAPerror ( "Searching", $result ); } sub LDAPerror { my ($from, $mesg) = @_; print "Return code: ", $mesg->code; print " Message: ", $mesg->error_name; print " :", $mesg->error_text; print "MessageID: ", $mesg->mesg_id; print " DN: ", $mesg->dn; #--- # Programmer note: # # "$mesg->error" DOESN'T work!!! # #print " Message: ", $mesg->error; #----- } UNBIND $ldap->unbind; LDAP SCHEMA RETRIEVAL
The following code snippet shows how to retrieve schema information. The first procedure is to initialize a new LDAP object using the same procedures as listed at the beginning of this document. The second procedure is to bind to your directory server. Some servers may require authentication to retrieve the schema from the directory server. This procedure is listed at the beginning of this document too. After a successful bind you are ready to retrieve the schema information. You do this by initializing a schema object. $schema = $ldap->schema ( ); In this case Net::LDAP will attempt to determine the dn under which the schema can be found. First it will look for the attribute "subschemasubentry" in the root DSE. If that cannot be found then it will default to the assumption of "cn=schema" Alternatively you can specify the dn where the schema is to be found with $schema = $ldap->schema ( dn => $dn ); Once we have a dn to search for, Net::LDAP will fetch the schema entry with $mesg = $self->search ( base => $dn, scope => 'base', filter => '(objectClass=subschema)', ); Once the schema object has been initialized, schema methods are used to retrieve the data. There are a number of ways this can be done. Information on the schema methods can be found in the Net::LDAP::Schema pod documentation. The following is a code snippet showing how to get and display information about returned attributes. # # Get the attributes # @attributes = $schema->all_attributes ( ); # # Display the attributes # foreach $ar ( @attributes ) { print "attributeType: ", $ar->{name}, " "; # # Print all the details # foreach $key ( keys %{$ar} ) { print join ( " ", " $key:", ref ( $ar->{$key} ) ? @{$ar->{$key}} : $ar->{$key} ), " "; } } The process is the basically the same for getting objectClass information. Where schema->all_attributes() is used, substitute schema->all_objectclasses(). From that point on the process is the same for both objectClasses and attributes. BUGS
None known, but there may be some AUTHOR
(of this document) Russell Biggs <rgb@ticnet.com> COPYRIGHT
All rights to this document are hereby relinquished to Graham Barr. perl v5.16.3 2013-06-07 Net::LDAP::Examples(3)
All times are GMT -4. The time now is 08:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy