rcwizard12435
rcwizard12435

Reputation: 63

How to trim strings in a file

I have a file like this:

10.123.214.214:445
124.4235.123:443
124.34352.124.1:80
2142354.1341:80
12435.12412:70

Is there a way I can print everything before the :? I am thinking awk or sed would be the best way, but I am not sure how to come up with the right command

Expected output:

10.123.214.214
124.4235.123
124.34352.124.1
2142354.1341
12435.12412

Upvotes: 0

Views: 79

Answers (4)

George Vasiliou
George Vasiliou

Reputation: 6335

Just for completeness, this is the sed equivalent to keep the first column of a : delimited file:

sed 's/:.*//g' file1

Upvotes: 0

Claes Wikner
Claes Wikner

Reputation: 1517

Another awk proposal.

awk -F: '{print $1}' file

10.123.214.214
124.4235.123
124.34352.124.1
2142354.1341
12435.12412

Upvotes: 1

WahhabB
WahhabB

Reputation: 520

In AWK, as follows:

BEGIN { FS=":" } 
{ print $1 }

This makes the colon your Field Separator, so the first word ($1) is everything before the first colon on the line.

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203324

$ cut -d: -f1 file
10.123.214.214
124.4235.123
124.34352.124.1
2142354.1341
12435.12412

It's the only thing cut exists to do - don't take away it's raison d'être :-).

Upvotes: 3

Related Questions