Saturday, February 17, 2024

SED COMMAND ON LINUX




to view file content with line number:-
cat -n file.txt 


-- to delete last line in the file :-
sed -i '$d' filename 
-- to delete first line in the file :-
sed -i '1d' filename 


-- to append something 

[root@localhost ~]# sed -i '$a\
> export ORACLE_HOME=/u02/ogg19c\
> export PATH=$ORACLE_HOME/bin:$PATH\
> export LD_LIBRARY_PATH=$ORACLE_HOME/lib
> ' bash_profile
[root@localhost ~]# cat bash_profile
india
is my country
export ORACLE_HOME=/u02/ogg19c
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib

in above :- $ taken for last line of the file so append in the last.


-- to delete lines from file using sed command 
[root@localhost ~]# cat -n bash_profile
     1  india
     2  is my country
     3  export ORACLE_HOME=/u02/ogg19c
     4  export PATH=$ORACLE_HOME/bin:$PATH
     5  export LD_LIBRARY_PATH=$ORACLE_HOME/lib
[root@localhost ~]# # delete line number ---> 3 and 5 using below 
[root@localhost ~]# sed -i '3,5d' bash_profile
[root@localhost ~]#
[root@localhost ~]# cat -n bash_profile
     1  india
     2  is my country

No comments:

Post a Comment

Enable OpenSSH on Windows 11

Step 1: Install OpenSSH Server You can do this via PowerShell (run as Administrator ): Check if it's already available: Get-WindowsCapab...