Advanced Unix Tips for System Administrators
awk
awk '{print $9}'
sed
Using sed to delete the first line from a display:
bdf | sed
1d
using sed to remove a pattern (/dev/dsk/vpath) from a file and
then send the remaining text (a number) to sort -n:
cat wte_tdvb_vpaths | sed
's/\/dev\/dsk\/vpath//'|sort -n
using sed to parse the vgdisplay to
isolate the number embedded in the logical volume name oraXXXlv and reverse sort
numerically:
vgdisplay
-v| grep "LV Name"|grep ora|awk '{print $3}'|sed 's/\/dev\/vg[0-9][0-9]\///'|sed
's/ora//'|sed 's/lv//'|sort -nr | more
tr
translate
using translate to combine multiple lines in a
strings of /etc/vpath.cfg (HP-UX using IBM SDD software to access ESS
San)
strings /etc/vpath.cfg | grep -v '\.' | sed 's/vpath/z vpath/' | tr
'\012' ' ' | tr 'z' '\012' | grep rdsk | awk '{ print $2, $1 }' | sort >
jdr_vpath_work
- strings prints out ascii contents of mixed ascii file
- grep -v does not print out lines with "." in them
- sed puts a unique character in the separater line (line that starts with
vpath)
- tr replaces a linefeed with a space - effectively combining all lines in
the file to a single line
- tr replace z with a line feed - effectively creating new lines only where
vpath is
- grep finds only lines with rdsk (eliminating some vpaths without disks
associated with them)
- awk prints 2nd and 1st fields
- sort them
xargs
ls -l | grep drw | grep qdbwrite | ? | xargs -i -p {} chgrp pcsas
{}
To clean up the display in bdf when using grep (which will not
display all lines).
bdf | sed 1d | xargs -n 6 | grep -i supb | grep
<string>
Calculating under unix:
expr 4096 \*
2698
Lists processes and kills them in one line
ps -ef |grep
NA_RPT2 | grep -v grep | tee rpt2kill | awk '{print $2;}' | xargs -l1 kill
-9
vmquery -b -p 8 | sed 1,2d | wc -l
bpcllist | xargs -i -p
-t bpclsched \{\} -U
Problem: Move .sav files to
.22may03
ONCDEV01#
ls
bill.sav bill2.sav bill3.sav
bill4.dat
ONCDEV01# ls *.sav | sed
's/\(.*\).sav$/mv "&" "\1.22may03"/'
mv "bill.sav"
"bill.22may03"
mv "bill2.sav" "bill2.22may03"
mv "bill3.sav"
"bill3.22may03"
ONCDEV01# ls *.sav | sed
's/\(.*\).sav$/mv "&" "\1.22may03"/'|sh
ONCDEV01# ls
bill.22may03
bill2.22may03 bill3.22may03 bill4.dat
Problem: Copy
.dat files to .11may03 files
ls |grep
today | sed 's/\(.*\).dat$/cp "&" "\1.dat.12may03"/'|sh
Problem: Remove the .txt and the end of
the file name:
root.oncsgid4:/tmp/java1.4# ls
PHCO_21187.txt
PHKL_18543.txt
PHSS_22868.txt
PHCO_23651.txt
PHKL_20016.txt
PHSS_26945.txt
PHCO_23791.txt
PHKL_27980.txt
PHSS_27858.txt
PHCO_26111.txt
PHKL_28150.txt
PHSS_28433.txt
PHCO_28425.txt
PHKL_28766.txt
PHSS_28869.txt
PHCO_29108.txt
PHKL_29434.txt
sdk14_14105_1100.depot
root.oncsgid4:/tmp/java1.4# ls |grep txt | sed 's/\(.*\).txt$/mv "&"
"\1"/'|sh
root.oncsgid4:/tmp/java1.4# ls
PHCO_21187
PHKL_18543
PHSS_22868
PHCO_23651
PHKL_20016
PHSS_26945
PHCO_23791
PHKL_27980
PHSS_27858
PHCO_26111
PHKL_28150
PHSS_28433
PHCO_28425
PHKL_28766
PHSS_28869
PHCO_29108
PHKL_29434
sdk14_14105_1100.depot
List volume groups and their size
vgdisplay -v | grep -i -e "VG Name" -e "PE Size" -e
"^Total PE"|more
Parse the binary file /etc/vpath.cfg and find the
ESS storage units that are configured:
strings vpath.cfg | grep IBM | awk '{print $3}'|cut -c
8-12|uniq
Parse multiple line file and join only those
lines that end in X (SYMMETRIX) and only print out the 7th and 9th field of the
combined lines (that have EMC in them). The source is a file called
/tmp/wte.emc.
sed -e :a -e '/X$/N;
s/X\n//; ta' /tmp/wte.emc |grep EMC | awk '{print $7 " "$9}'
Do an
ioscan and only get lines that contain EMC or /dev. Join the EMC lines
with the next line. Remove any lines that don't have EMC and then print only the
7th and 9th field.
ioscan -fnC disk |
grep -e "EMC" -e "/dev" | sed -e :a -e '/X$/N; s/X\n//; ta' |grep EMC | awk
'{print $7 " "$9}'
Generate a list of printers that are
defined on a system:
lpstat -v| grep
device |awk '{print $3}'|sed 's/://g'>
/tmp/printerlist.txt
A 2 step process to de-assign tapes
in Veritas Netbackup:
vmquery -l -a | awk
'{print $1 "\t" $13 "\t" $20 "\t" $28}'|grep 200[0-2]|awk '{print $1" "$2"
"$4}'|xargs -i -p echo "vmquery -deassignbyid {}x0" >>
/tmp/wte.vmquery
This creates a file in /tmp called
wte.vmquery. Just chmod +x wte.vmquery and then execute it as a
script. The -p option will prompt the use for each file. In the
situation that this command was used, 200 commands were generated, so after the
proof of concept the -p option was removed.
Removing erroneous
special files in a list from ioscan -fnC tape:
ioscan -fnC tape |grep tape |awk '{print $3}'|xargs -i
rmsf -H {}