Extracting lines that contain an exact phrase that includes a tab
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a file that contains the following lines
50[tab]H[tab]1
44[tab]H[tab]2
4[tab]H[tab]3
4[tab]H[tab]4
44[tab]H[tab]5
234[tab]H[tab]6
I would like to only extract the last line that starts with the exact phrase "4[tab]H" (so this would be "4[tab]H[tab]4") into another file. I have tried:
grep "^4*.H" filein.in | tail -1 >> fileout.out
but instead it extracts "44[tab]H[tab]5". I need it to extract the last line that starts with the exact string: "4[tab]H".
text-processing grep
add a comment |Â
up vote
0
down vote
favorite
I have a file that contains the following lines
50[tab]H[tab]1
44[tab]H[tab]2
4[tab]H[tab]3
4[tab]H[tab]4
44[tab]H[tab]5
234[tab]H[tab]6
I would like to only extract the last line that starts with the exact phrase "4[tab]H" (so this would be "4[tab]H[tab]4") into another file. I have tried:
grep "^4*.H" filein.in | tail -1 >> fileout.out
but instead it extracts "44[tab]H[tab]5". I need it to extract the last line that starts with the exact string: "4[tab]H".
text-processing grep
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a file that contains the following lines
50[tab]H[tab]1
44[tab]H[tab]2
4[tab]H[tab]3
4[tab]H[tab]4
44[tab]H[tab]5
234[tab]H[tab]6
I would like to only extract the last line that starts with the exact phrase "4[tab]H" (so this would be "4[tab]H[tab]4") into another file. I have tried:
grep "^4*.H" filein.in | tail -1 >> fileout.out
but instead it extracts "44[tab]H[tab]5". I need it to extract the last line that starts with the exact string: "4[tab]H".
text-processing grep
I have a file that contains the following lines
50[tab]H[tab]1
44[tab]H[tab]2
4[tab]H[tab]3
4[tab]H[tab]4
44[tab]H[tab]5
234[tab]H[tab]6
I would like to only extract the last line that starts with the exact phrase "4[tab]H" (so this would be "4[tab]H[tab]4") into another file. I have tried:
grep "^4*.H" filein.in | tail -1 >> fileout.out
but instead it extracts "44[tab]H[tab]5". I need it to extract the last line that starts with the exact string: "4[tab]H".
text-processing grep
asked Mar 31 at 23:48
A. B
224
224
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
In ksh93/zsh/bash/mksh/FreeBSD sh:
grep $'^4tH' filein.in | tail -n 1
Or in any shell:
awk '/^4tH/ line=$0; ; END if(line!="") print line; ' filein.in
add a comment |Â
up vote
1
down vote
With gnu sed
sed '/^4tH/h;$bA;N;D;:A;x;/^$/d' infile
2
Orgsed -n '/^4tH/h;$g;/./p;'
â Stéphane Chazelas
Apr 1 at 10:10
1
Or gsed '/^4tH/h;$!d;g;/./!d'
â ctac_
Apr 1 at 10:36
add a comment |Â
up vote
0
down vote
You can also reverse the input file line wise using tac
and then get first match
$ # add another t after H if needed
$ tac ip.txt | grep -m1 $'^4tH'
4 H 4
$ tac ip.txt | awk -F't' '$1=="4" && $2=="H"print; exit'
4 H 4
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
In ksh93/zsh/bash/mksh/FreeBSD sh:
grep $'^4tH' filein.in | tail -n 1
Or in any shell:
awk '/^4tH/ line=$0; ; END if(line!="") print line; ' filein.in
add a comment |Â
up vote
4
down vote
accepted
In ksh93/zsh/bash/mksh/FreeBSD sh:
grep $'^4tH' filein.in | tail -n 1
Or in any shell:
awk '/^4tH/ line=$0; ; END if(line!="") print line; ' filein.in
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
In ksh93/zsh/bash/mksh/FreeBSD sh:
grep $'^4tH' filein.in | tail -n 1
Or in any shell:
awk '/^4tH/ line=$0; ; END if(line!="") print line; ' filein.in
In ksh93/zsh/bash/mksh/FreeBSD sh:
grep $'^4tH' filein.in | tail -n 1
Or in any shell:
awk '/^4tH/ line=$0; ; END if(line!="") print line; ' filein.in
edited Apr 1 at 10:08
Stéphane Chazelas
280k53514846
280k53514846
answered Mar 31 at 23:56
Hauke Laging
53.2k1282130
53.2k1282130
add a comment |Â
add a comment |Â
up vote
1
down vote
With gnu sed
sed '/^4tH/h;$bA;N;D;:A;x;/^$/d' infile
2
Orgsed -n '/^4tH/h;$g;/./p;'
â Stéphane Chazelas
Apr 1 at 10:10
1
Or gsed '/^4tH/h;$!d;g;/./!d'
â ctac_
Apr 1 at 10:36
add a comment |Â
up vote
1
down vote
With gnu sed
sed '/^4tH/h;$bA;N;D;:A;x;/^$/d' infile
2
Orgsed -n '/^4tH/h;$g;/./p;'
â Stéphane Chazelas
Apr 1 at 10:10
1
Or gsed '/^4tH/h;$!d;g;/./!d'
â ctac_
Apr 1 at 10:36
add a comment |Â
up vote
1
down vote
up vote
1
down vote
With gnu sed
sed '/^4tH/h;$bA;N;D;:A;x;/^$/d' infile
With gnu sed
sed '/^4tH/h;$bA;N;D;:A;x;/^$/d' infile
answered Apr 1 at 9:52
ctac_
1,016116
1,016116
2
Orgsed -n '/^4tH/h;$g;/./p;'
â Stéphane Chazelas
Apr 1 at 10:10
1
Or gsed '/^4tH/h;$!d;g;/./!d'
â ctac_
Apr 1 at 10:36
add a comment |Â
2
Orgsed -n '/^4tH/h;$g;/./p;'
â Stéphane Chazelas
Apr 1 at 10:10
1
Or gsed '/^4tH/h;$!d;g;/./!d'
â ctac_
Apr 1 at 10:36
2
2
Or
gsed -n '/^4tH/h;$g;/./p;'
â Stéphane Chazelas
Apr 1 at 10:10
Or
gsed -n '/^4tH/h;$g;/./p;'
â Stéphane Chazelas
Apr 1 at 10:10
1
1
Or gsed '/^4tH/h;$!d;g;/./!d'
â ctac_
Apr 1 at 10:36
Or gsed '/^4tH/h;$!d;g;/./!d'
â ctac_
Apr 1 at 10:36
add a comment |Â
up vote
0
down vote
You can also reverse the input file line wise using tac
and then get first match
$ # add another t after H if needed
$ tac ip.txt | grep -m1 $'^4tH'
4 H 4
$ tac ip.txt | awk -F't' '$1=="4" && $2=="H"print; exit'
4 H 4
add a comment |Â
up vote
0
down vote
You can also reverse the input file line wise using tac
and then get first match
$ # add another t after H if needed
$ tac ip.txt | grep -m1 $'^4tH'
4 H 4
$ tac ip.txt | awk -F't' '$1=="4" && $2=="H"print; exit'
4 H 4
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can also reverse the input file line wise using tac
and then get first match
$ # add another t after H if needed
$ tac ip.txt | grep -m1 $'^4tH'
4 H 4
$ tac ip.txt | awk -F't' '$1=="4" && $2=="H"print; exit'
4 H 4
You can also reverse the input file line wise using tac
and then get first match
$ # add another t after H if needed
$ tac ip.txt | grep -m1 $'^4tH'
4 H 4
$ tac ip.txt | awk -F't' '$1=="4" && $2=="H"print; exit'
4 H 4
answered Apr 1 at 2:22
Sundeep
6,9511826
6,9511826
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f434760%2fextracting-lines-that-contain-an-exact-phrase-that-includes-a-tab%23new-answer', 'question_page');
);
Post as a guest
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
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
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