scanning and grepping

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question






















  • 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















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?







share|improve this question






















  • 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













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?







share|improve this question














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?









share|improve this question













share|improve this question




share|improve this question








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

















  • 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











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'





share|improve this answer






















  • 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

















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 ses and 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)





share|improve this answer






















  • Thanks Steeldriver. will you please explain the pattern ?
    – user261421
    Nov 20 '17 at 15:11

















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




  1. -n do not print anything unless a match is made


  2. /rea ses/ only consider lines that match this RE


  3. s!...!...!p substitute the RE within the first two exclamation marks (!) for the following string, but only print the line if a match occurs


  4. The RE ^.*/(.*).[^.]*$ matches



    • Everything up to the last slash /

    • Everything from there to the last dot . (remembered as pattern 1)

    • Everything else


  5. The substitution of the pattern described in #4 is made with pattern 1, i.e. your filename without the trailing dotted extension






share|improve this answer






















  • 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











  • 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

















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)






share|improve this answer






















  • ... or escaped. Fixed. Thx
    – xenoid
    Nov 20 '17 at 21:37










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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





























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'





share|improve this answer






















  • 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














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'





share|improve this answer






















  • 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












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'





share|improve this answer














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'






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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












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 ses and 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)





share|improve this answer






















  • Thanks Steeldriver. will you please explain the pattern ?
    – user261421
    Nov 20 '17 at 15:11














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 ses and 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)





share|improve this answer






















  • Thanks Steeldriver. will you please explain the pattern ?
    – user261421
    Nov 20 '17 at 15:11












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 ses and 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)





share|improve this answer














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 ses and 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)






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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










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




  1. -n do not print anything unless a match is made


  2. /rea ses/ only consider lines that match this RE


  3. s!...!...!p substitute the RE within the first two exclamation marks (!) for the following string, but only print the line if a match occurs


  4. The RE ^.*/(.*).[^.]*$ matches



    • Everything up to the last slash /

    • Everything from there to the last dot . (remembered as pattern 1)

    • Everything else


  5. The substitution of the pattern described in #4 is made with pattern 1, i.e. your filename without the trailing dotted extension






share|improve this answer






















  • 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











  • 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














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




  1. -n do not print anything unless a match is made


  2. /rea ses/ only consider lines that match this RE


  3. s!...!...!p substitute the RE within the first two exclamation marks (!) for the following string, but only print the line if a match occurs


  4. The RE ^.*/(.*).[^.]*$ matches



    • Everything up to the last slash /

    • Everything from there to the last dot . (remembered as pattern 1)

    • Everything else


  5. The substitution of the pattern described in #4 is made with pattern 1, i.e. your filename without the trailing dotted extension






share|improve this answer






















  • 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











  • 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












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




  1. -n do not print anything unless a match is made


  2. /rea ses/ only consider lines that match this RE


  3. s!...!...!p substitute the RE within the first two exclamation marks (!) for the following string, but only print the line if a match occurs


  4. The RE ^.*/(.*).[^.]*$ matches



    • Everything up to the last slash /

    • Everything from there to the last dot . (remembered as pattern 1)

    • Everything else


  5. The substitution of the pattern described in #4 is made with pattern 1, i.e. your filename without the trailing dotted extension






share|improve this answer














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




  1. -n do not print anything unless a match is made


  2. /rea ses/ only consider lines that match this RE


  3. s!...!...!p substitute the RE within the first two exclamation marks (!) for the following string, but only print the line if a match occurs


  4. The RE ^.*/(.*).[^.]*$ matches



    • Everything up to the last slash /

    • Everything from there to the last dot . (remembered as pattern 1)

    • Everything else


  5. The substitution of the pattern described in #4 is made with pattern 1, i.e. your filename without the trailing dotted extension







share|improve this answer














share|improve this answer



share|improve this answer








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











  • 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










  • 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











  • 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










  • 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










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)






share|improve this answer






















  • ... or escaped. Fixed. Thx
    – xenoid
    Nov 20 '17 at 21:37














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)






share|improve this answer






















  • ... or escaped. Fixed. Thx
    – xenoid
    Nov 20 '17 at 21:37












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)






share|improve this answer














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)







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • ... 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

The Forum (Inglewood, California)

Palaiologos