Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Tuesday, 13 August 2019

Linux - How to Copy, Cut, and Paste in Vi

In this article, we’ll show you how to copy, cut and paste in vi.
This will be explained both selecting text as using movement commands. We’ll also tell you how to copy and cut multiple chunks of text.

1. Introduction

To remember easily the commands used for copying and cutting in vi, it’s useful to know the words they come from:
  • d comes from delete, which you usually know as cut.
  • y comes from yank, which you usually know as copy.
  • p comes from paste, so nothing new here.
Keeping this in mind, you may find it easier to memorize the commands.

2. Selecting, Copying (or Cutting) and Pasting Text

We’ll start with normal mode and position the cursor at the start of the text that we want to copy. Then we’ll do the following steps:
  • Press v to start selecting.
  • You can also use V (capital v) to select whole lines or Ctrl+v to select rectangular blocks.
  • Move the cursor to the end of the text you want to copy.
  • Press y to copy the text (or d to cut it).
  • Move the cursor where you want to paste the text.
  • Press p to paste the text after the cursor (or P (capital p) to paste it before the cursor).

3. Copying and Cutting with Movement Commands

As well as you can copy and cut by selecting the text that you want to, you can also use movement commands to specify what text will be selected. This way you can do:
  • yy or `Y — copy the whole line (including the new line character).
  • y$ — copy from the cursor to the end of the line (excluding the new line character).
  • y0 — copy from the cursor to the start of the line.
  • yiw— copy the current word (excluding surrounding whitespace).
  • yaw— copy the current word (including surrounding whitespace).

4. Multiple Copying or Cutting

To copy or cut several chunks of text at the same time, you’ll need to use vi’s registers. A register is a location in Vim’s memory identified with a single letter. A double quote plus character is used to specify that the next letter typed is the name of a register (so "a would be the a register).
After this short explanation, for doing this you should type "ay and yank that word to the a register. Then you’ll be able to paste that word anywhere in the text using "ap" (or“aP` if you want to paste it before the cursor).

