- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- filename field extraction
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-17-2005 01:33 PM
тАО11-17-2005 01:33 PM
I have a file with different length and separated by "_". See example of filename below.
aaaa_bb_cc_1111_222222_s1.txt
xxxx_yy_cc_dd_1111_222222_s2.txt
I would like to seek help on how to extract the 1111 and 222222 from the filenames above and put them in a variable.
Maximum points for all correct replies.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-17-2005 02:23 PM
тАО11-17-2005 02:23 PM
Solutionextract()
{
typeset V=${1}
shift
typeset -i XSTAT=0
echo "${V}" | awk -F '_' '{printf("%s_%s\n",$(NF - 2),$(NF - 1))}'
XSTAT=${?}
return ${XSTAT}
} # extract
V1="aaaa_bbb_cc_11111_222222_s1.txt"
XX=$(extract "${V1}")
echo "XX = ${XX}"
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-17-2005 02:32 PM
тАО11-17-2005 02:32 PM
Re: filename field extraction
the 1111 and 222222 needs to be in a separate variable. thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-17-2005 02:53 PM
тАО11-17-2005 02:53 PM
Re: filename field extraction
Thanks for your input!
Nice job!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-21-2005 02:29 AM
тАО11-21-2005 02:29 AM
Re: filename field extraction
My suggestion in this case would be to use awk, as Clay did but instead of counting fields I would just use $NF-2 and $NF-1 sense the portions of the file names you want are always going to be 2nd and 3rd from the last.
Might make for a simpler read in the script if you ever have to troubleshoot it.
Howard