Connect with us

Home

Sed command with examples

Published

on

Sed stands for stream editor. A stream editor performs the text transformation from input stream and the input stream may be a file or input from a pipeline.

#cat editing.txt
hi
hello
welcome
how are you
hope you are fine

. . .Sed command to print lines. . .

1.#sed ’1p’ editing.txt
hi
hi
hello
welcome
how are you
hope you are fine

==> 1p which tells that print the first line and show the remaining lines. so we can see from the output hi is priented twice.

2.#sed -n ’1p’ editing.txt
hi

===> -n options is nothing but noprint, (i.e) It prints only the first line and didn’t show the other lines.

3.#sed -n ’2,4p’ editing.txt
hello
welcome
how are you

===> The above command is used to print only a certain range of lines.

4.#sed -n ‘$p’ editing.txt
hope you are fine

===> The above command displays only the last line. The ‘$’ symbol indicated the last line and ‘-n’is used for no print and it combines with option ‘p’ to displays only the last line .

. . .Sed Command to delete lines. . .

1. #sed ’1d’ editing.txt

hello
welcome
how are you
hope you are fine

===> The above command used to delete only the fisrt line and display the remaining lines.

2. #sed ’2,4d’ editing.txt
hi

===> The above command is used to delete a range of lines.

. . .Sed Command for search and replace. . .

1.#sed ‘s/hi/changed/’ editing.txt
changed
hello
welcome
how are you
hope you are fine

==> The above command search for hi and replace to changed and print the remaining lines

2.#sed -n ‘s/hi/changes/p’ editing.txt
changes

===> The above command search for hi and replace to changed and don’t print the remaining lines

3.#sed ’1,4s/are/is/’ editing.txt
hi
hello
welcome
how is you
hope you are fine

===> The above command used to do search and replace only for a range of lines.

. . .How to delete empty lines using sed command. . .

#cat new_file
hi

The above line is empty

1.#cat new_file | sed -e ‘/^$/d’
hi
The above line is empty

2.#sed -e ‘/^$/d’ new_file
hi
The above line is empty

===> The above two commands produce the same results, It search for empty line and delete it. ‘^’ is nothing but starting of the line and ‘$’ tells the end of the line, so from the starting to ending of the line is empty means delete it.

. . .How to remove space from a word. . .

#cat remove.txt
s p a c e.

#cat remove.txt | sed ‘s/ //g’
space

# sed ‘s/ //g’ remove.txt
space

===> It nothing but search and replace, the above command search for empty space and replace it with nothig, so then space becomes nospace.

. . .How to remove a lines permanently from a file. . .

#cat new_file
hi

The above line is empty

#sed -i ’2d’ new_file

===> This above command will delete the line number 2 permanently from the file new_file.

. . .How to assign numbers for lines using sed command. . .

#cat editing.txt
hi
hello
welcome

#sed ‘=’ editing.txt
1
hi
2
hello
3
welcome

===> In the above command ‘=’ symbol is used to assign number for each lines, it works like as same as ‘nl’ command.

. . .How to use Word Boundaries using sed command. . .

Word Boundaries – \<\>

#cat first_text
raj
rajkumar
i am rajkumar

#sed -e ‘s/\<raj\>/kumar/g’ first

kumar
kumarkumar
i am kumarkumar

===> Normally if u use search and replace it will replace any word which contains raj to kumar, if ‘g’ is specified.

#sed -e ‘s/\/kumar/g’ first_text
kumar
rajkumar
i am rajkumar

===> If u use Word Boundaries means the only exact work will be searched and get replaced.(Its same like as ‘grep -w’ command)

. . .How to include files in sed command. . .

#cat commands
raj
linux

#vim lol
{
s/raj/kumar/g
s/linux/linuzzzzzzzz/g
}

#sed -f lol commands
kumar
linuzzzzzzzz

===> The ‘ -f ‘ option is used to include files in command prompt.

Continue Reading

Home

Protect the web directories by user authentication in apache

Published

on

By

Directory Password Protection 

In order to restrict the clients from accessing the web directory of a server and to allow only certain users, apache uses the Directory Password Protection Method.

How to Set Password Protection to directory ?

In Apache the .htaccess (Name of the Distributed Configuration file) file is placed under the Directory that we need to protect. To configure this directory password protection follow the below steps.

 Step 1:

In the Apache configuration file httpd.conf, add an entry in Virtualhost container to secure the protected directories.

<VirtualHost 192.168.1.2:80>
Servername 192.168.1.2
DocumentRoot /var/www/html
AccessFileName .htaccess
<Directory /var/www/html/private >
AllowOverride All
</Directory>
</VirtualHost>

