Substring from end to a letter's index length in unix shell script

Navdeep

I have string say,

a="abc_def ghi__333_31122013_Monthly.pdf"

I need the substract monthly and 31122013 from the above string in shell script. Basically doing substring from last till the first index of '_'.

Any help or suggestion is welcome.

Navdeep

anubhava

Using awk:

a="abc_def ghi__333_31122013_Monthly.pdf"
awk -F '[_.]' '{print $(NF-2), $(NF-1)}' <<< "$a"
31122013 Monthly

Using IFS in BASH:

IFS=[_.] && arr=($a)
l=${#arr[@]}
echo ${arr[$l-3]}
31122013
echo ${arr[$l-2]}
Monthly

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I remove a character from a string in a Unix shell script?

From Dev

Grep / Find oracle table name from UNIX Shell / SQL Script

From Dev

Find substring from end to index of a delimiter in PYTHON

From Dev

How to get a substring after the last underscore (_) in unix shell script

From Dev

Execute command from string variable in unix shell script

From Dev

Unix Shell Script: Remove duplicates from line ignore blank lines

From Dev

Run Perl Script From Unix Shell Script

From Dev

How to cut a space from a string in a Unix shell script

From Dev

Match the substring that starts from the last capital letter word that is not in parenthesis to the end of the string

From Dev

Substring from index 1 until the end of string

From Dev

Substring causing index and length error

From Dev

How to read & count XML records from a file in UNIX shell Script

From Dev

Access UNIX server and run a shell script from java application

From Dev

Arithmetic in Unix shell Script

From Dev

How do I remove a character from a string in a Unix shell script?

From Dev

Unix Shell Script

From Dev

Unix Shell Script: Remove duplicates from line ignore blank lines

From Dev

Shell Script - Unix

From Dev

Shell script for ssh into a Unix/Linux server from MacOS X 10.10

From Dev

How to trigger shell script on Unix via email from Exchange Server

From Dev

How to cut a space from a string in a Unix shell script

From Dev

Finding substring at the end of line in shell

From Dev

Extracting string from Substring in shell script

From Dev

String length with shell script

From Dev

Shell script, linux, unix, shell

From Dev

Extract substring from the first letter

From Dev

Delete a single line from a file with a unix shell script

From Dev

bash shell script get substring from string containing quotes

From Dev

Unix shell script to populate date from file as first column in the file

Related Related

  1. 1

    How do I remove a character from a string in a Unix shell script?

  2. 2

    Grep / Find oracle table name from UNIX Shell / SQL Script

  3. 3

    Find substring from end to index of a delimiter in PYTHON

  4. 4

    How to get a substring after the last underscore (_) in unix shell script

  5. 5

    Execute command from string variable in unix shell script

  6. 6

    Unix Shell Script: Remove duplicates from line ignore blank lines

  7. 7

    Run Perl Script From Unix Shell Script

  8. 8

    How to cut a space from a string in a Unix shell script

  9. 9

    Match the substring that starts from the last capital letter word that is not in parenthesis to the end of the string

  10. 10

    Substring from index 1 until the end of string

  11. 11

    Substring causing index and length error

  12. 12

    How to read & count XML records from a file in UNIX shell Script

  13. 13

    Access UNIX server and run a shell script from java application

  14. 14

    Arithmetic in Unix shell Script

  15. 15

    How do I remove a character from a string in a Unix shell script?

  16. 16

    Unix Shell Script

  17. 17

    Unix Shell Script: Remove duplicates from line ignore blank lines

  18. 18

    Shell Script - Unix

  19. 19

    Shell script for ssh into a Unix/Linux server from MacOS X 10.10

  20. 20

    How to trigger shell script on Unix via email from Exchange Server

  21. 21

    How to cut a space from a string in a Unix shell script

  22. 22

    Finding substring at the end of line in shell

  23. 23

    Extracting string from Substring in shell script

  24. 24

    String length with shell script

  25. 25

    Shell script, linux, unix, shell

  26. 26

    Extract substring from the first letter

  27. 27

    Delete a single line from a file with a unix shell script

  28. 28

    bash shell script get substring from string containing quotes

  29. 29

    Unix shell script to populate date from file as first column in the file

HotTag

Archive