Copy multiple files with same name from different directories

Thursday, May 30, 2013 , , , , 1 Comments

I regularly do this. So I automated it.
Below is a small simple script for copying files which are present in two different directories but without same names into a single directory with having name collision.

#!/usr/bin/sh

for i in `find <source_dir_path> -type f`
do
new=`echo $i|nawk -F"/" '{split($NF,a,".");print "<target_dir_path>"a[1]"_"$(NF-1)"."a[2]}'`
cp $i $new
done

1 comments:

Pattern Match Using Perl-Append file contents

Wednesday, May 29, 2013 , 0 Comments

If i want to append all the lines of one file  to a line in second file only if there is a pattern match:

For example:
first_file.txt:
111111
1111
11
1

second_file.txt:

122221
2222
22
2

pattern to match:

2222
output:

122221
111111
1111
11
1
2222
111111
1111
11
1
22
2

Below is the perl command to achieve this:

perl -lne 'BEGIN{open(A,"first_file.txt");@f=<A>}
           print;if(/2222/){print @f}' 
           second_file.txt

0 comments:

Retrieving a List of files from Remote Server

Friday, May 17, 2013 , 0 Comments

Below is the perl script for retrieving the list of files from Remote server:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Net::FTP;
    my $ftp_site     = 'localhost';
    my $ftp_dir      = '/home/simon/software';
    my $ftp_user     = 'a_login_name';
    my $ftp_password = 'a_password';
    my $glob         = 'ex*';
    my @remote_files;
    my $ftp = Net::FTP-&gt;new($ftp_site)
        or die "Could not connect to $ftp_site: $!";
    $ftp-&gt;login($ftp_user, $ftp_password)
        or die "Could not login to $ftp_site with user $ftp_user: $!";
    $ftp-&gt;cwd($ftp_dir)
        or die "Could not change remote working " .
                 "directory to $ftp_dir on $ftp_site";
    @remote_files = $ftp-&gt;ls($glob);
    foreach my $file (@remote_files) {
        print "Saw: $file\n";
    }
    $ftp-&gt;quit();

0 comments:

List of Bash Conditional statements

Wednesday, May 15, 2013 , 3 Comments

At times you need to specify different courses of action to be taken in a shell script, depending on the success or failure of a command. The if construction allows you to specify such conditions.

The most compact syntax of the if command is:
if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi

The TEST-COMMAND list is executed, and if its return status is zero, the CONSEQUENT-COMMANDS list is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.
The TEST-COMMAND often involves numerical or string comparison tests, but it can also be any command that returns a status of zero when it succeeds and some other status when it fails. Unary expressions are often used to examine the status of a file. If the FILE argument to one of the primaries is of the form /dev/fd/N, then file descriptor "N" is checked. stdin, stdout and stderr and their respective file descriptors may also be used for tests.

Expressions used with if
The table below contains an overview of the so-called "primaries" that make up the TEST-COMMAND command or list of commands. These primaries are put between square brackets to indicate the test of a conditional expression.

Primary expressions
[ -a FILE ] True if FILE exists.
[ -b FILE ] True if FILE exists and is a block-special file.
[ -c FILE ] True if FILE exists and is a character-special file.
[ -d FILE ] True if FILE exists and is a directory.
[ -e FILE ] True if FILE exists.
[ -f FILE ] True if FILE exists and is a regular file.
[ -g FILE ] True if FILE exists and its SGID bit is set.
[ -h FILE ] True if FILE exists and is a symbolic link.
[ -k FILE ] True if FILE exists and its sticky bit is set.
[ -p FILE ] True if FILE exists and is a named pipe (FIFO).
[ -r FILE ] True if FILE exists and is readable.
[ -s FILE ] True if FILE exists and has a size greater than zero.
[ -t FD ]   True if file descriptor FD is open and refers to a terminal.
[ -u FILE ] True if FILE exists and its SUID (set user ID) bit is set.
[ -w FILE ] True if FILE exists and is writable.
[ -x FILE ] True if FILE exists and is executable.
[ -O FILE ] True if FILE exists and is owned by the effective user ID.
[ -G FILE ] True if FILE exists and is owned by the effective group ID.
[ -L FILE ] True if FILE exists and is a symbolic link.
[ -N FILE ] True if FILE exists and has been modified since it was last read.
[ -S FILE ] True if FILE exists and is a socket.
[ FILE1 -nt FILE2 ] True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.
[ FILE1 -ot FILE2 ] True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not.
[ FILE1 -ef FILE2 ] True if FILE1 and FILE2 refer to the same device and inode numbers.
[ -o OPTIONNAME ] True if shell option "OPTIONNAME" is enabled.
[ -z STRING ] True of the length if "STRING" is zero.
[ -n STRING ] or [ STRING ] True if the length of "STRING" is non-zero.
[ STRING1 == STRING2 ]  True if the strings are equal. "=" may be used instead of "==" for strict POSIX compliance.
[ STRING1 != STRING2 ]  True if the strings are not equal.
[ STRING1 < STRING2 ]  True if "STRING1" sorts before "STRING2" lexicographically in the current locale.
[ STRING1 > STRING2 ]  True if "STRING1" sorts after "STRING2" lexicographically in the current locale.
[ ARG1 OP ARG2 ] "OP" is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are integers.
Expressions may be combined using the following operators, listed in decreasing order of precedence:
Combining expressions
[ ! EXPR ]          True if EXPR is false.
[ ( EXPR ) ]        Returns the value of EXPR. This may be used to override the normal precedence of operators.
[ EXPR1 -a EXPR2 ]  True if both EXPR1 and EXPR2 are true.
[ EXPR1 -o EXPR2 ]  True if either EXPR1 or EXPR2 is true.

