Wednesday, February 20, 2008
# more my_wc
#!/bin/sh
sed -n '$=' $1
# more my_head
#!/bin/sh
sed 10q $1
# more my_grep
#!/bin/sh
sed -n '/'$1'/p' $2
#more my_del_lines
#!/bin/sh
sed $1','$2'd' $3
#more my_tac
#!/bin/sh
sed -n '1!G;h;$p' $1
#more my_tail
#!/bin/sh
lines=`wc -l $2 awk '{print $1}' `
start=`expr $lines - $1`
sed "1,$start d" $2
#more line_insert
#!/bin/sh
sed '
/'$1'/ i\
Add this line before every line with WORD
' $2
Friday, January 11, 2008
----------------------------------------------------------------------------
1. Using expect package.
#!/usr/bin/expect
set newpassword [lindex $argv 1]
spawn /usr/bin/passwd [lindex $argv 0]
expect "New password:"
send "$newpassword\r"
expect "Re-enter new password:"
send "$newpassword\r"
expect eof
----------------------------------------------------------------------------
2. In case your operating system do not have expect package then you can do it using the perl script.
#!/usr/bin/perl -w
#######################################################################################
# Usage : $0
# Note : This script need to be run as root.
# Author : Rishi Pahuja
# Created : 01 Oct 2007
#######################################################################################
use strict;
my ($usr, $plain) = @ARGV;
my $itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
my $salt;
for (1..2) { $salt .= substr $itoa64, rand(length($itoa64)), 1; }
my $password = crypt($plain, $salt);
#print `usermod -p $password $usr`;
# Changing the password
system("usermod -p $password $usr");
if ( $? == 0 ) {
print "\nOS password changed successfully for user $usr to $plain.\n";
}
else {
print "ERROR: Changing password for user $usr.\n";
print " Return Code: $?\n";
print "Exiting with return code of 1.\n";
exit 1;
}
Wednesday, September 06, 2006
HANDY ONE-LINERS FOR SED
NUMBERING:
# number each line of a file (simple left alignment). Using a tab (see
# note on '\t' at end of file) instead of space will preserve margins.
sed = filename sed 'N;s/\n/\t/'
# count lines (emulates "wc -l")
sed -n '$='
TEXT CONVERSION AND SUBSTITUTION:
# delete leading whitespace (spaces, tabs) from front of each line
# aligns all text flush left
sed 's/^[ \t]*//'
# delete trailing whitespace (spaces, tabs) from end of each line
sed 's/[ \t]*$//'
# reverse order of lines (emulates "tac")
sed '1!G;h;$!d' # method 1
sed -n '1!G;h;$p' # method 2
# substitute (find and replace) "foo" with "bar" on each line
sed 's/foo/bar/' # replaces only 1st instance in a line
sed 's/foo/bar/4' # replaces only 4th instance in a line
sed 's/foo/bar/g' # replaces ALL instances in a line
sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # replace the next-to-last case
sed 's/\(.*\)foo/\1bar/' # replace only the last case
SELECTIVE PRINTING OF CERTAIN LINES:
# print first 10 lines of file (emulates behavior of "head")
sed 10q
# print only lines which match regular expression (emulates "grep")
sed -n '/regexp/p' # method 1
sed '/regexp/!d' # method 2
# print only lines which do NOT match regexp (emulates "grep -v")
sed -n '/regexp/!p' # method 1, corresponds to above
sed '/regexp/d' # method 2, simpler syntax
# print section of file based on line numbers (lines 8-12, inclusive)
sed -n '8,12p' # method 1
sed '8,12!d' # method 2
SELECTIVE DELETION OF CERTAIN LINES:
# print all of file EXCEPT section between 2 regular expressions
sed '/Iowa/,/Montana/d'
# delete the first 10 lines of a file
sed '1,10d'
# delete the last line of a file
sed '$d'
# delete all leading blank lines at top of file
sed '/./,$!d'
OPTIMIZING FOR SPEED:
Find and Replace
sed 's/foo/bar/g' filename # standard replace command
sed '/foo/ s/foo/bar/g' filename # executes more quickly
sed '/foo/ s//bar/g' filename # shorthand sed syntax
Deleting lines
sed -n '45,50p' filename # print line nos. 45-50 of a file
sed -n '51q;45,50p' filename # same, but executes much faster