Tuesday 2 March 2021

Top 10 Example of lsof commands in Linux and UNIX

In a Linux or UNIX operating system, there are many processes running simultaneously. These processes use files, sockets, and other resources that are managed by the operating system. To get information about these resources, we can use the "lsof" (list open files) command. Lsof is a powerful tool that can help us understand the system's state, monitor and debug applications, and troubleshoot issues. In this article, we will look at ten examples of using the lsof command in Linux and UNIX.

List all open files:

To list all the open files in the system, we can simply run the lsof command without any arguments. This will give us a comprehensive list of all the files and their corresponding processes.

lsof

Read more »

Labels: , ,

Monday 4 November 2019

perl read file - 15 ways

Method 1:


open(FH,'<file.txt') or die "Cant open file $!";

print FH;

close(FH);

Read more »

Labels:

Sunday 1 December 2019

perl piped open - reading data from a system process?

Perl Piped Open:

open function is very useful for reading or writing the data files on the system and apart from this file processing we can use pipe symbol for getting data from a system process or sending the data to a system process can be achieved.   lets try this now,


Read more »

Labels: , , , , , ,

Sunday 9 October 2022

Building and Deploying a Containerized Application with Amazon Elastic Kubernetes Service - Lab

 SPL-BE-200-COCEKS-1 - Version 1.0.8

© 2023 Amazon Web Services, Inc. or its affiliates. All rights reserved. This work may not be reproduced or redistributed, in whole or in part, without prior written permission from Amazon Web Services, Inc. Commercial copying, lending, or selling is prohibited. All trademarks are the property of their owners.

Note: Do not include any personal, identifying, or confidential information into the lab environment. Information entered may be visible to others.

Corrections, feedback, or other questions? Contact us at AWS Training and Certification.

Read more »

Labels: ,

Thursday 9 June 2022

perl vlsi program for Design Rule Checker


#!/usr/bin/perl


use strict;

use warnings;


# Read design rules from file

my $rule_file = shift;

open my $fh, "<", $rule_file or die "Error: Could not open rule file: $!\n";

my $rules = do { local $/; <$fh> };

close $fh;


# Read layout design from file

my $design_file = shift;

open $fh, "<", $design_file or die "Error: Could not open design file: $!\n";

my $design = do { local $/; <$fh> };

close $fh;


# Check design against rules

my $errors = 0;

foreach my $rule (split /\n/, $rules) {

    chomp $rule;

    $rule =~ s/^\s+|\s+$//g;

    next if $rule =~ /^#/;

    if ($rule =~ /^(\S+)\s+(.*?)$/) {

        my $rule_type = $1;

        my $rule_pattern = $2;

        if ($rule_type eq "layer") {

            # Check for correct layer usage

            my @matches = $design =~ /$rule_pattern/g;

            if (@matches) {

                print "Error: Found $rule_type \"$rule_pattern\" in layout design\n";

                $errors++;

            }

        }

        elsif ($rule_type eq "spacing") {

            # Check for minimum spacing between objects

            my $spacing = $rule_pattern;

            my $pattern = "\\s*[Mm]\\s*\\(\\s*\\d+\\s*\\)\\s*[Mm]"; # matches two adjacent metal layers

            my @matches = $design =~ /$pattern/g;

            foreach my $match (@matches) {

                if ($match =~ /($pattern){0,$spacing-1}/) {

                    print "Error: Found adjacent objects with spacing less than $spacing in layout design\n";

                    $errors++;

                    last; # exit loop once an error is found

                }

            }

        }

        else {

            print "Warning: Unknown design rule type \"$rule_type\"\n";

        }

    }

}


if ($errors == 0) {

    print "Design passed all design rule checks.\n";

}

else {

    print "Design failed $errors design rule checks.\n";

}


 it will print out any errors or warnings that are detected during the design rule checking process. If there are no errors, it will print a message indicating that the design has passed all design rule checks.

design rules are specified in a file, where each rule is a line that starts with a rule type ("layer" or "spacing"), followed by a rule pattern that specifies the layer or spacing requirements. 

For example:

# Design rules file

layer M1

layer M2

spacing 3 

run it from the command line and specify the design rules file and layout design file as arguments. For example:

$ perl design_rule_checker.pl design_rules.txt layout_design.gds

 design_rule_checker.pl is the name of the Perl program, design_rules.txt is the name of the file containing the design rules, and layout_design.gds is the name of the layout design file to be checked.

example design rules file that checks for some common design rule violations:

# Design Rules File



# Define spacing rules

spacing metal1_metal1 0.12

spacing metal1_via1 0.12

spacing metal2_metal2 0.15

spacing metal2_via2 0.15



# Define width rules

width metal1 0.25

width metal2 0.4



# Define enclosure rules

enclosure metal1_via1 0.1

enclosure metal2_via2 0.2

The spacing rule checks the minimum distance between two layers, while the width rule checks the minimum width of a metal layer. The enclosure rule checks the minimum amount of metal surrounding a via.

The metal1_metal1, metal1_via1, metal2_metal2, and metal2_via2 keywords are the layer types that the rules apply to. These keywords should match the layer names used in the layout design file.

To use this design rules file with the design_rule_checker.pl program, you would specify the filename as the first argument when running the program:

$ perl design_rule_checker.pl design_rules.txt layout_design.gds


#!/usr/bin/perl
use strict;
use warnings;

# Read in the design rules file
my $rules_file = $ARGV[0];
open(my $rules_fh, '<', $rules_file) or die "Could not open $rules_file: $!";

# Parse the design rules and store them in a hash
my %rules;
while (my $line = <$rules_fh>) {
    chomp $line;
    my ($rule_type, $layer_type, $value) = split ' ', $line;
    $rules{$rule_type}{$layer_type} = $value;
}
close $rules_fh;

# Read in the layout design file
my $layout_file = $ARGV[1];
open(my $layout_fh, '<', $layout_file) or die "Could not open $layout_file: $!";

# Parse the layout design and check against the design rules
my $line_number = 0;
while (my $line = <$layout_fh>) {
    $line_number++;
    chomp $line;

    # Check for minimum spacing violations
    if ($line =~ /^S\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)/) {
        my ($type1, $layer1, $type2, $layer2, $distance, $extension) = ($1, $2, $3, $4, $5, $6);
        if ($rules{'spacing'}{"$layer1\_$layer2"} && $distance < $rules{'spacing'}{"$layer1\_$layer2"}) {
            print "Error: Minimum spacing violation on line $line_number\n";
        }
    }

    # Check for minimum width violations
    if ($line =~ /^W\s(\S+)\s(\S+)/) {
        my ($layer, $width) = ($1, $2);
        if ($rules{'width'}{$layer} && $width < $rules{'width'}{$layer}) {
            print "Error: Minimum width violation on line $line_number\n";
        }
    }

    # Check for minimum enclosure violations
    if ($line =~ /^E\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)/) {
        my ($type1, $layer1, $type2, $layer2, $distance) = ($1, $2, $3, $4, $5);
        if ($rules{'enclosure'}{"$layer1\_$layer2"} && $distance < $rules{'enclosure'}{"$layer1\_$layer2"}) {
            print "Error: Minimum enclosure violation on line $line_number\n";
        }
    }
}
close $layout_fh;

The design rules file should contain the following information:

  • Minimum spacing between two layers
  • Minimum width for a layer
  • Minimum enclosure of one layer around another layer

The layout design file should contain the following information for each component:

  • Type of component
  • Layer of component
  • Position of component
  • Dimensions of component

The design_rule_checker.pl program will output an error message for any violations of the design rules, indicating the type of violation and the line number in the layout design file where the violation occurred.


Labels:

Friday 8 May 2020

perl program to Merging Files into a Single File

This Perl script reads all the files with a given extension in the current directory and merges them into a single file. The merged file name can be passed as a command-line argument.

Method 1:Using file handles

#!/usr/bin/perl

my ($ext, $merged_file) = @ARGV;

my @files = glob "*.$ext";

open my $out_fh, '>', $merged_file or die "Couldn't open $merged_file: $!";

foreach my $file (@files) {

    open my $in_fh, '<', $file or warn "Couldn't open $file: $!";

    while (my $line = <$in_fh>) {

        print $out_fh $line;

    }

    close $in_fh;

}

close $out_fh;

Save the script as merge.pl and run it with perl merge.pl txt merged.txt. This will merge all the files with the extension "txt" into a single file called "merged.txt".

Method 2: Using cat command

This method uses the cat command to concatenate multiple files into a single file.

#!/usr/bin/perl

