scanning and grepping

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a file (*.ses) which contains following line
$ rea ses '../../../../abcdefgh/abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001.ses'
When I use this command:
cat a4.ses | grep -im1 'rea ses' | awk -F'[/]' 'print $NF'
the output is:
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001.ses'
I just want to output to be:
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001
without the extension.
How can I do that?
centos text-processing awk grep
add a comment |Â
up vote
0
down vote
favorite
I have a file (*.ses) which contains following line
$ rea ses '../../../../abcdefgh/abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001.ses'
When I use this command:
cat a4.ses | grep -im1 'rea ses' | awk -F'[/]' 'print $NF'
the output is:
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001.ses'
I just want to output to be:
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001
without the extension.
How can I do that?
centos text-processing awk grep
I've reverted the damage to this question from an earlier edit. Please note that the single quote on the end of the "faulty" output provided in this question is really there.
â roaima
Nov 20 '17 at 20:37
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a file (*.ses) which contains following line
$ rea ses '../../../../abcdefgh/abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001.ses'
When I use this command:
cat a4.ses | grep -im1 'rea ses' | awk -F'[/]' 'print $NF'
the output is:
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001.ses'
I just want to output to be:
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001
without the extension.
How can I do that?
centos text-processing awk grep
I have a file (*.ses) which contains following line
$ rea ses '../../../../abcdefgh/abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001.ses'
When I use this command:
cat a4.ses | grep -im1 'rea ses' | awk -F'[/]' 'print $NF'
the output is:
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001.ses'
I just want to output to be:
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001
without the extension.
How can I do that?
centos text-processing awk grep
edited Nov 20 '17 at 20:52
Jeff Schaller
32.1k849109
32.1k849109
asked Nov 20 '17 at 14:44
user261421
I've reverted the damage to this question from an earlier edit. Please note that the single quote on the end of the "faulty" output provided in this question is really there.
â roaima
Nov 20 '17 at 20:37
add a comment |Â
I've reverted the damage to this question from an earlier edit. Please note that the single quote on the end of the "faulty" output provided in this question is really there.
â roaima
Nov 20 '17 at 20:37
I've reverted the damage to this question from an earlier edit. Please note that the single quote on the end of the "faulty" output provided in this question is really there.
â roaima
Nov 20 '17 at 20:37
I've reverted the damage to this question from an earlier edit. Please note that the single quote on the end of the "faulty" output provided in this question is really there.
â roaima
Nov 20 '17 at 20:37
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
2
down vote
accepted
If .ses' is a static extension, simply hard-code the removal of those trailing 5 characters into awk by printing the string from the beginning until 5 from the end:
awk -F/ 'print substr($NF, 1, length($NF)-5)'
If the length of the extension can vary, then replace it with the empty string before printing it:
awk -F/ 'gsub(/..+$/, "", $NF); print $NF'
don't want to hardcode any numbers!!
â user261421
Nov 20 '17 at 15:01
Thansks for the Tip... I have combined your way in-combination with one of my other programm!! Super working
â user261421
Nov 20 '17 at 15:05
I do agree with Hardcoding ( if the extension is same). Thanks
â user261421
Nov 20 '17 at 15:07
well-spotted, @roaima; thank you! I'll correct my post.
â Jeff Schaller
Nov 20 '17 at 20:49
add a comment |Â
up vote
2
down vote
If your grep supports perl compatible regular expression (PCRE) syntax:
$ grep -Po 'rea ses.*/K[^.]*' file
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001
Explanation:
- match
rea sesand then greedily everything up to/inclusive; then - match the longest sequence of non-period characters
- discard (
K) the left portion and output only what remains of the match (-o)
Thanks Steeldriver. will you please explain the pattern ?
â user261421
Nov 20 '17 at 15:11
add a comment |Â
up vote
1
down vote
You can dispense with that pipeline and use sed instead
sed -n '/rea ses/s!^.*/(.*).[^.]*$!1!p' a4.ses
Output
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001
What that sed command does can be described as follows
-ndo not print anything unless a match is made/rea ses/only consider lines that match this REs!...!...!psubstitute the RE within the first two exclamation marks (!) for the following string, but only print the line if a match occursThe RE
^.*/(.*).[^.]*$matches- Everything up to the last slash
/ - Everything from there to the last dot
.(remembered as pattern1) - Everything else
- Everything up to the last slash
The substitution of the pattern described in #4 is made with pattern
1, i.e. your filename without the trailing dotted extension
can you suggest still simpler way ? length of the last name may vary, it may or may not contain any '_' characters!!
â user261421
Nov 20 '17 at 14:59
grepif you have a suitable one. Orawk. Or evenpythonif you want. There are lots of solutions. This is mine.
â roaima
Nov 20 '17 at 15:00
Thanks Roaima...using your way in combination with my other scripts.
â user261421
Nov 20 '17 at 15:06
your response was super fast :)
â user261421
Nov 20 '17 at 15:06
add a comment |Â
up vote
0
down vote
You can use basename to remove a trailing extension:
cat a4.ses | grep -im1 'rea ses' | awk -F'[/]' 'print $NF' | xargs basename -s .ses'
(submitted for completeness, given your process, @steeldriver's answer is better)
... or escaped. Fixed. Thx
â xenoid
Nov 20 '17 at 21:37
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
If .ses' is a static extension, simply hard-code the removal of those trailing 5 characters into awk by printing the string from the beginning until 5 from the end:
awk -F/ 'print substr($NF, 1, length($NF)-5)'
If the length of the extension can vary, then replace it with the empty string before printing it:
awk -F/ 'gsub(/..+$/, "", $NF); print $NF'
don't want to hardcode any numbers!!
â user261421
Nov 20 '17 at 15:01
Thansks for the Tip... I have combined your way in-combination with one of my other programm!! Super working
â user261421
Nov 20 '17 at 15:05
I do agree with Hardcoding ( if the extension is same). Thanks
â user261421
Nov 20 '17 at 15:07
well-spotted, @roaima; thank you! I'll correct my post.
â Jeff Schaller
Nov 20 '17 at 20:49
add a comment |Â
up vote
2
down vote
accepted
If .ses' is a static extension, simply hard-code the removal of those trailing 5 characters into awk by printing the string from the beginning until 5 from the end:
awk -F/ 'print substr($NF, 1, length($NF)-5)'
If the length of the extension can vary, then replace it with the empty string before printing it:
awk -F/ 'gsub(/..+$/, "", $NF); print $NF'
don't want to hardcode any numbers!!
â user261421
Nov 20 '17 at 15:01
Thansks for the Tip... I have combined your way in-combination with one of my other programm!! Super working
â user261421
Nov 20 '17 at 15:05
I do agree with Hardcoding ( if the extension is same). Thanks
â user261421
Nov 20 '17 at 15:07
well-spotted, @roaima; thank you! I'll correct my post.
â Jeff Schaller
Nov 20 '17 at 20:49
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
If .ses' is a static extension, simply hard-code the removal of those trailing 5 characters into awk by printing the string from the beginning until 5 from the end:
awk -F/ 'print substr($NF, 1, length($NF)-5)'
If the length of the extension can vary, then replace it with the empty string before printing it:
awk -F/ 'gsub(/..+$/, "", $NF); print $NF'
If .ses' is a static extension, simply hard-code the removal of those trailing 5 characters into awk by printing the string from the beginning until 5 from the end:
awk -F/ 'print substr($NF, 1, length($NF)-5)'
If the length of the extension can vary, then replace it with the empty string before printing it:
awk -F/ 'gsub(/..+$/, "", $NF); print $NF'
edited Nov 20 '17 at 20:52
answered Nov 20 '17 at 14:53
Jeff Schaller
32.1k849109
32.1k849109
don't want to hardcode any numbers!!
â user261421
Nov 20 '17 at 15:01
Thansks for the Tip... I have combined your way in-combination with one of my other programm!! Super working
â user261421
Nov 20 '17 at 15:05
I do agree with Hardcoding ( if the extension is same). Thanks
â user261421
Nov 20 '17 at 15:07
well-spotted, @roaima; thank you! I'll correct my post.
â Jeff Schaller
Nov 20 '17 at 20:49
add a comment |Â
don't want to hardcode any numbers!!
â user261421
Nov 20 '17 at 15:01
Thansks for the Tip... I have combined your way in-combination with one of my other programm!! Super working
â user261421
Nov 20 '17 at 15:05
I do agree with Hardcoding ( if the extension is same). Thanks
â user261421
Nov 20 '17 at 15:07
well-spotted, @roaima; thank you! I'll correct my post.
â Jeff Schaller
Nov 20 '17 at 20:49
don't want to hardcode any numbers!!
â user261421
Nov 20 '17 at 15:01
don't want to hardcode any numbers!!
â user261421
Nov 20 '17 at 15:01
Thansks for the Tip... I have combined your way in-combination with one of my other programm!! Super working
â user261421
Nov 20 '17 at 15:05
Thansks for the Tip... I have combined your way in-combination with one of my other programm!! Super working
â user261421
Nov 20 '17 at 15:05
I do agree with Hardcoding ( if the extension is same). Thanks
â user261421
Nov 20 '17 at 15:07
I do agree with Hardcoding ( if the extension is same). Thanks
â user261421
Nov 20 '17 at 15:07
well-spotted, @roaima; thank you! I'll correct my post.
â Jeff Schaller
Nov 20 '17 at 20:49
well-spotted, @roaima; thank you! I'll correct my post.
â Jeff Schaller
Nov 20 '17 at 20:49
add a comment |Â
up vote
2
down vote
If your grep supports perl compatible regular expression (PCRE) syntax:
$ grep -Po 'rea ses.*/K[^.]*' file
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001
Explanation:
- match
rea sesand then greedily everything up to/inclusive; then - match the longest sequence of non-period characters
- discard (
K) the left portion and output only what remains of the match (-o)
Thanks Steeldriver. will you please explain the pattern ?
â user261421
Nov 20 '17 at 15:11
add a comment |Â
up vote
2
down vote
If your grep supports perl compatible regular expression (PCRE) syntax:
$ grep -Po 'rea ses.*/K[^.]*' file
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001
Explanation:
- match
rea sesand then greedily everything up to/inclusive; then - match the longest sequence of non-period characters
- discard (
K) the left portion and output only what remains of the match (-o)
Thanks Steeldriver. will you please explain the pattern ?
â user261421
Nov 20 '17 at 15:11
add a comment |Â
up vote
2
down vote
up vote
2
down vote
If your grep supports perl compatible regular expression (PCRE) syntax:
$ grep -Po 'rea ses.*/K[^.]*' file
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001
Explanation:
- match
rea sesand then greedily everything up to/inclusive; then - match the longest sequence of non-period characters
- discard (
K) the left portion and output only what remains of the match (-o)
If your grep supports perl compatible regular expression (PCRE) syntax:
$ grep -Po 'rea ses.*/K[^.]*' file
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001
Explanation:
- match
rea sesand then greedily everything up to/inclusive; then - match the longest sequence of non-period characters
- discard (
K) the left portion and output only what remains of the match (-o)
edited Nov 20 '17 at 15:17
answered Nov 20 '17 at 14:59
steeldriver
31.9k34979
31.9k34979
Thanks Steeldriver. will you please explain the pattern ?
â user261421
Nov 20 '17 at 15:11
add a comment |Â
Thanks Steeldriver. will you please explain the pattern ?
â user261421
Nov 20 '17 at 15:11
Thanks Steeldriver. will you please explain the pattern ?
â user261421
Nov 20 '17 at 15:11
Thanks Steeldriver. will you please explain the pattern ?
â user261421
Nov 20 '17 at 15:11
add a comment |Â
up vote
1
down vote
You can dispense with that pipeline and use sed instead
sed -n '/rea ses/s!^.*/(.*).[^.]*$!1!p' a4.ses
Output
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001
What that sed command does can be described as follows
-ndo not print anything unless a match is made/rea ses/only consider lines that match this REs!...!...!psubstitute the RE within the first two exclamation marks (!) for the following string, but only print the line if a match occursThe RE
^.*/(.*).[^.]*$matches- Everything up to the last slash
/ - Everything from there to the last dot
.(remembered as pattern1) - Everything else
- Everything up to the last slash
The substitution of the pattern described in #4 is made with pattern
1, i.e. your filename without the trailing dotted extension
can you suggest still simpler way ? length of the last name may vary, it may or may not contain any '_' characters!!
â user261421
Nov 20 '17 at 14:59
grepif you have a suitable one. Orawk. Or evenpythonif you want. There are lots of solutions. This is mine.
â roaima
Nov 20 '17 at 15:00
Thanks Roaima...using your way in combination with my other scripts.
â user261421
Nov 20 '17 at 15:06
your response was super fast :)
â user261421
Nov 20 '17 at 15:06
add a comment |Â
up vote
1
down vote
You can dispense with that pipeline and use sed instead
sed -n '/rea ses/s!^.*/(.*).[^.]*$!1!p' a4.ses
Output
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001
What that sed command does can be described as follows
-ndo not print anything unless a match is made/rea ses/only consider lines that match this REs!...!...!psubstitute the RE within the first two exclamation marks (!) for the following string, but only print the line if a match occursThe RE
^.*/(.*).[^.]*$matches- Everything up to the last slash
/ - Everything from there to the last dot
.(remembered as pattern1) - Everything else
- Everything up to the last slash
The substitution of the pattern described in #4 is made with pattern
1, i.e. your filename without the trailing dotted extension
can you suggest still simpler way ? length of the last name may vary, it may or may not contain any '_' characters!!
â user261421
Nov 20 '17 at 14:59
grepif you have a suitable one. Orawk. Or evenpythonif you want. There are lots of solutions. This is mine.
â roaima
Nov 20 '17 at 15:00
Thanks Roaima...using your way in combination with my other scripts.
â user261421
Nov 20 '17 at 15:06
your response was super fast :)
â user261421
Nov 20 '17 at 15:06
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can dispense with that pipeline and use sed instead
sed -n '/rea ses/s!^.*/(.*).[^.]*$!1!p' a4.ses
Output
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001
What that sed command does can be described as follows
-ndo not print anything unless a match is made/rea ses/only consider lines that match this REs!...!...!psubstitute the RE within the first two exclamation marks (!) for the following string, but only print the line if a match occursThe RE
^.*/(.*).[^.]*$matches- Everything up to the last slash
/ - Everything from there to the last dot
.(remembered as pattern1) - Everything else
- Everything up to the last slash
The substitution of the pattern described in #4 is made with pattern
1, i.e. your filename without the trailing dotted extension
You can dispense with that pipeline and use sed instead
sed -n '/rea ses/s!^.*/(.*).[^.]*$!1!p' a4.ses
Output
abcd_efgh-A20_ABC-abcdefgh-Abcdefgh_Abcdef_123er_vb001
What that sed command does can be described as follows
-ndo not print anything unless a match is made/rea ses/only consider lines that match this REs!...!...!psubstitute the RE within the first two exclamation marks (!) for the following string, but only print the line if a match occursThe RE
^.*/(.*).[^.]*$matches- Everything up to the last slash
/ - Everything from there to the last dot
.(remembered as pattern1) - Everything else
- Everything up to the last slash
The substitution of the pattern described in #4 is made with pattern
1, i.e. your filename without the trailing dotted extension
edited Nov 20 '17 at 14:59
answered Nov 20 '17 at 14:53
roaima
39.9k546109
39.9k546109
can you suggest still simpler way ? length of the last name may vary, it may or may not contain any '_' characters!!
â user261421
Nov 20 '17 at 14:59
grepif you have a suitable one. Orawk. Or evenpythonif you want. There are lots of solutions. This is mine.
â roaima
Nov 20 '17 at 15:00
Thanks Roaima...using your way in combination with my other scripts.
â user261421
Nov 20 '17 at 15:06
your response was super fast :)
â user261421
Nov 20 '17 at 15:06
add a comment |Â
can you suggest still simpler way ? length of the last name may vary, it may or may not contain any '_' characters!!
â user261421
Nov 20 '17 at 14:59
grepif you have a suitable one. Orawk. Or evenpythonif you want. There are lots of solutions. This is mine.
â roaima
Nov 20 '17 at 15:00
Thanks Roaima...using your way in combination with my other scripts.
â user261421
Nov 20 '17 at 15:06
your response was super fast :)
â user261421
Nov 20 '17 at 15:06
can you suggest still simpler way ? length of the last name may vary, it may or may not contain any '_' characters!!
â user261421
Nov 20 '17 at 14:59
can you suggest still simpler way ? length of the last name may vary, it may or may not contain any '_' characters!!
â user261421
Nov 20 '17 at 14:59
grep if you have a suitable one. Or awk. Or even python if you want. There are lots of solutions. This is mine.â roaima
Nov 20 '17 at 15:00
grep if you have a suitable one. Or awk. Or even python if you want. There are lots of solutions. This is mine.â roaima
Nov 20 '17 at 15:00
Thanks Roaima...using your way in combination with my other scripts.
â user261421
Nov 20 '17 at 15:06
Thanks Roaima...using your way in combination with my other scripts.
â user261421
Nov 20 '17 at 15:06
your response was super fast :)
â user261421
Nov 20 '17 at 15:06
your response was super fast :)
â user261421
Nov 20 '17 at 15:06
add a comment |Â
up vote
0
down vote
You can use basename to remove a trailing extension:
cat a4.ses | grep -im1 'rea ses' | awk -F'[/]' 'print $NF' | xargs basename -s .ses'
(submitted for completeness, given your process, @steeldriver's answer is better)
... or escaped. Fixed. Thx
â xenoid
Nov 20 '17 at 21:37
add a comment |Â
up vote
0
down vote
You can use basename to remove a trailing extension:
cat a4.ses | grep -im1 'rea ses' | awk -F'[/]' 'print $NF' | xargs basename -s .ses'
(submitted for completeness, given your process, @steeldriver's answer is better)
... or escaped. Fixed. Thx
â xenoid
Nov 20 '17 at 21:37
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can use basename to remove a trailing extension:
cat a4.ses | grep -im1 'rea ses' | awk -F'[/]' 'print $NF' | xargs basename -s .ses'
(submitted for completeness, given your process, @steeldriver's answer is better)
You can use basename to remove a trailing extension:
cat a4.ses | grep -im1 'rea ses' | awk -F'[/]' 'print $NF' | xargs basename -s .ses'
(submitted for completeness, given your process, @steeldriver's answer is better)
edited Nov 20 '17 at 21:36
answered Nov 20 '17 at 15:15
xenoid
1,7051620
1,7051620
... or escaped. Fixed. Thx
â xenoid
Nov 20 '17 at 21:37
add a comment |Â
... or escaped. Fixed. Thx
â xenoid
Nov 20 '17 at 21:37
... or escaped. Fixed. Thx
â xenoid
Nov 20 '17 at 21:37
... or escaped. Fixed. Thx
â xenoid
Nov 20 '17 at 21:37
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%2f405787%2fscanning-and-grepping%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
I've reverted the damage to this question from an earlier edit. Please note that the single quote on the end of the "faulty" output provided in this question is really there.
â roaima
Nov 20 '17 at 20:37