MitchellK
MitchellK

Reputation: 2632

Remove period character from beginning of line with sed or awk

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

Answers (3)

Claes Wikner
Claes Wikner

Reputation: 1517

awk '/^.d/sub(/./,"")' file

doubleclick.com
doubleclick.net
------------0-------------0.0n-line.info
----0.0----0.1596.hk

Upvotes: -2

Akshay Hegde
Akshay Hegde

Reputation: 16997

Since tagged

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

Sundeep
Sundeep

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

Related Questions