LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 04-28-2006, 12:10 AM   #1
blanny
Member
 
Registered: Dec 2002
Location: CA,USA
Distribution: RHCE in training :)
Posts: 57

Rep: Reputation: 15
Awk question


Hi, Can anyone tell me how I can use awk to print out iport in this list?

24.232.133.87:8080 elite proxy Argentina (Munro)
62.178.214.128:7212 elite proxy Austria (Vienna)
201.24.189.149:6588 elite proxy Brazil (Brasília)
201.36.157.84:6588 elite proxy Brazil (Fortaleza)
200.159.255.70:3128 elite proxy Brazil (Rio De Janeiro)
200.244.50.206:6588 elite proxy Brazil (Rio De Janeiro)

For some reason awk -F: '{print $1,$2}' iplist spits out the entire thing.

Thanks
 
Old 04-28-2006, 12:30 AM   #2
vbisis
Member
 
Registered: Dec 2005
Distribution: Slackware
Posts: 250

Rep: Reputation: 34
Don't set separator as :, there no : after the port, so awk will output the rest of the line as $2.

You could use default output separator blank and just print out $1.
 
Old 04-28-2006, 12:32 AM   #3
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 12.04
Posts: 913

Rep: Reputation: 32
Code:
awk '{print $1}' | awk -F: '{print $2}'
There may be a better way, but this should do the job.
Code:
8080
7212
6588
6588
3128
6588
[EDIT] Ah, you want both the IP address and port. Then yes, vbisis's suggestions should work:
Code:
awk '{print $1}'

Last edited by daihard; 04-28-2006 at 12:35 AM.
 
Old 04-28-2006, 04:04 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Why use heavy awk for this, when a simple "cut" is all you need?

Code:
cut -f1
Pipe output of that into:
Code:
cut -d: -f1
or
cut -d: -f2
to get resp. the IP and the port separately.
 
Old 04-28-2006, 09:18 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
cut is a choice I often overlook because I learned about awk 1st.

A problem w/ cut for step 1: is the "IP:port" separated from the rest of the line by a space or a tab? awk doesn't care, cut does.

Also, if I am interpreting the orig. Q correctly, the desired output form is:
IP port
IP port
...

Using awk:
Code:
awk '{print $1}'  | awk -F: '{print $1 "  " $2}'
## or
awk '{print $1}'  | awk -F: '{print $1 "\t" $2}'
W/ cut:
Code:
cut -d' ' -f1  | cut -d: -f1-2 --output-delimiter="  "
To me, awk is easier to use when the output needs any formatting at all.
 
Old 05-02-2006, 02:11 AM   #6
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Agreed that awk is very handy and often useful to do a bunch of stuff (like formatting) all at once.
Downside, for me at least, is that, since awk is so powerful, it can be a little tricky to master.
And cut's failure to work well with regex's is indeed a downside.

However, I recommend using the commands for what they were originally designed. So I prefer not forcing "cut" to alter it's output (it wasn't designed for output formatting, unlike awk or sed). Instead, use a simple tool like sed (stream editing) or tr (transliteration) to do the formatting. Advantage of using sed or tr is, that

So,
Code:
cut -d' ' -f1  | cut -d: -f1-2 --output-delimiter="  "
then becomes something like
Code:
cut -d' ' -f1 | tr ":" " "
or
cut -d' ' -f1 | sed -e 's/:/  /'
 
Old 05-02-2006, 08:03 AM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
Thanks for the reminder about tr, another one I forget because I jumped right into sed & awk. (Literally -- a friend gave the O'Reilly book.)

While we're discussing string processing tools, let's not forget cat's options, it can do much more than just concatenate (RTFM cat).


This reminds me of quotation I ran into recently, which I paraphrase here:

Never use awk when cut will do.
Never use cut when sed will do.
Never use sed when tr will do.
Never use tr when cat will do.
Avoid cat at all costs.

Best I can manage from memory. If someone knows the original, please post a reference.

I would modernize this by adding
"Never use perl when awk will do."
at the beginning.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
awk question puishor Programming 2 08-26-2005 09:44 AM
awk question denalitastic Linux - Newbie 1 06-07-2005 10:42 AM
AWK question paraiso Linux - Newbie 5 05-12-2005 01:37 PM
awk question jonlake Linux - Software 2 05-04-2005 03:58 PM
awk question dilberim82 Programming 2 10-01-2003 07:12 PM


All times are GMT -5. The time now is 07:48 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration