Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

INPUT:

10.175.192.16   vgxyxgf.de.foo.net  asdf    34efg
10.175.192.17   sdtds.de.foo.net    xyyxv   e5432
10.175.243.20   asdfycfb.us.foo.net dfvasf  34525

OUTPUT:

10.175.192.16   vgxyxgf
10.175.192.17   sdtds
10.175.243.20   asdfycfb

How can I make the OUTPUT? Like "awk '{SOMEMAGIC}' foo.txt

So I have the "INPUT" and I need a /etc/hosts file style OUTPUT... :\

share|improve this question
    
Can you clarify your question? –  gabe. Jan 27 '12 at 1:01

3 Answers 3

up vote 5 down vote accepted

With awk

awk '{sub(/\..*/,"",$2);$0=$1 "  "$2}1' foo.txt

With sed

sed 's/^\(\([^.]*\.\)\{4\}\).*$/\1/;s/\.$//' foo.txt
share|improve this answer
    
I don't know how it works... but it work!! wow.. –  LanceBaynes Jan 26 '12 at 4:28
1  
@LanceBaynes Magic of course! What I did was replace everything after the first dot in the 2nd field $2 with a null string. I then told awk that the current line $0 is only made up of the first field $1 followed by two spaces followed by the (altered) second field $2 –  SiegeX Jan 26 '12 at 4:36

Just for fun, here's a non-awk, non-sed answer:

cut -f1-4 -d. foo.txt
share|improve this answer

Another awk solution, analogous to Steven's cut answer:

awk -F. -v OFS=. '{print $1, $2, $3, $4}' filename

Uses dots as the field separator, and prints the first 4 fields dot-separated.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.