Tuesday, 16 November 2021

Unix Sed and awk command - Part 1

 1) How to Print only Blank Line of File.

sed -n '/^$/p' data_file.txt


2) To Print all lines Except First Line

sed –n ‘1!p’ data_file.txt


3) To Print First and Last Line using Sed Command

sed  -n ‘1p’ data_file.txt

sed –n ‘$p’ data_file.txt


4) How to get only Zero Byte files that are present in the directory

ls -ltr| awk '/^-/ { if($5 ==0) print $9 }'


5) How to add a First record and Last Record to the current file in Linux

sed -i -e '1i Header' -e '$a Trailor' data_file.txt


6) Delete all Line except First Line

sed –n ‘1!d’ data_file.txt


7) How to display an Even number of records into one file and Odd number of records into another file

awk 'NR %2 == 0' data_file.txt

awk 'NR %2 !=  0' data_file.txt


8) Add at the start of the line 

awk '{print "START"$0}' FILE 


9) Add at end of the line 

awk '{print $0"END"}' FILE 


10)Remove all empty lines:

sed '/^$/d' data_file.txt or sed '/./!d' data_file.txt


11) To see a particular line

For example, if you just want to see the 180th line in a file

 sed -n '180p' testfile.txt


12) To find a particular column in the file 

cat data_file.txt |awk -F"," '{print $2}'


13) To rename the file with the current date 

mv test test_`date +%Y-%m-%d


14) Command to take out all those lines which are having 8 at 17th position 

grep '^.\{16\}8' data_file.txt > modifeid_data_file.txt


15) To remove the nth line without opening the file 

sed 'nd' file1>file2 to remove multiple lines sed -e 1d -e 5d


16) To find the top 20 files with the most space

ls -ltr|awk -F" " '{print $5 $9}' sort -n tail -20


17) To find the record in the first file, not in the second 

comm -13 data_file.txt data_file2.txt


18) If you are looking from something that is contained in a file but you don't know which directory it is in do the following:

find. -name "*" xargs grep -i something This will find all of the files in the directory and below and grep for the string something in those files!


19) Delete Files Delete all the files starting with the name data_file 

find. -type f -name "data_file*" exec rm -f {} \;


20) Remove blank space from the file 

sed -e "s/ *//g" data_file.txt >data_file.txt_wo_space


21) How to do grep on a large number of files in a directory 

grep -rl "Search Text" /tmp


22) Get all the columns from the file and record the count for each of the columns?

head -n 1 file.name | awk -F'|' '{print NF; exit}'

No comments:

Post a Comment

how to create dml dynamically in Ab-initio

 $[ begin let int i = 0; let string(int) complete_list = "emp_nm,billable,age,designation,location"; let string(int) file_content ...