The [ (or test) built-in evaluates conditional expressions using a set of rules based on the number of arguments. More information about this subject can be found in the Bash documentation. Just like the if is closed with fi, the opening square bracket should be closed after the conditions have been listed.

3 comments:

Semicolon and Perl

Monday, May 13, 2013 , 0 Comments

I guess most of the people have noticed about this and have just ignored it. Or they must not have noticed it at all. Do you know this fact about perl:
If you write this :
for(@arr) { print $_ }
Its not an error in Perl. According to Perldoc:
Every simple statement must be terminated with a semicolon, unless it is the final statement in a block, in which case the semicolon is optional.

0 comments:

Row width and Column separator in Sybase isql

Thursday, May 09, 2013 , 0 Comments

Many of you either donot know or perhaps you are not aware of these since there is very less documentation available in google search for these

In general I use:
isql -U<user> -P<passsword>  -D<dbname>

There are two ioptions in isql.
which are
-s     Column separator.This wil separate columns with what ever character is followed by this option
       another option is (and probably very important):
-w     This will set the row width.By default is set to 80 characters and if the row length exceeds 80          
       characters, a new line is added in the console output.
for eg:
isql -U<user> -P<passsword>  -D<dbname> -s ',' -w 65535

0 comments:

Consequetive pattern match in Perl

Tuesday, May 07, 2013 , 0 Comments

Make a pattern that will match three consecutive copies of whatever is currently contained in $what. That is, if
$what="fred" , your pattern should match "fredfredfred".
If
$what is "fred|barney", your pattern should match
"fredfredbarney" and
"barneyfredfred" and
"barneybarneybarney" and  many other variations.
Well, This is quite tricky. But the magic match to do over here is :
/($str){3}/

 Below is the example code:
#!/usr/bin/perl
use warnings;
use strict;
# notice the example has the `|`. Meaning
# match "fred" or "barney" 3 times.
my $str = 'fred|barney';
my @tests = qw(fred fredfredfred barney barneybarneybarny barneyfredbarney);
for my $test (@tests) {
if( $test =~ /^($str){3}$/ ) {
print "$test matched!\n";
} else {
print "$test did not match!\n";
}
}

Below is the execution:
> ./temp9.pl
fred did not match!
fredfredfred matched!
barney did not match!
barneybarneybarny did not match!
barneyfredbarney matched!

0 comments:

How does free work in C

Thursday, May 02, 2013 , , , 0 Comments

This is very important for every C/C++ programmer to know. And most of the interviewers too ask this question.I myself had asked in many interviews, but hardly I can get a good response from the people. So I thought it would be better to post it here so that people will get to know.
char *ptr =malloc(5*sizeof(char));
The above statement will allocate 5 bytes of memory for the pointer ptr on HEAP. Most of the people that I came across specially graduates think that memory is allocated on stack which is actually not true.

So after you use ptr in your code and get the task done you have to delete or free the memory.so you write this for it.
free(ptr);
Remember, you are not passing 5 bytes to free as an argument, but how come free function know that it has to free 5 bytes? When you call malloc(), you specify the amount of memory to allocate. The amount of memory actually used is slightly more than this, and includes extra information that records (at least) how big the block is. You can't (reliably) access that other information - and nor should you . When you call free(), it simply looks at the extra information to find out how big the block is.

This is the logic behind this magic(at least for a programmer this is a magic provided by the standard).
also see the link in c-faqs:
How does free know how many bytes to free?
Same principle is followed for delete and new.

0 comments: