Perl one liner to extract first three octets of IP Address


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl one liner to extract first three octets of IP Address
# 1  
Old 01-14-2016
Perl one liner to extract first three octets of IP Address

Hi

I have an inherited perl script that extracts the first three octets of an IP Address using a combination of split() against a dot and then builds it all back together again, its a whole block of code to do this


I wondered if anyone had a one liner in their arsenal to extract the first three octets into a variable ?


Cheers
# 2  
Old 01-14-2016
My perlis non-existent, but you might use this regex:
Code:
sed 's/\.[^.]*$//' file

# 3  
Old 01-14-2016
Thanks, so within my perl script I can invoke shell (cheating a bit) to get the result

Code:
my $firstthree = `echo $ip | sed -e 's/\.[^.]*\$//'`;
chomp $firstthree;

Which returns the desired result

But id love to know how to do this natively in perl without having to invoke a shell process
# 4  
Old 01-14-2016
You know you can use regexes in perl as well, do you?
# 5  
Old 01-14-2016
Code:
$ip="10.20.30.40";
$ip=~s/\.[^.]*$//;
printf "%s\n", $ip;

Or with split
Code:
$ip="10.20.30.40";
($a1,$a2,$a3)=split(/\./,$ip);
printf "%s\n", join ".",$a1,$a2,$a3

# 6  
Old 01-14-2016
Or, just using shell built-ins, assuming that there are 4 period separated parts of your input ip address and you want to output the 1st three:
Code:
$ ip=w.x.y.z
$ echo ${ip%.*}
w.x.y
$

# 7  
Old 01-14-2016
Please, try in Perl
Code:
my ($mask) = $ip =~ /^(\d+\.\d+\.\d+)/;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl one liner to wrap comment lines

Greetings fellow scripters. I find myself editing multiple files, sometimes with the same bits of information. My bash script, a changelog, and a plist file (OS X). Once I realized this, I thought why not script part of this process (and so it begins). In any case, I've solved several of the... (1 Reply)
Discussion started by: reid
1 Replies

2. Shell Programming and Scripting

PERL one liner

hi, I am using PERL one liner for oracle database connection as : $PERL -e "use DBI; DBI->connect(qw(DBI:Oracle:SID user passwd));" is there a way to append select statement to this connection ? i.e. DB connection and select stmt in one line ? how to do sysdba connection using one lines... (1 Reply)
Discussion started by: talashil
1 Replies

3. Shell Programming and Scripting

Delete line - Perl one liner

Hi all, I need a Perl one liner which prints a newline into a .txt file, only where the line starts with "/mediawiki-1.19.0/". It should add the newline to the line before. My problem is, when I try to realize this (with my little knowledge :rolleyes: ) i come to the point where the // are... (4 Replies)
Discussion started by: Mr.Smith
4 Replies

4. UNIX for Dummies Questions & Answers

Perl one liner to replace text

Not quite a unix question but problem in a perl command. Taking a chance if someone knows about the error cat 1 a b c d perl -p -e 's/a/b/g' 1 b b c d What is the problem here?? perl -p -i -e 's/a/b/g' 1 Can't remove 1: Text file busy, skipping file. (2 Replies)
Discussion started by: analyst
2 Replies

5. Shell Programming and Scripting

replace awk with a perl one liner (REGEXP and FS)

hello, I want to replace awk with a perl one liner in unix. i use in awk REGEX and FS ( field separator) because awk syntaxes in different unix os versions have not the same behaviour. Awk, Nawk and GNU Awk Cheat Sheet - good coders code, great reuse i have a file named "file" and want... (5 Replies)
Discussion started by: bora99
5 Replies

6. Shell Programming and Scripting

Replacing Awk with One-liner Perl

can someone help me translate the following command, from: /usr/bin/awk "/^$TOFDAYM $TOFDAYD /,0" $LOGFILE to something like perl -e ..... basically, i want to use perl to do awk functions within a shell script. i want to do the above awk, using perl. any suggestions? (9 Replies)
Discussion started by: SkySmart
9 Replies

7. Shell Programming and Scripting

Perl one-liner convert to script format problem asking

Input_file_1: ABC1 DEF11 ABC3 DEF7 ABC7 DEF36 Input_file_2: DEF7 light 23 DEF11 over 2 DEF11 over 1 DEF17 blue 0 Perl one-liner that join two input file based on columns sharing a value (In this example, column 2 in Input_file_1 and column 1 in... (3 Replies)
Discussion started by: perl_beginner
3 Replies

8. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

9. Shell Programming and Scripting

awk/perl one-liner assist

In a ~4GB file there are lines like, 13.13.4.3 Googe.com - Jan/23/2011:00:00:00 +0000 "URL Google HTTP/1.1" 45 56 208 - "http://www.gogle.com/webhp?hl=en&tab=nw#hl=en&source=hp&biw=1366&bih=667&q=hello&aq=f&aqi=&aql=&oq=&fp=c432485467934a89" ".Net; Fox" - 13.145.3.3 Goge.com -... (3 Replies)
Discussion started by: gameboy87
3 Replies

10. Shell Programming and Scripting

Perl One Liner

Hi , Can anybody explain how this perl one liner works.. It is to test whether the number is prime or not perl -le 'print "PRIME" if (1 x shift) !~ /^(11+)\1+$/' 19 Thanks in advance Shihab (2 Replies)
Discussion started by: shihabvk
2 Replies
Login or Register to Ask a Question