Unix Awk Swd

3
1.Awk general syntax is as bellow. awk 'BEGIN {<initializations>} <search pattern 1> {<program actions>} <search pattern 2> {<program actions>} ... END {<final actions>}' 2.To replace a pattern in a file. awk '{gsub(/pattern to be replaced/,"pattern to be replaced with ");print}' filename>dest_filename. Dest_filename will have made changes 3. searching a file in current directory find . -name \*filename 4.concatinating 2 files side by side paste a_file b_file > outfilename 5. printing n lines before a parrern is matched grep -B 2 "important" online_backup.dat 6. printing n lines after a parrern is matched grep -A 2 "flexibility" linux_enterpise.dat 7. awk '/SUMMARY/ {print $1}' daqa_report.xml 9.awk '{ sub(/candidate/, "& and his wife"); print }' 10.sed '/unix/ a "Add a new line"' file.txt (use \ for ") add a line after match

description

unix

Transcript of Unix Awk Swd

Page 1: Unix Awk Swd

1.Awk general syntax is as bellow. awk 'BEGIN {<initializations>} <search pattern 1> {<program actions>} <search pattern 2> {<program actions>} ... END {<final actions>}'

2.To replace a pattern in a file. awk '{gsub(/pattern to be replaced/,"pattern to be replaced with ");print}' filename>dest_filename. Dest_filename will have made changes

3. searching a file in current directory find . -name \*filename

4.concatinating 2 files side by side paste a_file b_file > outfilename

5. printing n lines before a parrern is matched grep -B 2 "important" online_backup.dat

6. printing n lines after a parrern is matched grep -A 2 "flexibility" linux_enterpise.dat

7. awk '/SUMMARY/ {print $1}' daqa_report.xml

9.awk '{ sub(/candidate/, "& and his wife"); print }'

10.sed '/unix/ a "Add a new line"' file.txt (use \ for ")

add a line after match11. sed '/unix/ i "Add a new line"' file.txt add a line initial to match

12.while (($key,$value) = each(%$h2)) accessing hash 13. # Get command line options, variables should start with $opt<switch>

Page 2: Unix Awk Swd

use Getopt::Long;my $ret = &GetOptions('help', \$opt_help, 'modelname=s', \$opt_modelname, 'm=s', \$opt_modelname );14. exeption handle. $SIG{'INT'} = \&exceptionhandler;$SIG{'TERM'} = \&exceptionhandler;

1. 15. $string =~ s/^\s+//; #remove leading spaces2. $string =~ s/\s+$//; #remove trailing spaces

15 .vimrc content

syn on set nu set wrap16. deleting a line if a pattern is found in vi:g/{pattern}/d

17: back substitution in vi

:%s/^\(.*\)$/RewriteRule ^\1 [R=301,L]/g

18. to do remove a lineperl -pi -e 'if(/acetate/){undef $_}' your_file