Say, I have a config file, myconfig.txt, which contains credentials for an oracle connection:
$ cat myconfig.txt SQLUSR="scott" SQLPWD="pwd123"
$ cat myconfig.txt SQLUSR="scott" SQLPWD="pwd123"
#!/usr/bin/python l1=['hi','hello','welcome'] f=open('f1.txt','w') for ele in l1: f.write(ele+'\n') f.close()The list is iterated, and during every iteration, the write method writes a line to a file along with the newline character.
#!/usr/bin/perl use warnings ; use strict ; $\="\n"; my @arr=(5..8); print "@arr"; foreach (@arr){ $_+=10; } print "@arr";This snippet when run outputs:
5 6 7 8 15 16 17 18As seen above, just by updating the $_, the array elements get updated automatically.
my @arr=qw(bob alice alice chris bob); my @arr1; foreach my $x (@arr){ push @arr1, $x if !grep{$_ eq $x}@arr1; } print "@arr1";A loop is run on the array elements. For every element, it is checked to find out whether the element is already present in the new array. If present, it is skipped, else added to the new array.
$ dateTo get the date command like output in Perl:
my $x=3456; my $sum=0; while ($x) { $sum+=$x%10; $x/=10; } print $sum;The digits are extracted by taking the modulus of the number with 10, and then dividing the number by 10. This is repeated till the original number becomes zero at the end of which the sum is retrieved.
$ x=0010 $ echo $x 00101. Using typeset command of ksh:
$ typeset -LZ x $ x=0010 $ echo $x 10The variable x is declared using the typeset command. The -Z option of typeset will strip the leading zeros present when used along with -L option.
$ cat f1.txt AIX,1 Solaris,2 Unix,3 Linux,4
$ time real 0m0.000s user 0m0.000s sys 0m0.000stime command output shows 3 different components:
cat file Solaris,25,11 Ubuntu,31,2 Fedora,21,3 LinuxMint,45,4 RedHat,12,5
$ sed 's/[^,]*,//' file 25,11 31,2 21,3 45,4 12,5This regular expression searches for a sequence of non-comma([^,]*) characters and deletes them which results in the 1st field getting removed.
$ cat file AIX Solaris Unix Linux HPUX
$ awk 'NR%3' file AIX Solaris Linux HPUXNR%3 will be true for any line number which is not multiple of 3, and hence the line numbers which are multiple's of 3 does not get printed.
my $str="hi hello"; my ($var1,$var2)=split /\s+/,$str;\s+ stands for one or more spaces. By using \s+ as the expression for the split function, it splits everytime it encouters a series of spaces and hence the words are retrieved.
my $str="hi hello"; my @arr=split /\s+/,$str;
$ cat file P1,2012 12 4 21 36 48,2012 12 4 22 26 53 P2,2012 12 4 20 36 48,2012 12 4 21 21 23 P3,2012 12 4 18 36 48,2012 12 4 20 12 35Time difference in seconds:
$ awk -F, '{d2=mktime($3);d1=mktime($2);print $1","d2-d1,"secs";}' file P1,3005 secs P2,2675 secs P3,5747 secsUsing mktime function, the Unix time is calculated for the date time strings, and their difference gives us the time elapsed in seconds.
$ echo | awk '{print systime();}' 1358146640Note: systime function does not take any arguments.
$ date '+%s' 1357906744
my $x='welcome'; my @arr=split (//, $x); print "@arr";
w e l c o m e
#!/usr/bin/perl use warnings; use strict; $\="\n"; my %h1=(Jan => 31, Feb => 28, Mar => 31); foreach my $key (keys%h1) { print "$key => $h1{$key} "; }Using the keys function, the keys are generated and processed in a loop. In every iteration, the key and its corresponding value is printed using the $key variable which holds the key.