LinuxQuestions.org
Help answer threads with 0 replies.
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 06-08-2010, 07:47 AM   #16
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192

Well if only to remove the last field, irrelevant of field number, then the first answer slightly changed works well:
Code:
awk 'NR < 5{$NF = ""}1' input_file
 
Old 06-08-2010, 07:48 AM   #17
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Thanks colucix, saves me finding your previous explanation.
"1" is fine for me - I am not going to start using pi to 14 decimals ....
 
Old 06-08-2010, 08:18 AM   #18
rkski
Member
 
Registered: Jan 2009
Location: Canada
Distribution: CentOS 6.3, Fedora 17
Posts: 247

Rep: Reputation: 51
Code:
Thanks for your clue. I used awk '{for(i=NR;i<=4;i++)$2="";print $0}' input.dat > output dat and I got what I needed.
in the for statement you are calculating $2="" 5 times, then 4 times,... until NR >= 5 and then for statement is just tested&fails..which is a waste.

sysg00 soln is much better


2nd point: about the 1 trick. I wasn't sure about it till it was explained here. Basically easier to write 1 (or any true) than {print}.

Last edited by rkski; 06-08-2010 at 08:20 AM.
 
Old 06-08-2010, 09:36 AM   #19
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Yes, grail was more succinct, but colucix fleshed it out. Thanks to both of you for the explanations. I'd already discovered that it didn't have to be 1 exactly, so I suspected it was something like that, but it's nice to have it confirmed.

So it's basically just a shortcut for this:
Code:
awk 'NR<5 {$2=""} {print}' input.dat
And like syg00, I don't think I'll be playing around with it much either.
 
  


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
how to grep awk or sed the first row and column Bone11409 Linux - Newbie 2 03-21-2010 08:18 PM
transpose row to column? resolute155 Programming 3 09-07-2009 02:29 PM
how to get the second column and first row data from a file??? loplayers Linux - Newbie 3 11-05-2007 07:35 PM
Transposing a column into a row mayyash Linux - General 1 09-30-2005 02:23 AM
How to write the contents of a column as a row in linux markraem Linux - Software 4 07-17-2004 07:35 PM

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

All times are GMT -5. The time now is 04:48 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