0% found this document useful (0 votes)
133 views

Sed Awk Commands

This document provides examples of useful sed and awk commands for printing specific lines from files, deleting lines, filtering files by size, and adding or removing lines. It shows how to print only blank lines, the first and last line, all lines except the first, and delete all lines except the first using sed commands. It also demonstrates how to list only zero byte files, add a header and trailer to a file, and output even and odd numbered records to separate files using awk. Additionally, it includes sed commands for removing empty lines.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

Sed Awk Commands

This document provides examples of useful sed and awk commands for printing specific lines from files, deleting lines, filtering files by size, and adding or removing lines. It shows how to print only blank lines, the first and last line, all lines except the first, and delete all lines except the first using sed commands. It also demonstrates how to list only zero byte files, add a header and trailer to a file, and output even and odd numbered records to separate files using awk. Additionally, it includes sed commands for removing empty lines.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Sed/Awk Top Commands(Unix interview questions)

In this post i will mention very useful sed/awk commands,These are quite handy i
n day to day work and also useful in unix interview.

How to Print only Blank Line of File.

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

To Print First and Last Line using Sed Command
sed -n 1p Test_file.txt

sed n $p Test_file.txt

To Print all line Except First Line
sed n 1!p Test_file.txt

Delete all Line except First Line
sed n 1!d Test_file.txt

How to get only Zero Byte files which are present in the directory

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

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

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

How to display Even number of records into one file and Odd number of records in
to another file
awk 'NR %2 == 0' test_files.txt

awk 'NR %2 != 0' test_files.txt
Remove all empty lines:
sed '/^$/d' test_file.txt

sed '/./!d' test_file.txt

You might also like