LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2010, 01:48 PM   #1
fredora
LQ Newbie
 
Registered: Nov 2010
Posts: 19

Rep: Reputation: 1
awk command help




Hi All,

I have a file with 3 columns.

Billy 50 Comments are in the third column

I need to use 'awk' and create a program that outputs all comments. (The third field).
The problem with my current command is I can only print the first word of the 3rd column. Is there a way to print the full line, other than {print $3,$4,$5...}

This is what I have so far...

Code:
awk '/[A-Za-z.]/ {print $3}' data0


Thank you,

Mike
 
Old 11-25-2010, 02:04 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
You can try:
Code:
awk '{ for(i=3; i<=NF; i++) {printf("%s ", $i)} print $NF }' file
 
Old 11-25-2010, 02:17 PM   #3
rikijpn
Member
 
Registered: Jun 2007
Location: Japan
Distribution: Debian lenny, DSL, Solaris 10
Posts: 157

Rep: Reputation: 33
This answer is uglier, but probably easier to understand if are not much experienced in programming/awk.

Code:
awk '{$1="";$2="";print substr($0,3)}'
This sets the first and second field to an empty string, and as the input field separators (spaces characters in this case) would remain, you'd have to print from the third character in the string (using substr).

A related page
.
 
Old 11-25-2010, 05:00 PM   #4
fredora
LQ Newbie
 
Registered: Nov 2010
Posts: 19

Original Poster
Rep: Reputation: 1
Thank you, both answers work. Much appreciated!
 
Old 11-25-2010, 05:23 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Just an alternative
Code:
awk '$2=$1="";sub(/^ */,"")' file
 
  


Reply



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
About awk command incomingid Linux - Newbie 5 04-01-2009 10:44 PM
help reg. awk command phone_book Linux - Newbie 1 03-20-2009 09:50 PM
awk command toaravind Linux - General 5 07-02-2008 01:25 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
the 'awk' command. iconicmoronic Linux - Newbie 2 04-08-2007 12:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:03 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration