sed regular expression failure

Chris Schmitz

I have a sample file with the contents:

Filesystem                                         512-blocks       Used  Available Capacity   iused      ifree %iused  Mounted on
/dev/disk0s2                                        467182912  419318824   47352088    90%  52478851    5919011   90%   /
devfs                                                     419        419          0   100%       727          0  100%   /dev
/dev/disk1s2                                        975093952  673515008  301578944    70%  84189374   37697368   69%   /Volumes/Local_Storage
map -hosts                                                  0          0          0   100%         0          0  100%   /net
map auto_home                                               0          0          0   100%         0          0  100%   /home
localhost:/l3ZTI82fIEDeEEIvUkf44A                   467182912  467182912          0   100%         0          0  100%   /Volumes/MobileBackups
/dev/disk2s2                                       1952853344 1925763856   27089488    99% 240720480    3386186   99%   /Volumes/SK Backup
/dev/disk3s2                                        199328216   88909928  110418288    45%  11113739   13802286   45%   /Volumes/Secure_Storage
/dev/disk4s2                                         59328216   51456432    7871784    87%   6432052     983973   87%   /Volumes/Secure
/dev/disk5s2                                         60000000   12713448   47286552    22%   1589179    5910819   21%   /Volumes/Secure_Personal
//[email protected]/Storage 4294701048 1128302984 3166398064    27% 141037871  395799758   26%   /Volumes/Storage
/dev/disk6s2                                           200000       9952     190048     5%      1242      23756    5%   /Volumes/VAULT
//[email protected]/chris.s           467182912  437521864   29661048    94%  54690231    3707631   94%   /Volumes/chris.schmitz
//chris@hq-srv03/NET              167563256   50264576  117298680    30%         0 18446744073709551615    0%   /Volumes/NETLOGON

And I'm working on pulling out just the ip addresses and host names from the file. Right now I'm working on grabbing the ips using the following pattern:

cat dfsample.txt | awk '/@/' | sed -E 's/.*([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*/\1/g'

With unexpected results:

//[email protected]/Storage 4294701048 1128302984 3166398064    27% 141037871  395799758   26%   /Volumes/Storage
2.20.1.76
//chris@hq-srv03/NET               167563256   50264576  117298680    30%         0 18446744073709551615    0%   /Volumes/NETLOGON

My expectation for the sed section was that the .* before and after the pattern defined in the parens would select the entire line and when I substituted the line for the pattern found within the parens using the \1 it would substitute the entire line with the found pattern leaving only the ip address.

For some reason the first two digits of my ip address is getting cut off. When I try the pattern in the parens in sublime it finds the ip without an issue. What is it that I'm missing?

jthill
sed -nr 's,^//[^@/]*@([^/]*)/.*,\1,p'  

gets both hostnames and IP addresses and won't be fooled by "interesting" volume names.

If your sed doesn't have the r flag, the escaping isn't too ugly on this one, I probably should have given it just this way:

sed -n 's,^//[^@/]*@\([^/]*\)/.*,\1,p'

(edit: [^@] -> [^@/] safety play)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Escaping plus in sed regular expression

From Java

regular expression in sed replace question mark

From Dev

Sed multiline regular expression issue

From Dev

sed with regular expression

From Dev

understand Regular expression in a sed command

From Dev

the use of "+" in sed regular expression

From Dev

Converting a sed regular expression to python code

From Dev

Is there another regular-expression "flavor" in GNU sed?

From Dev

sed positional replacement with regular expression

From Dev

C POSIX Regular Expression failure

From Dev

How to match everything but a regular expression with sed?

From Dev

sed regular expression failed on solaris

From Dev

sed to edit only part of a file with regular expression

From Dev

the use of "+" in sed regular expression

From Dev

Converting a sed regular expression to python code

From Dev

sed: regular expression,how to substitute?

From Dev

Regular expression with sed

From Dev

Using Sed with regular expression to save results into a variable

From Dev

How to use [\w]+ in regular expression in sed?

From Dev

sed replace regular expression match

From Dev

sed regular expression extraction

From Dev

sed regular expression matching more than intended

From Dev

sed command with regular expression

From Dev

How to match everything but a regular expression with sed?

From Dev

Using Sed in a for loop with a regular expression

From Dev

Negation of sed regular expression

From Dev

sed regular expression for escaped urls

From Dev

Sed Regular Expression with /P

From Dev

+ Regular Expression not working in sed