sed removing everything until and including the first period if there's more than one period on that line and do this for the whole file
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
4
down vote
favorite
sed
removing everything until and including the first period if there is more than one period on that line and do this for the whole file.
Before sed:
akamai.com
cdnjs.cloudflare.com
com.cdn.cloudflare.net
After sed:
akamai.com
cloudflare.com
cdn.cloudflare.net
awk sed
add a comment |Â
up vote
4
down vote
favorite
sed
removing everything until and including the first period if there is more than one period on that line and do this for the whole file.
Before sed:
akamai.com
cdnjs.cloudflare.com
com.cdn.cloudflare.net
After sed:
akamai.com
cloudflare.com
cdn.cloudflare.net
awk sed
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
sed
removing everything until and including the first period if there is more than one period on that line and do this for the whole file.
Before sed:
akamai.com
cdnjs.cloudflare.com
com.cdn.cloudflare.net
After sed:
akamai.com
cloudflare.com
cdn.cloudflare.net
awk sed
sed
removing everything until and including the first period if there is more than one period on that line and do this for the whole file.
Before sed:
akamai.com
cdnjs.cloudflare.com
com.cdn.cloudflare.net
After sed:
akamai.com
cloudflare.com
cdn.cloudflare.net
awk sed
edited Jul 27 at 17:30
slmâ¦
232k65479649
232k65479649
asked Jul 27 at 17:09
Bjorn
302
302
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
12
down vote
accepted
$ sed '/..*./s/^[^.]*.//' file
akamai.com
cloudflare.com
cdn.cloudflare.net
The sed
script first matches lines that contain at least two dots using the regular expression ..*.
(could also have been written [.].*[.]
). For lines matching this, a substitution that removes everything up to and including the first dot is performed.
Using awk
, being somewhat long-winded in comparison to the above:
$ awk -F '.' -vOFS='.' 'NF > 2 n=split($0, a); $0=""; for (i=2;i<=n;++i) $(NF+1)=a[i] 1' file
akamai.com
cloudflare.com
cdn.cloudflare.net
Here, whenever there are more than two dot-delimited fields, we split the current line on dots, and then re-create the current record from that, skipping the first field. The trailing 1
at the end causes every line (modified or not) to be printed.
Shorter awk
in the same fashion as the sed
solution:
$ awk -F '.' 'NF > 2 sub("^[^.]*.", "") 1' file
akamai.com
cloudflare.com
cdn.cloudflare.net
It is possible to use only one regex in sed:sed '/.([^.]*.)/s//1/' infile
and the awk could be a lot smaller:awk '$0~resub(/^[^.]*./,"")1' re='^[^.]*\.([^.]*\.)' infile
â Isaac
Jul 27 at 21:03
Grep gets a bit complex:grep -P '^(?=[^.]*.[^.]*.)[^.]*.K.*|.*' infile
â Isaac
Jul 27 at 21:21
add a comment |Â
up vote
1
down vote
You could approach this using the following methods:
perl -lpe '$_ = $1 if /.(.*..*)/' input-file.txt
wherein, we rely on a regex
that zeroes in on a dot .
character that can see another dot to it's right. Then whatever is to the right is captured and made available in $1
and stuffed inside the current line. The -p
option to Perl
then takes this to the stdout
as well as the one that didn't match as well.
perl -F\. -pale '$_ = join ".", splice @F, 1 if @F > 2' input.txt
Input file is read line-by-line via the
-p
option and autoprint is also turned on via this option as well.Each record is split on the dot
.
and the individual fields stored in the array@F
indexed starting from0
via the-a
option.-l
option makesRS = ORS = "n"
Only when we have more than 2 elements in the array
@F
, meaning there were atleast 2 dots in the current record, we select such a record for modifications.For such a record, the
splice @F, 1
function strips away elements 2nd onwards and presents them to thejoin
function which then joins them using the dot character, this then is stuffed inside the$_
, aka, current record.The
-p
option then takes this modified current record to the stdout. The one that didnot modify is anyway taken silently to the stdout.
Using GNU sed
we can also do the task without taking recourse of capturing parens:
sed -e
s/./n/2;T
y/n./.n/
s/n/./2g
s/.*n//
' input.file
Output:
akamai.com
cloudflare.com
cdn.cloudflare.net
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
accepted
$ sed '/..*./s/^[^.]*.//' file
akamai.com
cloudflare.com
cdn.cloudflare.net
The sed
script first matches lines that contain at least two dots using the regular expression ..*.
(could also have been written [.].*[.]
). For lines matching this, a substitution that removes everything up to and including the first dot is performed.
Using awk
, being somewhat long-winded in comparison to the above:
$ awk -F '.' -vOFS='.' 'NF > 2 n=split($0, a); $0=""; for (i=2;i<=n;++i) $(NF+1)=a[i] 1' file
akamai.com
cloudflare.com
cdn.cloudflare.net
Here, whenever there are more than two dot-delimited fields, we split the current line on dots, and then re-create the current record from that, skipping the first field. The trailing 1
at the end causes every line (modified or not) to be printed.
Shorter awk
in the same fashion as the sed
solution:
$ awk -F '.' 'NF > 2 sub("^[^.]*.", "") 1' file
akamai.com
cloudflare.com
cdn.cloudflare.net
It is possible to use only one regex in sed:sed '/.([^.]*.)/s//1/' infile
and the awk could be a lot smaller:awk '$0~resub(/^[^.]*./,"")1' re='^[^.]*\.([^.]*\.)' infile
â Isaac
Jul 27 at 21:03
Grep gets a bit complex:grep -P '^(?=[^.]*.[^.]*.)[^.]*.K.*|.*' infile
â Isaac
Jul 27 at 21:21
add a comment |Â
up vote
12
down vote
accepted
$ sed '/..*./s/^[^.]*.//' file
akamai.com
cloudflare.com
cdn.cloudflare.net
The sed
script first matches lines that contain at least two dots using the regular expression ..*.
(could also have been written [.].*[.]
). For lines matching this, a substitution that removes everything up to and including the first dot is performed.
Using awk
, being somewhat long-winded in comparison to the above:
$ awk -F '.' -vOFS='.' 'NF > 2 n=split($0, a); $0=""; for (i=2;i<=n;++i) $(NF+1)=a[i] 1' file
akamai.com
cloudflare.com
cdn.cloudflare.net
Here, whenever there are more than two dot-delimited fields, we split the current line on dots, and then re-create the current record from that, skipping the first field. The trailing 1
at the end causes every line (modified or not) to be printed.
Shorter awk
in the same fashion as the sed
solution:
$ awk -F '.' 'NF > 2 sub("^[^.]*.", "") 1' file
akamai.com
cloudflare.com
cdn.cloudflare.net
It is possible to use only one regex in sed:sed '/.([^.]*.)/s//1/' infile
and the awk could be a lot smaller:awk '$0~resub(/^[^.]*./,"")1' re='^[^.]*\.([^.]*\.)' infile
â Isaac
Jul 27 at 21:03
Grep gets a bit complex:grep -P '^(?=[^.]*.[^.]*.)[^.]*.K.*|.*' infile
â Isaac
Jul 27 at 21:21
add a comment |Â
up vote
12
down vote
accepted
up vote
12
down vote
accepted
$ sed '/..*./s/^[^.]*.//' file
akamai.com
cloudflare.com
cdn.cloudflare.net
The sed
script first matches lines that contain at least two dots using the regular expression ..*.
(could also have been written [.].*[.]
). For lines matching this, a substitution that removes everything up to and including the first dot is performed.
Using awk
, being somewhat long-winded in comparison to the above:
$ awk -F '.' -vOFS='.' 'NF > 2 n=split($0, a); $0=""; for (i=2;i<=n;++i) $(NF+1)=a[i] 1' file
akamai.com
cloudflare.com
cdn.cloudflare.net
Here, whenever there are more than two dot-delimited fields, we split the current line on dots, and then re-create the current record from that, skipping the first field. The trailing 1
at the end causes every line (modified or not) to be printed.
Shorter awk
in the same fashion as the sed
solution:
$ awk -F '.' 'NF > 2 sub("^[^.]*.", "") 1' file
akamai.com
cloudflare.com
cdn.cloudflare.net
$ sed '/..*./s/^[^.]*.//' file
akamai.com
cloudflare.com
cdn.cloudflare.net
The sed
script first matches lines that contain at least two dots using the regular expression ..*.
(could also have been written [.].*[.]
). For lines matching this, a substitution that removes everything up to and including the first dot is performed.
Using awk
, being somewhat long-winded in comparison to the above:
$ awk -F '.' -vOFS='.' 'NF > 2 n=split($0, a); $0=""; for (i=2;i<=n;++i) $(NF+1)=a[i] 1' file
akamai.com
cloudflare.com
cdn.cloudflare.net
Here, whenever there are more than two dot-delimited fields, we split the current line on dots, and then re-create the current record from that, skipping the first field. The trailing 1
at the end causes every line (modified or not) to be printed.
Shorter awk
in the same fashion as the sed
solution:
$ awk -F '.' 'NF > 2 sub("^[^.]*.", "") 1' file
akamai.com
cloudflare.com
cdn.cloudflare.net
edited Jul 27 at 20:59
answered Jul 27 at 17:17
Kusalananda
101k13199311
101k13199311
It is possible to use only one regex in sed:sed '/.([^.]*.)/s//1/' infile
and the awk could be a lot smaller:awk '$0~resub(/^[^.]*./,"")1' re='^[^.]*\.([^.]*\.)' infile
â Isaac
Jul 27 at 21:03
Grep gets a bit complex:grep -P '^(?=[^.]*.[^.]*.)[^.]*.K.*|.*' infile
â Isaac
Jul 27 at 21:21
add a comment |Â
It is possible to use only one regex in sed:sed '/.([^.]*.)/s//1/' infile
and the awk could be a lot smaller:awk '$0~resub(/^[^.]*./,"")1' re='^[^.]*\.([^.]*\.)' infile
â Isaac
Jul 27 at 21:03
Grep gets a bit complex:grep -P '^(?=[^.]*.[^.]*.)[^.]*.K.*|.*' infile
â Isaac
Jul 27 at 21:21
It is possible to use only one regex in sed:
sed '/.([^.]*.)/s//1/' infile
and the awk could be a lot smaller: awk '$0~resub(/^[^.]*./,"")1' re='^[^.]*\.([^.]*\.)' infile
â Isaac
Jul 27 at 21:03
It is possible to use only one regex in sed:
sed '/.([^.]*.)/s//1/' infile
and the awk could be a lot smaller: awk '$0~resub(/^[^.]*./,"")1' re='^[^.]*\.([^.]*\.)' infile
â Isaac
Jul 27 at 21:03
Grep gets a bit complex:
grep -P '^(?=[^.]*.[^.]*.)[^.]*.K.*|.*' infile
â Isaac
Jul 27 at 21:21
Grep gets a bit complex:
grep -P '^(?=[^.]*.[^.]*.)[^.]*.K.*|.*' infile
â Isaac
Jul 27 at 21:21
add a comment |Â
up vote
1
down vote
You could approach this using the following methods:
perl -lpe '$_ = $1 if /.(.*..*)/' input-file.txt
wherein, we rely on a regex
that zeroes in on a dot .
character that can see another dot to it's right. Then whatever is to the right is captured and made available in $1
and stuffed inside the current line. The -p
option to Perl
then takes this to the stdout
as well as the one that didn't match as well.
perl -F\. -pale '$_ = join ".", splice @F, 1 if @F > 2' input.txt
Input file is read line-by-line via the
-p
option and autoprint is also turned on via this option as well.Each record is split on the dot
.
and the individual fields stored in the array@F
indexed starting from0
via the-a
option.-l
option makesRS = ORS = "n"
Only when we have more than 2 elements in the array
@F
, meaning there were atleast 2 dots in the current record, we select such a record for modifications.For such a record, the
splice @F, 1
function strips away elements 2nd onwards and presents them to thejoin
function which then joins them using the dot character, this then is stuffed inside the$_
, aka, current record.The
-p
option then takes this modified current record to the stdout. The one that didnot modify is anyway taken silently to the stdout.
Using GNU sed
we can also do the task without taking recourse of capturing parens:
sed -e
s/./n/2;T
y/n./.n/
s/n/./2g
s/.*n//
' input.file
Output:
akamai.com
cloudflare.com
cdn.cloudflare.net
add a comment |Â
up vote
1
down vote
You could approach this using the following methods:
perl -lpe '$_ = $1 if /.(.*..*)/' input-file.txt
wherein, we rely on a regex
that zeroes in on a dot .
character that can see another dot to it's right. Then whatever is to the right is captured and made available in $1
and stuffed inside the current line. The -p
option to Perl
then takes this to the stdout
as well as the one that didn't match as well.
perl -F\. -pale '$_ = join ".", splice @F, 1 if @F > 2' input.txt
Input file is read line-by-line via the
-p
option and autoprint is also turned on via this option as well.Each record is split on the dot
.
and the individual fields stored in the array@F
indexed starting from0
via the-a
option.-l
option makesRS = ORS = "n"
Only when we have more than 2 elements in the array
@F
, meaning there were atleast 2 dots in the current record, we select such a record for modifications.For such a record, the
splice @F, 1
function strips away elements 2nd onwards and presents them to thejoin
function which then joins them using the dot character, this then is stuffed inside the$_
, aka, current record.The
-p
option then takes this modified current record to the stdout. The one that didnot modify is anyway taken silently to the stdout.
Using GNU sed
we can also do the task without taking recourse of capturing parens:
sed -e
s/./n/2;T
y/n./.n/
s/n/./2g
s/.*n//
' input.file
Output:
akamai.com
cloudflare.com
cdn.cloudflare.net
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You could approach this using the following methods:
perl -lpe '$_ = $1 if /.(.*..*)/' input-file.txt
wherein, we rely on a regex
that zeroes in on a dot .
character that can see another dot to it's right. Then whatever is to the right is captured and made available in $1
and stuffed inside the current line. The -p
option to Perl
then takes this to the stdout
as well as the one that didn't match as well.
perl -F\. -pale '$_ = join ".", splice @F, 1 if @F > 2' input.txt
Input file is read line-by-line via the
-p
option and autoprint is also turned on via this option as well.Each record is split on the dot
.
and the individual fields stored in the array@F
indexed starting from0
via the-a
option.-l
option makesRS = ORS = "n"
Only when we have more than 2 elements in the array
@F
, meaning there were atleast 2 dots in the current record, we select such a record for modifications.For such a record, the
splice @F, 1
function strips away elements 2nd onwards and presents them to thejoin
function which then joins them using the dot character, this then is stuffed inside the$_
, aka, current record.The
-p
option then takes this modified current record to the stdout. The one that didnot modify is anyway taken silently to the stdout.
Using GNU sed
we can also do the task without taking recourse of capturing parens:
sed -e
s/./n/2;T
y/n./.n/
s/n/./2g
s/.*n//
' input.file
Output:
akamai.com
cloudflare.com
cdn.cloudflare.net
You could approach this using the following methods:
perl -lpe '$_ = $1 if /.(.*..*)/' input-file.txt
wherein, we rely on a regex
that zeroes in on a dot .
character that can see another dot to it's right. Then whatever is to the right is captured and made available in $1
and stuffed inside the current line. The -p
option to Perl
then takes this to the stdout
as well as the one that didn't match as well.
perl -F\. -pale '$_ = join ".", splice @F, 1 if @F > 2' input.txt
Input file is read line-by-line via the
-p
option and autoprint is also turned on via this option as well.Each record is split on the dot
.
and the individual fields stored in the array@F
indexed starting from0
via the-a
option.-l
option makesRS = ORS = "n"
Only when we have more than 2 elements in the array
@F
, meaning there were atleast 2 dots in the current record, we select such a record for modifications.For such a record, the
splice @F, 1
function strips away elements 2nd onwards and presents them to thejoin
function which then joins them using the dot character, this then is stuffed inside the$_
, aka, current record.The
-p
option then takes this modified current record to the stdout. The one that didnot modify is anyway taken silently to the stdout.
Using GNU sed
we can also do the task without taking recourse of capturing parens:
sed -e
s/./n/2;T
y/n./.n/
s/n/./2g
s/.*n//
' input.file
Output:
akamai.com
cloudflare.com
cdn.cloudflare.net
answered Jul 28 at 19:30
Rakesh Sharma
3333
3333
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f458906%2fsed-removing-everything-until-and-including-the-first-period-if-theres-more-tha%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password