Reputation: 2632
hope somebody can help me. I am trying to remove a .
period character from the beginning of a line in a large list.
Example input list
.doubleclick.com
.doubleclick.net
0------------0-------------0.0n-line.info
0----0.0----0.1596.hk
If have tried this sed pipe sed 's/^.//' < input.txt > output.txt
But this gives me
doubleclick.com
doubleclick.net
------------0-------------0.0n-line.info
I have tried various other suggestions I found but nothing works as it is stripping the first period but also 0.
from a domain name like 0------------0-------------0.0n-line.info
I need to only strip the first dot and nothing else.
Upvotes: 2
Views: 2454
Reputation: 1517
awk '/^.d/sub(/./,"")' file
doubleclick.com
doubleclick.net
------------0-------------0.0n-line.info
----0.0----0.1596.hk
Upvotes: -2
Reputation: 16997
Since tagged awk
awk 'sub(/^\./,"")+1' infile
Explanation:
sub(..)
returns true, if substitution was made, and hence record will be printed, if not below +1
takes care of printing.
+1
at the end does default operation that is print current/record/row, print $0
. To know how awk works try, awk '1' infile
, which will print all records/lines, whereas awk '0' infile
prints nothing. Any number other than zero is true, which triggers the default behaviour.
Test Results:
$ cat f2
.doubleclick.com
.doubleclick.net
0------------0-------------0.0n-line.info
0----0.0----0.1596.hk
$ awk 'sub(/^\./,"")+1' f2
doubleclick.com
doubleclick.net
0------------0-------------0.0n-line.info
0----0.0----0.1596.hk
Upvotes: 3
Reputation: 23667
.
is a meta-character that matches any character, so you need to escape it to match it literally
$ # can also use: sed 's/^[.]//' ip.txt
$ sed 's/^\.//' ip.txt
doubleclick.com
doubleclick.net
0------------0-------------0.0n-line.info
0----0.0----0.1596.hk
See also sed manual - Overview of basic regular expression syntax
Upvotes: 4