print field before and after matching pattern of single linePrint nth line before the matched line, Matching line and nth line from the matched linePattern Matching and Delete the whole lineInsert new line after non matching patternreplace pattern matchingFind each line matching a pattern but print only the line above itInsert a line break before a numeric field or before an alphanumeric field that is just after a numeric fieldHow to add quotes to line break field values in a fileSed or awk - Insert a new line after Matching patternCompare data in 5th column with 6th column and print the least difference elementHow to get single character after space?
Not been paid even after reminding the Treasurer; what should I do?
Plato and the knowledge of the forms
Is there a way to prevent the production team from messing up my paper?
Did Captain America make out with his niece?
Based on what criteria do you add/not add icons to labels within a toolbar?
A verb for when some rights are not violated?
Is it double speak?
Probably terminated or laid off soon; confront or not?
Repeated! Factorials!
Tile the chessboard with four-colored triominoes
Vibration on the guitar when playing two strings
Is there a way to improve my grade after graduation?
Could an areostationary satellite help locate asteroids?
Nested Unlocked Packages requires Installation of Base Packages?
The Game of the Century - why didn't Byrne take the rook after he forked Fischer?
Why is the Vasa Museum in Stockholm so Popular?
How easy is it to get a gun illegally in the United States?
Why private jets such as GulfStream ones fly higher than other civil jets?
What's going on with an item that starts with an hbox?
How do I get the =LEFT function in excel, to also take the number zero as the first number?
Is a switch from R to Python worth it?
Definitional equality of two propositions about propositional equality
If the interviewer says "We have other interviews to conduct and then back to you in few days", is it a bad sign to not get the job?
How many years before enough atoms of your body are replaced to survive the sudden disappearance of the original body’s atoms?
print field before and after matching pattern of single line
Print nth line before the matched line, Matching line and nth line from the matched linePattern Matching and Delete the whole lineInsert new line after non matching patternreplace pattern matchingFind each line matching a pattern but print only the line above itInsert a line break before a numeric field or before an alphanumeric field that is just after a numeric fieldHow to add quotes to line break field values in a fileSed or awk - Insert a new line after Matching patternCompare data in 5th column with 6th column and print the least difference elementHow to get single character after space?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
My file contains the data below:
tail fn0 logfile
more tail3 fn0 logfile1
get than tail4 fn0 logfile2
I want to get output from the field before fn0 and the field after fn0.
Expected output:
tail logfile
tail3 logfile1
tail4 logfile2
linux text-processing
New contributor
add a comment |
My file contains the data below:
tail fn0 logfile
more tail3 fn0 logfile1
get than tail4 fn0 logfile2
I want to get output from the field before fn0 and the field after fn0.
Expected output:
tail logfile
tail3 logfile1
tail4 logfile2
linux text-processing
New contributor
add a comment |
My file contains the data below:
tail fn0 logfile
more tail3 fn0 logfile1
get than tail4 fn0 logfile2
I want to get output from the field before fn0 and the field after fn0.
Expected output:
tail logfile
tail3 logfile1
tail4 logfile2
linux text-processing
New contributor
My file contains the data below:
tail fn0 logfile
more tail3 fn0 logfile1
get than tail4 fn0 logfile2
I want to get output from the field before fn0 and the field after fn0.
Expected output:
tail logfile
tail3 logfile1
tail4 logfile2
linux text-processing
linux text-processing
New contributor
New contributor
edited 10 hours ago
Jeff Schaller♦
48.5k11 gold badges72 silver badges162 bronze badges
48.5k11 gold badges72 silver badges162 bronze badges
New contributor
asked 11 hours ago
rahulrahul
111 bronze badge
111 bronze badge
New contributor
New contributor
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
You can use awk
:
awk -v pattern="fn0" 'for (i=0;i<=NF;i++) if ($i==pattern) print $(i-1),$(i+1) ' file
or if you want to use a regex pattern:
awk -v pattern="^fn0$" 'for (i=0;i<=NF;i++) if ($i~pattern) print $(i-1),$(i+1) ' file
Output:
tail logfile
tail3 logfile1
tail4 logfile2
add a comment |
With perl
$ perl -lane '($i) = grep $F[$_] eq "fn0" 0..$#F;
print "$F[$i-1] $F[$i+1]"' ip.txt
tail logfile
tail3 logfile1
tail4 logfile2
-l
will remove newline character from input line, and add it back while printing-a
split input line based on whitespaces,@F
array will have the data($i) = grep $F[$_] eq "fn0" 0..$#F
get index of the element whose exact content isfn0
print "$F[$i-1] $F[$i+1]"
print the required fields
With sed
that supports ERE
$ sed -E 's/^(.* )?([^ ]+) fn0 ([^ ]+).*/2 3/' ip.txt
tail logfile
tail3 logfile1
tail4 logfile2
^(.* )?
optional fields at start of line([^ ]+) fn0 ([^ ]+)
capture fields before and afterfn0
(assuming single space as field separator).*
rest of the line2 3
required fields in output
add a comment |
a solution with grep and sed:
egrep -oh '[a-zA-Z0-9]+ fn0 [a-zA-Z0-9]+' testfile | sed 's/ fn0 / /'
New contributor
add a comment |
$ sed 's/.*(tail[[:digit:]]*) fn[[:digit:]]*/1/' file
tail logfile
tail3 logfile1
tail4 logfile2
The sed
expression is replaces everything on each line up to some substring matching tailXX fnYY
with tailXX
(where XX
and YY
are some positive integers).
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
rahul is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f534129%2fprint-field-before-and-after-matching-pattern-of-single-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use awk
:
awk -v pattern="fn0" 'for (i=0;i<=NF;i++) if ($i==pattern) print $(i-1),$(i+1) ' file
or if you want to use a regex pattern:
awk -v pattern="^fn0$" 'for (i=0;i<=NF;i++) if ($i~pattern) print $(i-1),$(i+1) ' file
Output:
tail logfile
tail3 logfile1
tail4 logfile2
add a comment |
You can use awk
:
awk -v pattern="fn0" 'for (i=0;i<=NF;i++) if ($i==pattern) print $(i-1),$(i+1) ' file
or if you want to use a regex pattern:
awk -v pattern="^fn0$" 'for (i=0;i<=NF;i++) if ($i~pattern) print $(i-1),$(i+1) ' file
Output:
tail logfile
tail3 logfile1
tail4 logfile2
add a comment |
You can use awk
:
awk -v pattern="fn0" 'for (i=0;i<=NF;i++) if ($i==pattern) print $(i-1),$(i+1) ' file
or if you want to use a regex pattern:
awk -v pattern="^fn0$" 'for (i=0;i<=NF;i++) if ($i~pattern) print $(i-1),$(i+1) ' file
Output:
tail logfile
tail3 logfile1
tail4 logfile2
You can use awk
:
awk -v pattern="fn0" 'for (i=0;i<=NF;i++) if ($i==pattern) print $(i-1),$(i+1) ' file
or if you want to use a regex pattern:
awk -v pattern="^fn0$" 'for (i=0;i<=NF;i++) if ($i~pattern) print $(i-1),$(i+1) ' file
Output:
tail logfile
tail3 logfile1
tail4 logfile2
edited 9 hours ago
answered 9 hours ago
pLumopLumo
7,20814 silver badges32 bronze badges
7,20814 silver badges32 bronze badges
add a comment |
add a comment |
With perl
$ perl -lane '($i) = grep $F[$_] eq "fn0" 0..$#F;
print "$F[$i-1] $F[$i+1]"' ip.txt
tail logfile
tail3 logfile1
tail4 logfile2
-l
will remove newline character from input line, and add it back while printing-a
split input line based on whitespaces,@F
array will have the data($i) = grep $F[$_] eq "fn0" 0..$#F
get index of the element whose exact content isfn0
print "$F[$i-1] $F[$i+1]"
print the required fields
With sed
that supports ERE
$ sed -E 's/^(.* )?([^ ]+) fn0 ([^ ]+).*/2 3/' ip.txt
tail logfile
tail3 logfile1
tail4 logfile2
^(.* )?
optional fields at start of line([^ ]+) fn0 ([^ ]+)
capture fields before and afterfn0
(assuming single space as field separator).*
rest of the line2 3
required fields in output
add a comment |
With perl
$ perl -lane '($i) = grep $F[$_] eq "fn0" 0..$#F;
print "$F[$i-1] $F[$i+1]"' ip.txt
tail logfile
tail3 logfile1
tail4 logfile2
-l
will remove newline character from input line, and add it back while printing-a
split input line based on whitespaces,@F
array will have the data($i) = grep $F[$_] eq "fn0" 0..$#F
get index of the element whose exact content isfn0
print "$F[$i-1] $F[$i+1]"
print the required fields
With sed
that supports ERE
$ sed -E 's/^(.* )?([^ ]+) fn0 ([^ ]+).*/2 3/' ip.txt
tail logfile
tail3 logfile1
tail4 logfile2
^(.* )?
optional fields at start of line([^ ]+) fn0 ([^ ]+)
capture fields before and afterfn0
(assuming single space as field separator).*
rest of the line2 3
required fields in output
add a comment |
With perl
$ perl -lane '($i) = grep $F[$_] eq "fn0" 0..$#F;
print "$F[$i-1] $F[$i+1]"' ip.txt
tail logfile
tail3 logfile1
tail4 logfile2
-l
will remove newline character from input line, and add it back while printing-a
split input line based on whitespaces,@F
array will have the data($i) = grep $F[$_] eq "fn0" 0..$#F
get index of the element whose exact content isfn0
print "$F[$i-1] $F[$i+1]"
print the required fields
With sed
that supports ERE
$ sed -E 's/^(.* )?([^ ]+) fn0 ([^ ]+).*/2 3/' ip.txt
tail logfile
tail3 logfile1
tail4 logfile2
^(.* )?
optional fields at start of line([^ ]+) fn0 ([^ ]+)
capture fields before and afterfn0
(assuming single space as field separator).*
rest of the line2 3
required fields in output
With perl
$ perl -lane '($i) = grep $F[$_] eq "fn0" 0..$#F;
print "$F[$i-1] $F[$i+1]"' ip.txt
tail logfile
tail3 logfile1
tail4 logfile2
-l
will remove newline character from input line, and add it back while printing-a
split input line based on whitespaces,@F
array will have the data($i) = grep $F[$_] eq "fn0" 0..$#F
get index of the element whose exact content isfn0
print "$F[$i-1] $F[$i+1]"
print the required fields
With sed
that supports ERE
$ sed -E 's/^(.* )?([^ ]+) fn0 ([^ ]+).*/2 3/' ip.txt
tail logfile
tail3 logfile1
tail4 logfile2
^(.* )?
optional fields at start of line([^ ]+) fn0 ([^ ]+)
capture fields before and afterfn0
(assuming single space as field separator).*
rest of the line2 3
required fields in output
answered 8 hours ago
SundeepSundeep
7,7361 gold badge11 silver badges28 bronze badges
7,7361 gold badge11 silver badges28 bronze badges
add a comment |
add a comment |
a solution with grep and sed:
egrep -oh '[a-zA-Z0-9]+ fn0 [a-zA-Z0-9]+' testfile | sed 's/ fn0 / /'
New contributor
add a comment |
a solution with grep and sed:
egrep -oh '[a-zA-Z0-9]+ fn0 [a-zA-Z0-9]+' testfile | sed 's/ fn0 / /'
New contributor
add a comment |
a solution with grep and sed:
egrep -oh '[a-zA-Z0-9]+ fn0 [a-zA-Z0-9]+' testfile | sed 's/ fn0 / /'
New contributor
a solution with grep and sed:
egrep -oh '[a-zA-Z0-9]+ fn0 [a-zA-Z0-9]+' testfile | sed 's/ fn0 / /'
New contributor
New contributor
answered 9 hours ago
markgrafmarkgraf
1205 bronze badges
1205 bronze badges
New contributor
New contributor
add a comment |
add a comment |
$ sed 's/.*(tail[[:digit:]]*) fn[[:digit:]]*/1/' file
tail logfile
tail3 logfile1
tail4 logfile2
The sed
expression is replaces everything on each line up to some substring matching tailXX fnYY
with tailXX
(where XX
and YY
are some positive integers).
add a comment |
$ sed 's/.*(tail[[:digit:]]*) fn[[:digit:]]*/1/' file
tail logfile
tail3 logfile1
tail4 logfile2
The sed
expression is replaces everything on each line up to some substring matching tailXX fnYY
with tailXX
(where XX
and YY
are some positive integers).
add a comment |
$ sed 's/.*(tail[[:digit:]]*) fn[[:digit:]]*/1/' file
tail logfile
tail3 logfile1
tail4 logfile2
The sed
expression is replaces everything on each line up to some substring matching tailXX fnYY
with tailXX
(where XX
and YY
are some positive integers).
$ sed 's/.*(tail[[:digit:]]*) fn[[:digit:]]*/1/' file
tail logfile
tail3 logfile1
tail4 logfile2
The sed
expression is replaces everything on each line up to some substring matching tailXX fnYY
with tailXX
(where XX
and YY
are some positive integers).
answered 9 hours ago
Kusalananda♦Kusalananda
158k18 gold badges313 silver badges498 bronze badges
158k18 gold badges313 silver badges498 bronze badges
add a comment |
add a comment |
rahul is a new contributor. Be nice, and check out our Code of Conduct.
rahul is a new contributor. Be nice, and check out our Code of Conduct.
rahul is a new contributor. Be nice, and check out our Code of Conduct.
rahul is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f534129%2fprint-field-before-and-after-matching-pattern-of-single-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown