Using grep to filtergrep *stringA*[number]Using the grep filterHow to filter content between specific keywords using grep?Using individual lines from grep outputs“egrep: empty (sub)expression” when attempting to filter out words from a listHow to filter pair of bracesOutput current day using calGrep multiple files and output to multiple files in a single commandHow to use grep to search for a line with one of two words but not both?Use multiple expressions in grep consumer
Housemarks (superimposed & combined letters, heraldry)
Diatonic chords of a pentatonic vs blues scale?
Assigning function to function pointer, const argument correctness?
How and why do references in academic papers work?
How can one's career as a reviewer be ended?
Why isn't Bash trap working if output is redirected to stdout?
Proving that a Russian cryptographic standard is too structured
How to befriend someone who doesn't like to talk?
How do you play "tenth" chords on the guitar?
What plausible reason could I give for my FTL drive only working in space
A Salute to Poetry
Trying to get (more) accurate readings from thermistor (electronics, math, and code inside)
Are the guests in Westworld forbidden to tell the hosts that they are robots?
If there's something that implicates the president why is there then a national security issue? (John Dowd)
Why is the length of the Kelvin unit of temperature equal to that of the Celsius unit?
NUL delimited variable
Multiband vertical antenna not working as expected
How durable are silver inlays on a blade?
What do you call the action of "describing events as they happen" like sports anchors do?
Confused with atmospheric pressure equals plastic balloon’s inner pressure
Tikz-cd diagram arrow passing under a node - not crossing it
Is the Keras Embedding layer dependent on the target label?
How was the airlock installed on the Space Shuttle mid deck?
Does the Nuka-Cola bottler actually generate nuka cola?
Using grep to filter
grep *stringA*[number]Using the grep filterHow to filter content between specific keywords using grep?Using individual lines from grep outputs“egrep: empty (sub)expression” when attempting to filter out words from a listHow to filter pair of bracesOutput current day using calGrep multiple files and output to multiple files in a single commandHow to use grep to search for a line with one of two words but not both?Use multiple expressions in grep consumer
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Suppose I have a file.txt with the following lines :
hello myname1 is yellow.pcapng red
festive myname33 is hddd.pcapng dfdf
crude myname44 is hello.pcapng
Now my goal is to filter to the lines so it outputs to out.txt as follows :
myname1 yellow.pcapng
myname33 hddd.pcapng
myname44 hello.pcapng
Now I know that I can use :
grep -oh "w*mynamew*" /tmp/file.txt > /tmp/out.txt
grep -o '[^ ]+g' /tmp/file.txt > /tmp/out.txt
to get the both respective parts of the expression individually.
How do I combine these commands so that I get my desired output?
grep
New contributor
add a comment |
Suppose I have a file.txt with the following lines :
hello myname1 is yellow.pcapng red
festive myname33 is hddd.pcapng dfdf
crude myname44 is hello.pcapng
Now my goal is to filter to the lines so it outputs to out.txt as follows :
myname1 yellow.pcapng
myname33 hddd.pcapng
myname44 hello.pcapng
Now I know that I can use :
grep -oh "w*mynamew*" /tmp/file.txt > /tmp/out.txt
grep -o '[^ ]+g' /tmp/file.txt > /tmp/out.txt
to get the both respective parts of the expression individually.
How do I combine these commands so that I get my desired output?
grep
New contributor
3
do you have to usegrep
? You have a problem that's begging for anawk
solution, and awk should be available everywhere that grep is.
– Jeff Schaller♦
Jun 3 at 18:05
I was only familiar with grep and not awk, could you show me how to do using awk possibly?
– gangsignwall
Jun 3 at 18:13
@JeffSchaller It's actually begging for acut
solution, since this is exactly whatcut
is supposed to do
– ChatterOne
Jun 4 at 9:57
Good thing there is a cut solution, @ChatterOne!
– Jeff Schaller♦
Jun 4 at 10:30
add a comment |
Suppose I have a file.txt with the following lines :
hello myname1 is yellow.pcapng red
festive myname33 is hddd.pcapng dfdf
crude myname44 is hello.pcapng
Now my goal is to filter to the lines so it outputs to out.txt as follows :
myname1 yellow.pcapng
myname33 hddd.pcapng
myname44 hello.pcapng
Now I know that I can use :
grep -oh "w*mynamew*" /tmp/file.txt > /tmp/out.txt
grep -o '[^ ]+g' /tmp/file.txt > /tmp/out.txt
to get the both respective parts of the expression individually.
How do I combine these commands so that I get my desired output?
grep
New contributor
Suppose I have a file.txt with the following lines :
hello myname1 is yellow.pcapng red
festive myname33 is hddd.pcapng dfdf
crude myname44 is hello.pcapng
Now my goal is to filter to the lines so it outputs to out.txt as follows :
myname1 yellow.pcapng
myname33 hddd.pcapng
myname44 hello.pcapng
Now I know that I can use :
grep -oh "w*mynamew*" /tmp/file.txt > /tmp/out.txt
grep -o '[^ ]+g' /tmp/file.txt > /tmp/out.txt
to get the both respective parts of the expression individually.
How do I combine these commands so that I get my desired output?
grep
grep
New contributor
New contributor
edited Jun 3 at 18:06
Jeff Schaller♦
46.4k1166151
46.4k1166151
New contributor
asked Jun 3 at 18:01
gangsignwallgangsignwall
132
132
New contributor
New contributor
3
do you have to usegrep
? You have a problem that's begging for anawk
solution, and awk should be available everywhere that grep is.
– Jeff Schaller♦
Jun 3 at 18:05
I was only familiar with grep and not awk, could you show me how to do using awk possibly?
– gangsignwall
Jun 3 at 18:13
@JeffSchaller It's actually begging for acut
solution, since this is exactly whatcut
is supposed to do
– ChatterOne
Jun 4 at 9:57
Good thing there is a cut solution, @ChatterOne!
– Jeff Schaller♦
Jun 4 at 10:30
add a comment |
3
do you have to usegrep
? You have a problem that's begging for anawk
solution, and awk should be available everywhere that grep is.
– Jeff Schaller♦
Jun 3 at 18:05
I was only familiar with grep and not awk, could you show me how to do using awk possibly?
– gangsignwall
Jun 3 at 18:13
@JeffSchaller It's actually begging for acut
solution, since this is exactly whatcut
is supposed to do
– ChatterOne
Jun 4 at 9:57
Good thing there is a cut solution, @ChatterOne!
– Jeff Schaller♦
Jun 4 at 10:30
3
3
do you have to use
grep
? You have a problem that's begging for an awk
solution, and awk should be available everywhere that grep is.– Jeff Schaller♦
Jun 3 at 18:05
do you have to use
grep
? You have a problem that's begging for an awk
solution, and awk should be available everywhere that grep is.– Jeff Schaller♦
Jun 3 at 18:05
I was only familiar with grep and not awk, could you show me how to do using awk possibly?
– gangsignwall
Jun 3 at 18:13
I was only familiar with grep and not awk, could you show me how to do using awk possibly?
– gangsignwall
Jun 3 at 18:13
@JeffSchaller It's actually begging for a
cut
solution, since this is exactly what cut
is supposed to do– ChatterOne
Jun 4 at 9:57
@JeffSchaller It's actually begging for a
cut
solution, since this is exactly what cut
is supposed to do– ChatterOne
Jun 4 at 9:57
Good thing there is a cut solution, @ChatterOne!
– Jeff Schaller♦
Jun 4 at 10:30
Good thing there is a cut solution, @ChatterOne!
– Jeff Schaller♦
Jun 4 at 10:30
add a comment |
5 Answers
5
active
oldest
votes
Given your sample data, you could assume that words #2 and #4 are what you want to extract; you'd express that in awk with:
awk ' print $2, $4 ' < /tmp/file.txt > /tmp/out.txt
add a comment |
You could use cut
:
cut -d' ' -f2,4 < /tmp/file.txt > /tmp/out.txt
add a comment |
Using awk instead of grep.
awk 'print $2,$4' /tmp/file.txt > /tmp/out.txt
You are piping the output of cat file.txt
to awk
.
Then using awk expressions 'print $2,$4'
, you are printing the 2nd and 4th field of the split line, with a space to separate.
Your output will be how you desire.
New contributor
add a comment |
Could do this too:
cat file.txt | sed "s/.*(myname[0-9]*) is ([^ ]*).*/1 2/g" | grep ^myname > /tmp/out.txt
add a comment |
Since shell script method has been provided above, Tried in Python and worked fine too
#!/usr/bin/python
o=[]
k=open('i','r')
for z in k:
o.append(z.strip().split(' ')[1])
o.append(z.strip().split(' ')[3])
for d in range(0,len(o),2):
print " ".join(o[d:d+2])
output
myname1 yellow.pcapng
myname33 hddd.pcapng
myname44 hello.pcapng
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
);
);
gangsignwall 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%2f522665%2fusing-grep-to-filter%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Given your sample data, you could assume that words #2 and #4 are what you want to extract; you'd express that in awk with:
awk ' print $2, $4 ' < /tmp/file.txt > /tmp/out.txt
add a comment |
Given your sample data, you could assume that words #2 and #4 are what you want to extract; you'd express that in awk with:
awk ' print $2, $4 ' < /tmp/file.txt > /tmp/out.txt
add a comment |
Given your sample data, you could assume that words #2 and #4 are what you want to extract; you'd express that in awk with:
awk ' print $2, $4 ' < /tmp/file.txt > /tmp/out.txt
Given your sample data, you could assume that words #2 and #4 are what you want to extract; you'd express that in awk with:
awk ' print $2, $4 ' < /tmp/file.txt > /tmp/out.txt
edited Jun 3 at 18:42
Kusalananda♦
150k18288474
150k18288474
answered Jun 3 at 18:18
Jeff Schaller♦Jeff Schaller
46.4k1166151
46.4k1166151
add a comment |
add a comment |
You could use cut
:
cut -d' ' -f2,4 < /tmp/file.txt > /tmp/out.txt
add a comment |
You could use cut
:
cut -d' ' -f2,4 < /tmp/file.txt > /tmp/out.txt
add a comment |
You could use cut
:
cut -d' ' -f2,4 < /tmp/file.txt > /tmp/out.txt
You could use cut
:
cut -d' ' -f2,4 < /tmp/file.txt > /tmp/out.txt
answered Jun 4 at 7:02
pLumopLumo
5,7921125
5,7921125
add a comment |
add a comment |
Using awk instead of grep.
awk 'print $2,$4' /tmp/file.txt > /tmp/out.txt
You are piping the output of cat file.txt
to awk
.
Then using awk expressions 'print $2,$4'
, you are printing the 2nd and 4th field of the split line, with a space to separate.
Your output will be how you desire.
New contributor
add a comment |
Using awk instead of grep.
awk 'print $2,$4' /tmp/file.txt > /tmp/out.txt
You are piping the output of cat file.txt
to awk
.
Then using awk expressions 'print $2,$4'
, you are printing the 2nd and 4th field of the split line, with a space to separate.
Your output will be how you desire.
New contributor
add a comment |
Using awk instead of grep.
awk 'print $2,$4' /tmp/file.txt > /tmp/out.txt
You are piping the output of cat file.txt
to awk
.
Then using awk expressions 'print $2,$4'
, you are printing the 2nd and 4th field of the split line, with a space to separate.
Your output will be how you desire.
New contributor
Using awk instead of grep.
awk 'print $2,$4' /tmp/file.txt > /tmp/out.txt
You are piping the output of cat file.txt
to awk
.
Then using awk expressions 'print $2,$4'
, you are printing the 2nd and 4th field of the split line, with a space to separate.
Your output will be how you desire.
New contributor
edited Jun 4 at 6:33
Nasir Riley
3,3252410
3,3252410
New contributor
answered Jun 3 at 18:19
Mylo MyloMylo Mylo
211
211
New contributor
New contributor
add a comment |
add a comment |
Could do this too:
cat file.txt | sed "s/.*(myname[0-9]*) is ([^ ]*).*/1 2/g" | grep ^myname > /tmp/out.txt
add a comment |
Could do this too:
cat file.txt | sed "s/.*(myname[0-9]*) is ([^ ]*).*/1 2/g" | grep ^myname > /tmp/out.txt
add a comment |
Could do this too:
cat file.txt | sed "s/.*(myname[0-9]*) is ([^ ]*).*/1 2/g" | grep ^myname > /tmp/out.txt
Could do this too:
cat file.txt | sed "s/.*(myname[0-9]*) is ([^ ]*).*/1 2/g" | grep ^myname > /tmp/out.txt
answered Jun 3 at 18:23
Ulysses_Ulysses_
115
115
add a comment |
add a comment |
Since shell script method has been provided above, Tried in Python and worked fine too
#!/usr/bin/python
o=[]
k=open('i','r')
for z in k:
o.append(z.strip().split(' ')[1])
o.append(z.strip().split(' ')[3])
for d in range(0,len(o),2):
print " ".join(o[d:d+2])
output
myname1 yellow.pcapng
myname33 hddd.pcapng
myname44 hello.pcapng
add a comment |
Since shell script method has been provided above, Tried in Python and worked fine too
#!/usr/bin/python
o=[]
k=open('i','r')
for z in k:
o.append(z.strip().split(' ')[1])
o.append(z.strip().split(' ')[3])
for d in range(0,len(o),2):
print " ".join(o[d:d+2])
output
myname1 yellow.pcapng
myname33 hddd.pcapng
myname44 hello.pcapng
add a comment |
Since shell script method has been provided above, Tried in Python and worked fine too
#!/usr/bin/python
o=[]
k=open('i','r')
for z in k:
o.append(z.strip().split(' ')[1])
o.append(z.strip().split(' ')[3])
for d in range(0,len(o),2):
print " ".join(o[d:d+2])
output
myname1 yellow.pcapng
myname33 hddd.pcapng
myname44 hello.pcapng
Since shell script method has been provided above, Tried in Python and worked fine too
#!/usr/bin/python
o=[]
k=open('i','r')
for z in k:
o.append(z.strip().split(' ')[1])
o.append(z.strip().split(' ')[3])
for d in range(0,len(o),2):
print " ".join(o[d:d+2])
output
myname1 yellow.pcapng
myname33 hddd.pcapng
myname44 hello.pcapng
answered Jun 4 at 16:09
Praveen Kumar BSPraveen Kumar BS
2,0572311
2,0572311
add a comment |
add a comment |
gangsignwall is a new contributor. Be nice, and check out our Code of Conduct.
gangsignwall is a new contributor. Be nice, and check out our Code of Conduct.
gangsignwall is a new contributor. Be nice, and check out our Code of Conduct.
gangsignwall 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%2f522665%2fusing-grep-to-filter%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
3
do you have to use
grep
? You have a problem that's begging for anawk
solution, and awk should be available everywhere that grep is.– Jeff Schaller♦
Jun 3 at 18:05
I was only familiar with grep and not awk, could you show me how to do using awk possibly?
– gangsignwall
Jun 3 at 18:13
@JeffSchaller It's actually begging for a
cut
solution, since this is exactly whatcut
is supposed to do– ChatterOne
Jun 4 at 9:57
Good thing there is a cut solution, @ChatterOne!
– Jeff Schaller♦
Jun 4 at 10:30