Sunday 12 January 2020

Top 10 examples of grep command in UNIX and Linux

The grep command is a powerful tool for searching and filtering text files in UNIX and Linux systems. It allows users to search for a specific pattern in a file or a set of files and display the lines that match that pattern. This command is incredibly versatile and can be used for a variety of tasks, including log analysis, system administration, and programming.

In this blog post, we'll explore 10 examples of the grep command in UNIX and Linux, with code examples to illustrate each use case. By the end of this post, you'll have a better understanding of how to use this command and how it can help you in your day-to-day tasks.

Example 1: Basic Search

The most basic use case for the grep command is to search for a specific pattern in a file. To do this, simply enter the following command:

The most basic use case for the grep command is to search for a specific pattern in a file. To do this, simply enter the following command:

grep apple fruits.txt

For example, to search for the word "apple" in the file "fruits.txt", enter the following command:

This will display all lines in the file that contain the word "apple".

Example 2: Case-Insensitive Search

By default, the grep command is case-sensitive, which means that it will only match patterns that are identical in case to the search term. However, you can use the -i option to perform a case-insensitive search. For example:

grep -i apple fruits.txt


This will match lines that contain "apple", "Apple", or "APPLE".

Example 3: Search Multiple Files

You can also use the grep command to search multiple files at once. To do this, simply specify the filenames separated by spaces. For example:

grep apple fruits.txt vegetables.txt


This will search for the word "apple" in both the "fruits.txt" and "vegetables.txt" files.

Example 4: Search All Files in a Directory

To search all files in a directory, you can use the wildcard character "*". For example:

grep apple *


This will search for the word "apple" in all files in the current directory.

Example 5: Inverse Search

By default, the grep command displays all lines that match the search pattern. However, you can use the -v option to display all lines that do not match the pattern. For example:

grep -v apple fruits.txt


This will display all lines in the "fruits.txt" file that do not contain the word "apple".

Example 6: Search for Whole Words Only

By default, the grep command will match any occurrence of the search pattern, even if it's part of a larger word. For example, the search term "the" will match words like "there", "theme", and "other". To search for whole words only, use the -w option. For example:

grep -w the story.txt


This will only match the word "the", and not words that contain it as a substring.

Example 7: Recursive Search

If you want to search for a pattern in all files in a directory and its subdirectories, use the -r option. For example:

grep -r apple /home/user/documents


This will search for the word "apple" in all files in the "documents" directory and its subdirectories.

Example 8: Count Matches

If you just want to know how many times a pattern appears in a file, use the -c option. For example:

grep -c apple fruits.txt


Example 9:  Do not Matches

Search for lines that do not contain the word "example" in a file "file.txt"

grep -v "example" file.txt



Example 10: Exclude Matches in File

Search for a word "example" in all files except those with a ".txt" extension

grep "example" --exclude=*.txt *


Labels: , ,

Friday 4 October 2019

how many types available for getting string variable length in perl?

Method 1:

my $c = map $_, $h =~ /(.)/g;


Method 2:

my $c = split '', $h;


Method 3:

my $count = () = "hello" =~ /./g;


Method 4:

print length $h;


Method 5:

$cnt = "hello" =~ tr/[a-z][A-Z][0-9]//;


Method 6:

$h='hello';
my $v=$h=~ s/[a-z]//g;


Method 7:

print  @_ = push(@_, map $_, (split '', 'hellgo'));


Method 8:

print  @_ = push(@_, grep $_, (split '', 'hellgo'));


Method 9:

print push(@_, grep $_, "hello" =~ /./g);


Method 10:

print push(@_, map $_, "hello" =~ /./g);


Method 11:

print push(@_, grep $_, reverse "hello" =~ /./g);


Method 12:

print push(@_, map $_, reverse "hello" =~ /./g);


Method 13:

print push(@_, map $_, (split '', 'hellgo'));


Method 14:


my $c = grep/./, (reverse split '', 'hello');
print $c;


Method 15:

my $c = map $_, (reverse split '', 'hello');
print $c;


Method 16:

my $c = map $_, (reverse "hello" =~ /./g);
print $c;


Method 17:

my $c = grep/./, (reverse "hello" =~ /./g);
print $c;


Method 18:

my $c = map $_, (reverse split '', 'hello');
print $c;


Method 19:

my $c = grep/./, (reverse split '', 'hello');
print $c;


Method 20:

print scalar map $_, 'hello' =~ /(.)/g;


Method 21:

print scalar grep/./,'helloh' =~ /(.)/g;


Method 22:

print scalar  map $_, (reverse "hello" =~ /./g);


Method 23:

print scalar  map $_, (reverse split '', 'hello');


Method 24:

print scalar  map $_, (split '', 'hello');


Method 25:

print $r = @_= map $_, (split '', 'hello');


Method 26:

print $r = @_= grep $_, (split '', 'hello');


Method 27:

print $r = @_= grep $_, (reverse "hello" =~ /./g);


Method 28:

print $r = @_= grep/./,'helloh' =~ /(.)/g


Method 29:

print $r = @_= map  $_, (reverse "hello" =~ /./g);


Method 30:

print $r = @_= map/./,'helloh' =~ /(.)/g


Method 31:

print $r = @_= map $_, (split '', 'hello');


Method 32:

print $r = @_= grep $_, (split '', 'hello');


Method 33:

print $r = @_= grep $_, (reverse "hello" =~ /./g);


Method 34:

print $r = @_= grep/./,'helloh' =~ /(.)/g


Method 35:

print $r = @_= map  $_, (reverse "hello" =~ /./g);


Method 36:

print $r = @_= map/./,'helloh' =~ /(.)/g


Method 37:

print scalar  map $_, (reverse split '', 'hello');


Method 38:

print length reverse  grep/./,'helloh' =~ /(.)/g;


Method 39:

print length reverse  map $_,'helloh' =~ /(.)/g;


Method 40:

print grep/./, (reverse "hello" =~ tr/[a-z][A-Z][0-9]//);


Method 41:

print map $_, (reverse "hello" =~ tr/[a-z][A-Z][0-9]// );




For More Methods Reach Me:@ letscrackperlinterviewblogspot@gmail.com  

Labels: , , , , ,

Tuesday 23 April 2024

Extracting Data from XML Files Using Linux Command Line

In the world of system administration and data processing, the ability to extract specific information from XML files directly on the command line is a valuable skill. Although XML files are best handled with tools designed for XML due to their structure and nesting levels, sometimes you might find yourself restricted to using basic Unix utilities like grep and sed. Here, we’ll explore how to use these tools to extract data from XML files, specifically focusing on retrieving a username from an XML configuration file.

Read more »

Labels:

Thursday 31 October 2019

Perl In Line Hack - Perform Variable Assigning and Deletion in one Line

Method 1:


($_ = 'hellio') =~ s/i//;

print $_;


Method 2:


s/i// for (my $_ = 'hellio');

print $_;


Method 3:


substr(($_ = 'helloi'), index ($_, "i"), length($_),"")

print $_;


Method 4:


map $_ =~ s/i//,$_='hellio';

print $_;


Method 5:


grep $_ =~ s/i//,$_='hellio';

print $_;


Method 6:


print grep $_=~/[^i]+/,split('',$_='hellio');



Method 7:


map $_=~tr/i//d,$_="hellio";

print $_;


Method 8:


$_=~ s/i// if $_='hellio';

print $_;


Method 9:


$_ =~ tr/i//d if $_ = 'hellio';

print $_;


Method 10:


print grep $_=~/[^i]+/, @_, split '', 'hellio';


Method 11:


print grep $_=~/[^i]+/, "hellio"=~ /./g;


Method 12:


($_='hellio')=~tr/i//d;

print $_;


Method 13:


$_="hellio",$_=~tr/i//d;

print $_;


Method 14:


print @_,grep $_=~/[^i]+/, (split '', 'hellio');


Method 15:


grep $_=~tr/i//d,$_="hellio";

print $_;


kaavannan perl blogspot

Labels: , , , ,

Friday 1 November 2019

perl - find missing numbers in array

Method 1:

@a = (1,3,4,6); @c = map $a[$_-1]+1..$a[$_]-1, 1..@a-1; print join(" ", @c);




Read more »

Labels:

Tuesday 10 September 2019

How Many Possible ways to remove Duplicates in an array using perl?


Method 1:

sub filter {
    my %hash;
    grep !$hash{$_}++, @_;
}

my @array = qw(e f g h i);
my @final= filter(@array);


Method 2:

use List::MoreUtils qw(uniq);
my @array = qw(1 1 1 2 3 4 4);
my @final= uniq(@array);


Method 3:

my %hash = ();
foreach my $item (@array)
{
    $hash{$item}++;
}
my @final = keys %hash;


Method 4:

my @array=qw( 3 2 3 4 4 3 2);
my @final=keys %{{ map{$_=>1}@array}};
print join ' ', sort{$a<=>$b} @final;


Method 5:

%hash=();
my @array = qw(e j g h i);
@final = grep { ! $hash{$_} ++ } @array;


Method 6:

my @array;
my @finals;

foreach my $var ( @array )
{
    if (! grep(/$var/, @finals ))
    {
          push( @finals, $var );
     }
}


Method 7:

my @array = qw(e f g h i);
my %hash;
@hash{@array}=(1..@array);
@final=keys%hash;


Method 8:

my @final = keys {map {$_ => 1} @array};


Method 9:

@array = qw(e f g h i);
%hash1   = map { $_ => 1 } @array;
@final = keys %hash1;


Method 10:
my @array = qw(e f g h i);
my %hash;
foreach my $v (@array){
        $hash{$v}=1;
}
@final=keys%hash;


kaavannan perl blogspot

Labels: , ,

Tuesday 13 August 2019

How to find size of an array in Perl and what are the ways to find it?


Method1:
@a=(1,2,9,5,6);
print scalar @arr;


Method2:
@a=(1,2,9,5,6);
print $#a;   
print $#a + 1;


Method3:
perl -le '@a=(1,2,9,5,6); print $#a + 1;'


Method4:
my $size = @arr;
print $size;


Method 5:
@a=(1,2,9,5,6);
print @a."\n";


Method 6:
@a=(1,2,9,5,6);
print scalar grep /$_/ ,@a;



Method 7:
@a=(1,2,9,5,6);
print scalar map /$_/ ,@a;


Method 8:

@a=(1,2,9,5,6);
print scalar map $_ =~ s///,@a;


Method 9:

@a=(1,2,9,5,6,8);
print scalar grep $_ =~ s///,@a;

Method 10:

@a=(1,2,9,5,6,8);
print $_= map{$_ =~ s///}@a;

Method 11:

@a=(1,2,9,5,6,8);
print  scalar grep{$_ =~ s///}@a;


Method 12:

@a=(1,2,9,5,6,8);
$b=0;
$b+=$_=~s/// for @a;
print $b;




Sunday 20 October 2019

how can we empty an elements of an array using perl?

Method 1:

@a=(1,7,9,4);
$#a = -1;
print @a;


Method 2:

@a=(1,7,9,4);
@a = ();
print @a;


Method 3:

@a=(1,7,9,4);
$_=(@a) = ();
print @a;


Method 4:

@a=(1,7,9,4);
$_=\@a;
@$_=();
print @a;


Method 5:

@a=(1,7,9,4);
$_=\@a;
@$_=[];
@$_=();
print @a;


Method 6:

@a=(1,7,9,4);
@a= map /![^.]+/,@a;
print @a;



Method 7:

@a=(1,7,9,4)
@a= grep /![^.]+/,@a;
print @a;


Method 8:

@a=(1,7,9,4)
@a= /![^.]+/ ~~ @a
print @a;


Method 9:

@a=(1,7,9,4);
map $_=~s/.+//, @a;
print @a;


Method 10:

@a=(1,7,9,4);
grep $_=~s/.+//, @a;
print @a;


Method 11:

@a=(1,7,9,4);
map {  $_=~s/.+//g; } @a ;
print @a;


Method 12:

@a=(1,7,9,4);
grep {  $_=~s/.+//g; } @a ;
print @a;


Method 13:

@a=(1,7,9,4);
$_=~s/.+// for @a;
print @a;


Method 14:

@a=(1,7,9,4);
my @b=();
@a=@b;
print @a;


Method 15:

@a=(1,7,9,4);
for(@a)
{pop @a;shift @a;}
print @a;