- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: shell script
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
тАО07-18-2006 03:45 AM
тАО07-18-2006 03:45 AM
I have a string like
"ax-a1: xxx@xxx.com yyy@yyy.com zzz@zzz.com;
ax-a2: aaa@aaa.com bbb@bbb.com ccc@ccc.com;"
I need to extract the mailid seperated by a space for any given user like ax-a1.
I have tried to use
Test=`grep a1-ax mailids.txt`
echo $Test | awk '{print substr($0,":",index($0,";"))}'
but it doesn't seem to work. Can you please suggest??
TIA
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-18-2006 04:04 AM
тАО07-18-2006 04:04 AM
SolutionHmm,
The second argument to substr is an offset numner, and you passed it a piece of string.
That's not goign to work.
Please explain a little more clearly what you expact for output and where the input comes from.
- A single long string with multiple usernames + email?
- When ax-a1 is select, which are we supposed to return: xxx@xxx.com, or all of "xxx@xxx.com yyy@yyy.com zzz@zzz.com"?
You may readily find a solution by using a regexpr for field seperator. Check out the following example:
$ cat x
ax-a1: xxx@xxx.com yyy@yyy.com zzz@zzz.com; ax-a2: aaa@aaa.com bbb@bbb.com ccc@ccc.com;
$ awk -F'[:; ]+' '{print $3}' x
yyy@yyy.com
$ awk -F'[:;]' '{print $3}' x
ax-a2
$ awk -F'[:;]' '{print $2}' x
xxx@xxx.com yyy@yyy.com zzz@zzz.com
Hope this helps some...
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-18-2006 04:05 AM
тАО07-18-2006 04:05 AM
Re: shell script
if your string has an entry
ax-a1
und you grep for
a1-ax
you won't get results :-)
This will give you the second field stripped by the last character, which is ";" in your case:
awk -F: -v user=ax-a1 '$1 == user {print substr($2,1,length($2)-1)}' mailids.txt
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-18-2006 05:42 AM
тАО07-18-2006 05:42 AM
Re: shell script
Note, I had to strip spaces from the username for it to match easily:
$awk -F'[:;]' -v user=ax-a2 '{i=0; while (i++
aaa@aaa.com bbb@bbb.com ccc@ccc.com
Or you could have 'fields' be user:email by using ';' as FS (field seperator) and then splitting on ":" to look at the parts:
$ awk -F\; -v user=ax-a1 '{i=0; while (i++
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-18-2006 06:17 AM
тАО07-18-2006 06:17 AM
Re: shell script
How about:
# STR="ax-a1: xxx@xxx.com yyy@yyy.com zzz@zzz.com;ax-a2: aaa@aaa.com bbb@bbb.com ccc@ccc.com;"
# echo ${STR} | perl -nle 'BEGIN{$u=shift};print $1 if m/$u:(.+?);/' ax-a1
...would return:
xxx@xxx.com yyy@yyy.com zzz@zzz.com
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-18-2006 06:34 AM
тАО07-18-2006 06:34 AM
Re: shell script
# echo $Test | sed -n 's/\(.*\): \(.*\);/\2/p'
...and with awk
echo $Test | awk -F": " '{print z[split($2,z,";")-1]}'
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-18-2006 08:32 PM
тАО07-18-2006 08:32 PM
Re: shell script
you'll still have a ';' at the end but that's removed quickly with sed or something similar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2006 03:16 AM
тАО07-19-2006 03:16 AM