This is not my code alone. It was the group effort of Vivek Ram, Cijo George, Sandeep Unni and mine during our epic preparation for Yahoo!. I am just putting it open for others who would be looking for this information.
- Convert all lower case to upper and vice versa.
tr “[a-z] [A-Z]” “[A-Z] [a-z]” < file - print those lines with PRINT in it.On printing remove all small a.
grep ‘PRINT’ < file | sed ‘s/a//g’ - Print only those lines starting with a number and end with a character.
grep “^[0-9].*[A-Za-z]$” temp - remove all double occurences of a character like aa->a
sed ‘s/\(.\)\1*/\1/g’ < file - Remove all double occurences of a word with single. eg: ABS ABS->ABS
sed ‘s/\([A-Za-z]* \)\1*/\1/g’ < file - Replace third occurence of word ABS with XYZ
cat 6|tr ‘\n’ ‘$’|sed s/abs/xyz/3|tr ‘$’ ‘\n’ - Replace ABC->CED RTY->WWW
sed ‘{s/ABC/CED/g;s/RTY/WWW/g;} - If there are 3 lines such tat first line have ‘A’,second ‘B’ and third ‘C’ remove the lines.
cat 5|tr ‘\n’ ‘$’|sed ‘s/[^$]*A[^$]*$[^$]*B[^$]*$[^$]*C[^$]*//’|tr ‘$’ ‘\n’
or
sed ‘/A/ {N; /n\n.*B/ { N;/\n.*C/{s/.*//}}}’ file |grep -v “^$” - Print all lines except those from 20 to 30
awk ‘NR<20 {print $0} NR>30 {print $0}’ file - Remove all C comments from a C file.
sed ‘s/\/\/.*//’ 4|tr ‘\n’ ‘$’|sed ‘s_\/\*[^\*]*\*\/_g’|tr ‘$’ ‘\n’ - Remove all blank lines from a file.
grep -v “^$” file > file - Print total number of lines.
cat filename | wc -l - Print all lines from the line having MACS to 26th line
awk ‘/MACS/,(NR==26) {print $0}’ < file - If a line contain MEC enter ‘DATA’ at the end of the line else enter ‘SATA’ at the beginning.
awk ‘/MEC/ {print $0,”DATA”;} !/MEC/ {print “SATA”,$0;}’ filename - If the line contain HI replace all a->b ad b->c and c->d
sed ‘/HI/ {y/abc/bcd/;}’ filename - If a file contain word ‘XYZ’ anywhere in it no matter how many times,print ‘Success’ once.
awk ‘/XYZ/ {print “SUCCESS”; exit 0;}’ filename - If a file has the word ‘ABC’ first and somewhere else it has the word XYZ; print success only once.
awk ‘/.*ABC.*XYZ.*/ {print “SUCCESS”; exit 0;}’ filename - Remove all \n from the file
tr -d “/n” < file > file - Reverse all lines in a file.
tac file - Here’s a simple encryption algorithm that you can use to encode secret messages: shift every letter in the message 13 characters towards the end of the alphabet. (“a” becomes “n”; “b” becomes “o”; “x” loops around and becomes “k”; etc.) The beauty of this algorithm is that since the alphabet has 26 letters, you can also decrypt a message by repeating the process and shifting every letter in the message 13 characters towards the end of the alphabet. It’s often called “ROT 13″ because the algorithm “rotates” each letter 13 places. Write a ROT 13 program that will accept a line of text, print the encrypted message, then decrypt the message and print the result. [Test: does your program work for uppercase and lowercase letters?]Ans:
#!/usr/bin/perl -w
@in=<>;
foreach $var (@in)
{
@array=split(//,$var);
for($i=0;$i<=$#array;$i++)
{
if($array[$i]=~/[A-Z]/)
{$d=ord($array[$i])+13-ord(“Z”);
if($d>0){$w=ord(“A”)-1+$d;}
else {$w=ord($array[$i])+13;}
}elsif($array[$i]=~/[a-z]/)
{$d=ord($array[$i])+13-ord(“z”);
if($d>0){$w=ord(“a”)-1+$d;}
else {$w=ord($array[$i])+13;}
}
else {$w=ord($array[$i]);}
$w=chr($w);
print $w;
}
} - User provides a date in command line.Print all files that have been modified before that date.Use perl.Assume the script is run in the native folder itself.Ans:
#!/usr/bin/perl
@array=`ls -l`;
my $m;my $d;my $y;
$date=<STDIN>;
($y,$m,$d)=split(/-/,$date);
$date=$d+31*$m+365*$y;
foreach $line(@array)
{
@p=split(/\s+/,$line);
($y,$m,$d)=split(/-/,$p[5]);
$d2=$d+31*$m+365*$y;
if($d2<$date)
{print $p[7],”\n”;}
@p=();
}
Well there is lot more from this came from but those will be another blog post.