In the above configuration we need to protect the /var/www/html/private web directory. The .htaccess is the distributed file name placed under the protected directory, we can change this file name as per our need by assign any name in AccessFileName directive. In the Directory container we should mention the absolute path of protected directory, Inside the Directory container the All option in AllowOverride will allow all the possible configuration directives in the .htaccess file.

Note: If AllowOverride is set to none then the apache ignored the .htaccess file

Step 2:

Now Add a valid authorized user for the protected web directory by using htpasswd command

# htpasswd -c /var/www/html/private/.htpasswd <username>

The htpasswd command is used to generate the username and password for user authentication in protected directory. The option -c is used to create the password file (.htpasswd) if it does not exist.

Step 3:

Now configure the in .htaccess file which is placed under the protected directory “/var/www/html/private/.htaccess” as below.

AuthType Basic
AuthName “Private User Access”
AuthUserFile /var/www/html/private/.htpasswd
Require valid-user
Order Allow,Deny
Allow from all

In this above configuration entered in .htaccess file, the AuthType is configured as Basic, which defines the type of user authentication for the directory. The AuthName defines name of the authorization realm for a directory. The authenticated user password file stored location is denoted by AuthUserFile directive. The Require directive defines the permitted user to access the protected directory. we can also deny or allow from which client IP should access the protected IP by Allow and Deny Directive.

Note: make the .htaccess and .htpasswd file hidden from clients for security purpose.

Output :

All the configurations are over. Now just restart the apache service and call the configured virtual host protected web directory by browser.

 

Continue Reading

Home

Shell Script to add users and corresponding password automatically

Published

on

By

. . .List of Users to be Added . . .
#cat list
ravi
deepak

. . .Script to automate User and their Password Adding . . .
#vim auto.sh
#!bin/bash
cat /dev/urandom | tr -cd “a-zA-Z0-91234567890-=\`” | fold -w 9 | head -n 2 > pass
cp pass check
u=`cat list`
for j in $u
do
useradd $j
echo “User $j Added”
echo “==================================”
done
for i in $u
do
echo “User Name is :$i”
p=`cat pass`
echo “$p” | passwd –stdin “$i”
sed -i ’1d’ /home/rajm/script/blog/auto/pass
#sed -i ’1d’ $p
echo “User $i ’s password changed!”
echo “==============================”
done

. . .Give permisson and run the script. . .
#chmod +x auto.sh
sh auto.sh
User ravi Added
==================================
User deepak Added
==================================
User Name is :ravi
Changing password for user ravi.
passwd: all authentication tokens updated successfully.
User ravi ’s password changed!
==============================
User Name is :deepak
Changing password for user deepak.
passwd: all authentication tokens updated successfully.
User deepak ’s password changed!
==============================

. . .The File check used to check the password. . . .
#cat check
5<YX<jR0I
=tAn`0;mt

Continue Reading

Home

Symantec Antivirus source code got stolen,Hackers demanded $50,0000 to keep the source code private

Published

on

By

The Famous Antivirus Company Symantec source code got stolen by hackers. The Hackers Group Demanding $50,0000 to keep the source code private.

According to the email correspondence between the company and a hacker, Symantec has confirmed that more than a gigabyte of source code from their company had been stolen by the Hackers. Symantec had also revealed that the hackers had stolen the source code of 2006 versions along with other Symantec products from the same time period, and warned their users of PCAnywhere to disable its functionality until they patched the program earlier this month. The email exchange between the company and the Hacker is given below

“We will pay you $50,000.00 USD total,” Thomas said in an e-mail dated last Thursday. “However, we need assurances that you are not going to release the code after payment. We will pay you $2,500 a month for the first three months. Payments start next week. After the first three months you have to convince us you have destroyed the code before we pay the balance. We are trusting you to keep your end of the bargain.”

Symantec conducted an internal investigation into this incident and contacted law enforcement, given the attempted extortion and apparent theft of intellectual property.

The Hackers group also threaten the Company that if they detect any malevolent tracing action, they cancel the deal and put the source code on sale.

The company confirmed that “old” source code stolen by the hackers had exposed vulnerabilities in the program which allows remote access to computers. Other programs affected include Norton Antivirus Corporate Edition, Norton Internet Security and Norton System works (Norton Utilities and Norton Go Back).

However, only PC Anywhere is said to be at risk, until the releasing of  patches for PC  Anywhere by Symantec.

Continue Reading

Trending

Copyright © 2022 The Technical Stuff.