Grep for next blank line after line number
Use sed
for this, this should do the trick:
sed -n '1,/^\s*$/p' file
Just replace the first number before the comma, in this case 1
with the line number, demo to print each table from a given line number:
$ cat file
one
two
three
five
six
seven
nine
ten
eleven
$ sed -n '1,/^\s*$/p' file
one
two
three
$ sed -n '5,/^\s*$/p' file
five
six
seven
$ sed -n '9,/^\s*$/p' file
nine
ten
eleven
Using the -n
option to turn of default printing of every line and the p
flag sed
prints from the line number to the first line that matches the regexp where:
^ # Matches the start of the line
\s* # Matches zero or more whitespace characters
$ # Matches the end of the line
Using the format sed -n 'A,Bp'
where A
and B
can be either lines numbers or regular expression you can print subsections of files easily.
To print just the line number of the next blank line with sed
do:
$ sed -n '1,/^\s*$/{=}' file | tail -1
4
$ sed -n '5,/^\s*$/{=}' file | tail -1
8
$ sed -n '9,/^\s*$/{=}' file | tail -1
12
Or just printing where all the blanks lines are
$ sed -n '/^\s*$/{=}' file
4
8
12
Getting the next blank line number with awk
doesn't require using tail
:
$ awk 'NR>=1 && /^\s*$/{print NR;exit}' file
4
$ awk 'NR>=5 && /^\s*$/{print NR;exit}' file
8
$ awk 'NR>=9 && /^\s*$/{print NR;exit}' file
12
$ awk '/^\s*$/{print NR}' file
4
8
12
If it makes it clearer for you, you can pass a variable in with awk
using -v
$ awk -v start=1 'NR>=start && /^\s*$/{print NR;exit}' file
4
$ awk -v start=5 'NR>=start && /^\s*$/{print NR;exit}' file
8
$ awk -v start=9 'NR>=start && /^\s*$/{print NR;exit}' file
12
Obtain all the lines before and after a pattern match until a there is blank line
Use awk's paragraph mode (one or more empty lines act as record separator)
$ awk -v RS= '/ACCCC/' ip.txt
ABASLDKJ
ASDASKKK
ASDASDAS
ACCCC
ASDASDAS
ASDASDAS
-v
command line option helps to set value to a variableRS
is input record separator, whose default value is newline character
From awk manual
By a special dispensation, an empty string as the value of RS
indicates that records are separated by one or more blank lines. When
RS is set to the empty string, each record always ends at the first
blank line encountered. The next record doesn’t start until the first
nonblank line that follows. No matter how many blank lines appear in a
row, they all act as one record separator. (Blank lines must be
completely empty; lines that contain only whitespace do not count.)
How to print only the line which contains a blank lines before and after
With your shown samples, could you please try following.
awk -v RS="" 'NF==1' Input_file
tuv0657
tuv2330
tuv1049
tuv2101
Match empty lines in a file with 'grep'
The regular expression to match the end of the line is $
, not \n
(since grep
works a line at a time, it ignores the newlines between lines).
grep -c '^$' myfile.txt
grep: show lines surrounding each match
For BSD or GNU grep
you can use -B num
to set how many lines before the match and -A num
for the number of lines after the match.
grep -B 3 -A 2 foo README.txt
If you want the same number of lines before and after you can use -C num
.
grep -C 3 foo README.txt
This will show 3 lines before and 3 lines after.
Related Topics
Bash Variable Assignment Not Working Expected
How to Write Multiple Line String Using Bash with Variables
Find All Zero-Byte Files in Directory and Subdirectories
Preserving File Permissions for Samba Shares When File Is Edited
Linux, Why Can't I Write Even Though I Have Group Permissions
Get Current Time in Hours and Minutes
Forcing Driver to Device Match
Best Way to Divide in Bash Using Pipes
I Want to Remove Multiple Line of Text on Linux
Force Unmount of Nfs-Mounted Directory
How to Get Diff Between All Files Inside 2 Folders That Are on the Web
Linux Free Shows High Memory Usage But Top Does Not
Getting Stacktrace from Core Dump
How to Check Syslog in Bash on Linux