sed search a range and print first set
awk '/myname/{if(b) exit; else b=1}1' filename
$ cat temp.txt
myname
_ something
_ something
_ something
myname
_ something
_ something
myname
_ something
$ awk '/myname/{if(b) exit; else b=1}1' temp.txt
myname
_ something
_ something
_ something
Using sed to print range when pattern is inside the range?
You haven't said what an error message looks like, so I'll assume it contains the word "ERROR":
sed -n '/^MSG.*ERROR/{H;g;N;p;};/^DURATION/{s/.*//;h;d;};H' < logname
(I wish there were a tidier way to purge the hold space. Anyone?...)
How to print a range of lines with sed except the one matching the range-end pattern?
There might be better ways but I could come up with this sed 1 liner:
sed -rn '/ABC/,/^[^+]/{/(ABC|^\+)/!d;p;}' file
Another sed 1 liner is
sed -n '/ABC/,/^[^+]/{x;/^$/!p;}' file
One more sed 1 liner (and probably better)
sed -n '/ABC/I{h;:A;$!n;/^+/{H;$!bA};g;p;}' file
get range out using sed or awk, only if given pattern match
$ awk 'last~/START/ && /LINUX/ {print last; f=1} f{print} /END/{f=0} {last=$0}' file
START
OS:: LINUX
Release: xxx
Version: xxx
END
This approach will work even if there are lines in the input that are outside of START-END blocks.
How it works
last~/START/ && /LINUX/ {print last; f=1}
If the last line matches
START
and the current line matchesLINUX
, then print the last line and set variablef
to 1.f{print}
If
f
is nonzero, print the current line./END/{f=0}
If the current line matches
END
, the setf
to zero.last=$0
Save the current line in the variable
last
.
How can I extract a predetermined range of lines from a text file on Unix?
sed -n '16224,16482p;16483q' filename > newfile
From the sed manual:
p -
Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option.n -
If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If
there is no more input then sed exits without processing any more
commands.q -
Exitsed
without processing any more commands or input.
Note that the current pattern space is printed if auto-print is not disabled with the -n option.
and
Addresses in a sed script can be in any of the following forms:
number
Specifying a line number will match only that line in the input.An address range can be specified by specifying two addresses
separated by a comma (,). An address range matches lines starting from
where the first address matches, and continues until the second
address matches (inclusively).
sed — joining a range of selected lines
One way using GNU sed
:
sed -n '/begin/,/end/ { H;g; s/^\n//; /end/s/\n/ /gp }' file.txt
sed, awk or perl: Pattern range match, print 45 lines then add record delimiter
I think I understand what you want. Not sure about the bit about pull each record by pattern range. Is #matchee
always followed by a blank line and then the department line? So in fact record number 2?
This Perl fragment does what I understand you need.
If you prefer you can put the input file on the command line and drop the open
call. Then the loop would have to be while (<>) { ... }
.
Let us know if this is right so far, and what more you need from it.
use strict;
use warnings;
open my $fh, '<', 'mess-o-records.txt' or die $!;
my $count = 0;
while (<$fh>) {
if (/^#matchee/) {
print;
$count = 0;
}
else {
print if $count++ < 45;
}
}
Related Topics
Hugo Version Not Updating to Latest
Interpreting Openssl Speed Output for Rsa with Multi Option
Amazon Ses on Google Cloud Computing Instance Vm Using Postfix
Host Doing Unnecessary Dns Lookup for Localhost
How to Generate Files in a Docker Container for Having The Same Owner as The Host's User
How to Wget The More Recent File of a Directory
Why Pid of a Process Is Represented by Opaque Data Type
How to Solve Ssh: /Usr/Lib64/Libcrypto.So.10: No Version Information Available
Bash: Find and Replace Using Parameter Expansion
Curl Error "No Alternative Certificate.."
Numa-Aware Named Shared Memory for Linux
How to Use Find on Dirs with White Spaces
Wget Breaking with Content-Disposition
How Is Numa Represented in Virtual Memory
Process Control Block in Linux
Gradle 1.3: Build.Gradle Not Building Classes
Jmeter:Difference Between Jmeter.Sh and Jmeter Without Extension File in Jmeter