- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Need to grep for first initial and lastname
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
Forums
Discussions
Discussions
Discussions
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
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
09-22-2009 12:13 PM
09-22-2009 12:13 PM
firstname_lastname and I need the date in this form -
f_lastname
How shall I go about getting this.
Thanks,
Allan.
Solved! Go to Solution.
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2009 12:15 PM
09-22-2009 12:15 PM
Re: Need to grep for first initial and lastname
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2009 12:42 PM
09-22-2009 12:42 PM
Solutionsed -e 's/^\(.\)[^_]*_\(.*\)$/\1_\2/g' < firstname_lastname.txt > f_lastname.txt
This is a regular expression, which tends to look a bit cryptic. (Someone has said they tend to be Write-Only.)
So here's it split into meaningful parts:
s/ ... / ... /g = the global search-and-replace command, probably the most often-used command of sed.
The search expression:
^ = from the beginning of a line...
\(.\) = ...take a single character and remember it...
[^_]*_ = ...which is followed by any number of non-underscore characters, followed by a single underscore.
\(.*\)$ = After the underscore, take everything and remember it too.
The replacement expression:
\1_\2 = The first thing we asked sed to remember, followed by an underscore and the second thing.
MK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2009 12:52 PM
09-22-2009 12:52 PM
Re: Need to grep for first initial and lastname
Marco,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2009 01:04 PM
09-22-2009 01:04 PM
Re: Need to grep for first initial and lastname
# X="firstname_lastname"
# echo ${X}|perl -ple 's/^(.)(?:.+?)(_.+)/$1$2/'
f_lastname
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2009 07:48 PM
09-22-2009 07:48 PM
Re: Need to grep for first initial and lastname
You can do it like this way also
awk 'BEGIN { FS = "_" } ; { print substr($0,1,1)"_"$2 }' output file
Suraj
- Tags:
- awk