4.1. A Little More About Vi’s Registers

  • By default, all copying and cutting operations are stored in the unnamed registry "" (also named quotequote).
  • A double quote plus an uppercase character is used to append content to a register(with "Ay you would append a yanked text to the a register).
  • There are more default registers, some of them are:
  • "+ for Linux clipboard.
  • "* for Windows clipboard.
  • "0 will store the text of the last yank.
  • "1 will store the text from the last delete.
  • All current registers can be listed the list of all of them with the :reg command.

5. Conclusion

At this point, we’ve learned how to copy and cut text in vi.
To sum it up, you need to v select, y copy, d cut and p paste.

Friday, 9 August 2019

Grep for Windows – findstr example

I love grep command on Linux, it helped to search and filter strings easily, always wonder what is the equivalent tool on Windows, and found this findstr recently.
In this article, I will share some of my favorite “grep” examples on Linux, and how to “port” it to Windows with “findstr” command.

1. Filter a result

1.1 Classic example to filter a listing result.
#Linux
$ ls -ls | grep mkyong

#Windows
c:\> dir | findstr mkyong

1.2 Add ignore case, and filter the listing result with multiple strings.
#Linux - Need '-E' option and Uses "|" to separate multiple search strings.
$ ls -ls | grep -iE "mkyong|music"

#Windows - Use spaces to separate multiple search strings
c:\> dir | findstr -i "mkyong music"

2. Search a File

2.1 Search matched string in a file.
#Linux 
$ grep mkyong test.txt

#Windows
c:\> findstr mkyong test.txt

2.2 Counting the number of matches.
#Linux
$ grep -c mkyong test.txt

#Windows - Piped with find /c command.
c:\> findstr -N "mkyong" test.txt | find /c ":"

3. Search a list of files

3.1 Search matched string in a list of files.
#Linux
$ grep mkyong -lr /path/folder

#Windows
c:\> findstr /M mkyong c:\folder\*

* (grep) -l , (findstr) /M = print only name of files containing matches.

4. Help

4.1 The most powerful command ~
#Linux 
$ grep --help 
$ man grep 

#Windows
c:\> findstr -?

Thursday, 8 August 2019

Look for a string, word, or sentence in a file with Linux grep command, recursively

Introduction

Sometimes you remember a phrase or a given word or words, you put in a document, but you do not remember the name of the document. How to find it?, well do a search of all your documents, looking for that word or words or sentence, in other words, look for a string or strings.
Now, thanks to computers this is easier now, than it was in our parents' days.
If you are using Linux, you have grep to help on this job.

Look for a document, containing a given string

First the easy case, you know the exact sentence, you are looking for, and you at least remember the folder where the file is.
grep "sentence to look for" /home/user/docs/
Now, let's suppose you do not know if the sentence was in uppercase or in lowercase, so ask grep to ignore case.
grep -i "sentence to look for" /home/user/docs/
Well, let's suppose you have a lot of sub-folders, and you do not remember where your file is.
grep -r "sentence to look for" /home/user/docs/
Let's see an example of the output in my PC
grep -ri "Introduction" /home/user/post/
The output is:
/home/user/post/monthy-newsletter.txt:###Introduction###
/home/user/post/monthy-newsletter.txt:I'm no expert in MySQL, but anyway I have written three introduction-type MySQL posts, something we all need to know to start [how to create databases][1], [How to list those databases][2] and [How to create tables in MySQL][3]
/home/user/post/how-to-debug-bash-shell-scripts.txt:##Introduction##
/home/user/post/how-to-setup-dns-bind-master-slave-linux.txt:##Introduction
/home/user/post/interview-raphael-hertzog.txt:I also have plans for bigger changes concerning Debian, and among them is the introduction of Debian Rolling, a distribution similar to testing but with some design choices to make it more usable at any point in time.
/home/user/post/four-years-with-debian-testing.txt:##Introduction##
Binary file /home/user/post/.find-a-documents-with-given-string.txt.swp matches
/home/user/post/how-to-change-the-priority-of-Linux-processes.txt:##Introduction##
As you can see there are some binary files, also scanned, if we want to avoid that:
grep -riI "introduction" /home/user/post/
The output now is:
/home/user/post/monthy-newsletter.txt:###Introduction###
/home/user/post/monthy-newsletter.txt:I'm no expert in MySQL, but anyway I have written three introduction-type MySQL posts, something we all need to know to start [how to create databases][1], [How to list those databases][2] and [How to create tables in MySQL][3]
/home/user/post/how-to-debug-bash-shell-scripts.txt:##Introduction##
/home/user/post/how-to-setup-dns-bind-master-slave-linux.txt:##Introduction
/home/user/post/interview-raphael-hertzog.txt:I also have plans for bigger changes concerning Debian, and among them is the introduction of Debian Rolling, a distribution similar to testing but with some design choices to make it more usable at any point in time.
/home/user/post/four-years-with-debian-testing.txt:##Introduction##
/home/user/post/how-to-change-the-priority-of-Linux-processes.txt:##Introduction##
Finally, I just want the file names, and not the sentences where the sentence or word appears.
grep -riIl "introduction" /home/user/post/
The output will be:
/home/user/post/monthy-newsletter.txt
/home/user/post/how-to-debug-bash-shell-scripts.txt
/home/user/post/how-to-setup-dns-bind-master-slave-linux.txt
/home/user/post/interview-raphael-hertzog.txt
/home/user/post/four-years-with-debian-testing.txt
/home/user/post/how-to-change-the-priority-of-Linux-processes.txt

Linoux - How To Use awk In Bash Scripting

Print a Text File

awk '{ print }' /etc/passwd
OR
awk '{ print $0 }' /etc/passwd

Print Specific Field

Use : as the input field separator and print first field only i.e. usernames (will print the the first field. all other fields are ignored):
awk -F':' '{ print $1 }' /etc/passwd
Send output to sort command using a shell pipe:
awk -F':' '{ print $1 }' /etc/passwd | sort

Pattern Matching

You can only print line of the file if pattern matched. For e.g. display all lines from Apache log file if HTTP error code is 500 (9th field logs status error code for each http request):
awk '$9 == 500 { print $0}' /var/log/httpd/access.log
The part outside the curly braces is called the “pattern”, and the part inside is the “action”. The comparison operators include the ones from C:
== != < > <= >= ?:
If no pattern is given, then the action applies to all lines. If no action is given, then the entire line is printed. If “print” is used all by itself, the entire line is printed. Thus, the following are equivalent:
awk '$9 == 500 ' /var/log/httpd/access.log
awk '$9 == 500 {print} ' /var/log/httpd/access.log
awk '$9 == 500 {print $0} ' /var/log/httpd/access.log

Print Lines Containing tom, jerry AND vivek

Print pattern possibly on separate lines:
awk '/tom|jerry|vivek/' /etc/passwd

Print 1st Line From File

awk "NR==1{print;exit}" /etc/resolv.conf
awk "NR==$line{print;exit}" /etc/resolv.conf

Simply Arithmetic

You get the sum of all the numbers in a column:
awk '{total += $1} END {print total}' earnings.txt
Shell cannot calculate with floating point numbers, but awk can:
awk 'BEGIN {printf "%.3f\n", 2005.50 / 3}'

Call AWK From Shell Script

A shell script to list all IP addresses that accessing your website. This script use awk for processing log file and verification is done using shell script commands.
#!/bin/bash
d=$1
OUT=/tmp/spam.ip.$$
HTTPDLOG="/www/$d/var/log/httpd/access.log"
[ $# -eq 0 ] && { echo "Usage: $0 domain-name"; exit 999; }
if [ -f $HTTPDLOG ];
then
 awk '{print}' $HTTPDLOG >$OUT
 awk '{ print $1}' $OUT  |  sort -n | uniq -c | sort -n
else
 echo "$HTTPDLOG not found. Make sure domain exists and setup correctly."
fi
/bin/rm -f $OUT

AWK and Shell Functions

Here is another example. chrootCpSupportFiles() find out the shared libraries required by each program (such as perl / php-cgi) or shared library specified on the command line and copy them to destination. This code calls awk to print selected fields from the ldd output:
chrootCpSupportFiles() {
# Set CHROOT directory name
local BASE="$1"         # JAIL ROOT
local pFILE="$2"        # copy bin file libs
 
[ ! -d $BASE ] && mkdir -p $BASE || :
 
FILES="$(ldd $pFILE | awk '{ print $3 }' |egrep -v ^'\(')"
for i in $FILES
do
  dcc="$(dirname $i)"
  [ ! -d $BASE$dcc ] && mkdir -p $BASE$dcc || :
  /bin/cp $i $BASE$dcc
done
 
sldl="$(ldd $pFILE | grep 'ld-linux' | awk '{ print $1}')"
sldlsubdir="$(dirname $sldl)"
if [ ! -f $BASE$sldl ];
then
        /bin/cp $sldl $BASE$sldlsubdir
else
        :
fi
}
This function can be called as follows:
chrootCpSupportFiles /lighttpd-jail /usr/local/bin/php-cgi

AWK and Shell Pipes

List your top 10 favorite commands:
history | awk '{print $2}' | sort | uniq -c | sort -rn | head
Sample Output:
   172 ls
    144 cd
     69 vi
     62 grep
     41 dsu
     36 yum
     29 tail
     28 netstat
     21 mysql
     20 cat
Another example to find out domain expiry date:
$ whois cyberciti.com | awk '/Registry Expiry Date:/ { print $4 }'
Sample outputs:
2018-07-31T18:42:58Z

Awk Program File

You can put all awk commands in a file and call the same from a shell script using the following syntax:
awk -f mypgoram.awk input.txt

Awk in Shell Scripts – Passing Shell Variables TO Awk

You can pass shell variables to awk using the -v option:
n1=5
n2=10
echo | awk -v x=$n1 -v y=$n2 -f program.awk
Assign the value n1 to the variable x, before execution of the program begins. Such variable values are available to the BEGIN block of an AWK program:
BEGIN{ans=x+y}
{print ans}
END{}

How to install memcached into CPanel running CentOS

Installing libevent on CentOS

Run below commands to install libevent on CentOS. Use the latest libevent stable version.

wget https://github.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz
tar xvfz libevent-2.0.20-stable.tar.gz
cd libevent-2.0.20-stable
./configure
make
make install
Above commands will install libevent on your CentOS server.

Install Memcached server on CentOS

Run below commands to download latest Memcached server library and install it. Check their website to get the latest version.

wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
tar xvfz memcached-1.4.15.tar.gz
cd memcached-1.4.15
./configure
make
make install
Above steps will install Memcached server, now you can start it will following command.

memcached

Troubleshoot Tips

If you get following error “error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory” then you need to register libevent by following command.

export LD_LIBRARY_PATH= /usr/local/lib
You should keep above export command in your user profile (.bash_profile) so that you don’t need to export it always.
You might get an error message as “[memcached] can’t run as root without the -u switch” if you are using root user. You can’t run Memcached using root user. Use below command to start Memcached server.

Memcached Security Tip

Make sure to use -l option while starting Memcached server, so that only specific IPs can connect to the Memcached instance. If you won’t use this option, anybody who knows the IP and port of memcached can connect it using telnet, which is a security threat.

memcached -d -u nobody -p 11211
Now try to connect to memcached server using telnet and run some commands from memcached telnet commands to make sure its working fine.
Once you are satisfied that memcached server is running fine, go to the next step to install PHP Memcache extension.

Install PHP Memcached Extension on CentOS

Run below commands to install PHP Memcached extension and configure it. Make sure to use the latest stable version.

wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar xvfz memcache-2.2.7.tgz
cd memcache-2.2.7
phpize
./configure
make
make install
Above commands will install Memcached extension but to use it with PHP, we need to configure it in php.ini file and restart apache web server.

vi /usr/local/lib/php.ini
Add below line to the php.ini file.

extension=memcache.so
Check for extension_dir variable, if it’s not set in php.ini (most probably if you are installing any extension for the first time) then add following lines to provide directory location also. Make sure you change directory location as in your server.

extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613"
extension=memcache.so
Now your PHP configuration is done to load Memcached extension also, run below command to restart apache web server.

service httpd restart
Run below command to confirm that the Memcache module is loaded.

php -m

Test PHP Memcache Extension

To test whether everything is fine or not, just create a PHP file with following content.

<html>
<head>
  <title>PHP Memcache Extension Test</title>
</head>
<body>
<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
echo "Server's version: {$memcache->getVersion()}";
$tmp = new stdClass;
$tmp->string_attribute = 'JournalDev';
$tmp->string_attribute = 123;
$memcache->set('key', $tmp, false, 10) or die ("Failed to save temporary object at memcache server");
echo "Data from the cache:\n";
print_r($memcache->get('key'));
?> 
</body>
</html>
Now access this PHP file from browser and if you get output like this:

Server's version: 1.4.15Data from the cache: stdClass Object ( [string_attribute] => 123 )
It means that everything is fine. Memcached server is running fine and PHP Memcache module is able to connect and save/retrieve data into caching server.