use strict;

use warnings;

my ($output_file, @input_files) = @ARGV;

system("cat @input_files > $output_file");


Method 3: Using File::Slurp

This method uses the File::Slurp module to read each input file and append its contents to the output file.

#!/usr/bin/perl

use strict;

use warnings;

use File::Slurp;

my ($output_file, @input_files) = @ARGV;

my $output_contents = '';

foreach my $input_file (@input_files) {

    $output_contents .= read_file($input_file);

}

write_file($output_file, $output_contents);


Method 4: Using IO::All

This method uses the IO::All module to read each input file and append its contents to the output file.

#!/usr/bin/perl

use strict;

use warnings;

use IO::All;

my ($output_file, @input_files) = @ARGV;

my $output_contents = '';

foreach my $input_file (@input_files) {

    $output_contents .= io($input_file)->all;

}

io($output_file)->print($output_contents);

Save the script as merge_files_method4.pl and run it with perl merge_files_method4.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt.


Method 5: Using File::MergeSort

This method uses the File::MergeSort module to merge sorted files into a single file.

#!/usr/bin/perl

use strict;

use warnings;

use File::MergeSort;

my ($output_file, @input_files) = @ARGV;

my $sorter = File::MergeSort->new();

$sorter->cmp(sub { $_[0] cmp $_[1] });

foreach my $input_file (@input_files) {

    $sorter->load($input_file);

}

$sorter->save($output_file);

Save the script as merge_files_method5.pl and run it with perl merge_files_method5.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt, sorted.


Method 6: Using Path::Tiny

This method uses the Path::Tiny module to read each input file and append its contents to the output file.

#!/usr/bin/perl

use strict;

use warnings;

use Path::Tiny;

my ($output_file, @input_files) = @ARGV;

my $output_contents = '';

foreach my $input_file (@input_files) {

    $output_contents .= path($input_file)->slurp;

}

path($output_file)->spew($output_contents);

Save the script as merge_files_method6.pl and run it with perl merge_files_method6.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt.


Method 7: Using File::Slurper

This method uses the File::Slurper module to read each input file and append its contents to the output file.

#!/usr/bin/perl

use strict;

use warnings;

use File::Slurper qw(read_text write_text);

my ($output_file, @input_files) = @ARGV;

my $output_contents = '';

foreach my $input_file (@input_files) {

    $output_contents .= read_text($input_file);

}

write_text($output_file, $output_contents);

Save the script as merge_files_method7.pl and run it with perl merge_files_method7.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt.


Method 8: Using File::ReadBackwards

This method uses the File::ReadBackwards module to read each input file backwards and append its contents to the output file.

#!/usr/bin/perl

use strict;

use warnings;

use File::ReadBackwards;

my ($output_file, @input_files) = @ARGV;

open(my $out_fh, '>', $output_file) or die "Can't open $output_file: $!";

foreach my $input_file (@input_files) {

    my $in_fh = File::ReadBackwards->new($input_file) or warn "Can't open $input_file: $!";

    while (defined(my $line = $in_fh->readline)) {

        print $out_fh $line;

    }

}

close($out_fh);

Save the script as merge_files_method8.pl and run it with perl merge_files_method8.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt.

Labels:

Wednesday 22 April 2020

Python code to remove blank lines and duplicate whitespaces

This Python program is a simple yet powerful code snippet that removes blank lines and duplicate whitespaces from a given text file. The program reads a file named "filename" and removes any blank lines in the file. It also removes any duplicate whitespaces that may exist in the file. The final result is written back to the same file, overwriting the original contents. This program can be extremely useful for anyone working with text files, especially when it comes to cleaning up the contents of the file for further processing or analysis.

Read more »

Labels: ,

Friday 2 October 2020

write 1000 uniq perl interview questions and answers to master in perl - part1

  1. Q: In Perl, what does the sigil $_ represent?

    A: In Perl, the sigil $_ represents the default argument to a subroutine. It is a global variable that is set automatically by Perl whenever a subroutine is called.

  2. Q: In Perl, what does the backslash \ do in front of a variable name?

    A: In Perl, the backslash \ in front of a variable name is used to force a string interpretation of the variable, rather than a numeric interpretation. This can be useful when working with non-numeric variables, or when you want to avoid potential side effects caused by Perl's built-in operations on numbers

Read more »

Labels: