Print previous line if condition is met
This can be a way:
$ awk '$1=="BB" && $2>1 {print f} {f=$1}' file
AAAAAAAAAAAAA
Explanation
$1=="BB" && $2>1 {print f}
if the 1st field is exactlyBB
and 2nd field is bigger than1
, then printf
, a stored value.{f=$1}
store the current line inf
, so that it is accessible when reading the next line.
Keep current and previous line only if current line fulfills a given condition
Yet another awk
solution:
awk '/^>/ { header = $0; next } length > 50 && length <= 800 { print header ORS $0 }'
Print previous line along with matched line
You are on right path, You forgot to print the current line simply do.
awk '
/pattern/{
if(prev) { print prev ORS $0 }
if(FNR==1){ print }
next
}
{
prev=$0
}
' Input_file
I have also taken care of 1st first line here, in case pattern comes in very first line then it will simply print that line, since there is no previous line.
Explanation: Adding detailed explanation for above.
awk ' ##Starting awk program from here.
/pattern/{ ##Checking for specific pattern here.
if(prev) { print prev ORS $0 } ##Checking if prev is NOT NULL then print prev newline current line.
if(FNR==1){ print } ##Checking if its first line then simply print the line.
next ##next will skip all further statements from here.
}
{
prev=$0 ##Setting prev to current line here.
}
' Input_file ##Mentioning Input_file name here.
print a previous line based on a condition in python
Use tee
to run a pair of iterators across inf
. This only stores five lines in memory at any given time:
from itertools import tee
with open("SFU.txt") as inf:
# set up iterators
cfg,res = tee(inf)
# advance cfg by four lines
for i in range(4):
next(cfg)
for c,r in zip(cfg, res):
if "configuration" in c:
print(r)
and, as expected, results in
This is planet Mars
Edit: if you want to edit the -4th line, I suggest
def edited(r):
# make your changes to r
return new_r
with open("SFU.txt") as inf, open("edited.txt", "w") as outf:
# set up iterators
cfg, res = tee(inf)
for i in range(4):
next(cfg)
# iterate through in tandem
for c, r in zip(cfg, res):
if "configuration" in c:
r = edited(r)
outf.write(r)
# reached end - write out remaining queued values
for r in res:
outf.write(r)
Python to print previous line if an condition met in current line
When iterating over the lines, can't you just keep the previous line in each iteration?
# Holds the previous line
prev_line = None
for line in open(<something>, 'r'):
if is_ora_line(line) and prev_line is not None:
do_something_with_database_line(prev_line)
# Remember now the current line as the previous line
prev_line = line
Match a line and return a previous line before the match containing a pattern
This sed command should extract it:
sed -n '
/^Id: / { # If the line starts with "Id: "
s/// # Remove the "Id: "
h # Store what is left in the hold space
}
/^ email: '"$email"'/ { # If the line starts with " email: " plus the email
x # Swap pattern and hold space
p # Print pattern space
q # Stop processing
}
' infile
where $email
is the shell variable containing the escaped version of test_1@somedomain.com
:
raw='test_1@somedomain.com'
email=$(sed 's|[]/.*^$\[]|\\&|g' <<< "$raw")
This escapes the sed special characters .*/^$[]\
.
Or, more compact:
sed -n '/^Id: /{s///;h};/^ email: '"$email"'/{x;p;q}' infile
macOS sed requires an extra ;
before each closing }
.
And yes, it's probably easier with awk /p>
move short lines to previous line
Here is one way with awk
. It delays printing until it saw the next line.
Setting FS=""
works with gawk 4.2.1 and above and mawk 1.3.4 20200120.
Fails with "original" awk version 20121220. (Thanks @Ed Morton for the reminder!). Other versions not tested.
% awk -F '' 'NR==1{ line=$0; next }
NF<=60{ prev=$0; printf("%s%s",line,prev); line=""; next }
NF>60{ print line } { line=$0 }
END{ print line }' file
1 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw2 owenuof wuoef wue fiwuf wiuenf wie
3 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw
4 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw5 owenuof wuoef wue fiwuf wiuenf wie6 owenuof wuoef wue fiwuf wiuenf wie
7 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw
8 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw
With "original" awk using length()
% oawk 'NR==1{ line=$0; next }
length($0)<=60{ prev=$0; printf("%s%s",line,prev); line=""; next }
length($0)>60{ print line } { line=$0 }
END{ print line }' file
1 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw2 owenuof wuoef wue fiwuf wiuenf wie
3 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw
4 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw5 owenuof wuoef wue fiwuf wiuenf wie6 owenuof wuoef wue fiwuf wiuenf wie
7 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw
8 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw
Data
% cat file
1 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw
2 owenuof wuoef wue fiwuf wiuenf wie
3 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw
4 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw
5 owenuof wuoef wue fiwuf wiuenf wie
6 owenuof wuoef wue fiwuf wiuenf wie
7 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw
8 ewf nwje fwkjef wkejf wkej fwkejf wkejfwkjef woief nowienfw
calculate current line column less previous line column using awk
You can try this awk
$ awk 'NR==1{ print $0 } NR>1{ print $0,$2 - pre } { pre=$2 }' file
a 9
b 2 -7
c 5 3
d 3 -2
e 7 4
Related Topics
Linux Kernel Config_Debug_Section_Mismatch Make Errors
What Would It Take to Make Windows a Posix Compliant Operating System Out of The Box
How to Set My Application's Desktop Icon for Linux: Kde, Gnome etc
Why Ulimit Can't Limit Resident Memory Successfully and How
Invalid Argument" Setting Key "Net.Core.Somaxconn"
Append The Time Stamp to a File Name in Ubuntu
Smbclient - Send All Files in Directory
Linux Os: /Proc/[Pid]/Smaps Vs /Proc/[Pid]/Statm
Understanding Tcpdump Filter & Bit-Masking
Manage Source Under Git and Svn Simultanously - Does It Make Sense
Does Kernel's Panic() Function Completely Freezes Every Other Process
How to Gain Root Permission Without Leaving Vim
Does Socket Io Involve Disk Io
Sed Replacement Not Working When Using Variables