LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 01:27 AM   #1
dhodho
LQ Newbie
 
Registered: Jun 2010
Location: Indonesia
Distribution: Ubuntu 10.04
Posts: 26

Rep: Reputation: 15
[ask awk] remove certain row in a column


Hi, everyone.

I am newbie in awk programming.
If I have input file like below

2006 2 3
2007 1 2
2008 2 4
2009 1 3
2010 2 1

I wish get output file like:

2006 2
2007 1
2008 2
2009 1 3
2010 2 1

Is it possible to get such output file using awk?
Thanks very much.
 
Old 06-08-2010, 01:49 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Your example isn't very clear: Do you only want to remove the third column if the year (first column) is 2008 or less?
 
Old 06-08-2010, 02:21 AM   #3
dhodho
LQ Newbie
 
Registered: Jun 2010
Location: Indonesia
Distribution: Ubuntu 10.04
Posts: 26

Original Poster
Rep: Reputation: 15
Sorry for unclear explanation from my previous email. Ok, I will explain what I mean.

Below my real input file :

20080901 0.000024
20080902 0.000020
20080903 0.000021
20080904 0.000032
20080905 0.000000
20080906 0.000000
20080907 0.000013
20080908 0.000009
20080909 0.000000
20080910 0.000015
20080911 0.000050
20080912 0.000028
20080913 0.000020
...

I want to remove the fourth first row of the second column, so I wish get output file like:

20080901
20080902
20080903
20080904
20080905 0.000000
20080906 0.000000
20080907 0.000013
20080908 0.000009
20080909 0.000000
20080910 0.000015
20080911 0.000050
20080912 0.000028
20080913 0.000020

Is it possible getting such above output file using awk?
Thanks ..
 
Old 06-08-2010, 02:29 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Check out the NR builtin variable. Should be dead easy.
 
Old 06-08-2010, 02:31 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,144

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
Very simple - check the record number is less than 5 and set field 2 to null.
Any decent tutorial should give you the leads you need.
 
Old 06-08-2010, 06:07 AM   #6
dhodho
LQ Newbie
 
Registered: Jun 2010
Location: Indonesia
Distribution: Ubuntu 10.04
Posts: 26

Original Poster
Rep: Reputation: 15
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.
 
Old 06-08-2010, 06:10 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Great to see you got what you wanted.

Would you please tag this thread as Solved (See Thread Tools).
 
Old 06-08-2010, 06:33 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,144

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
Whilst anyone with a C background can see what you've done, I tend toward the KISS principle. How about something like
Code:
awk 'NR<5 {$2=""}1' input.dat
(keep an eye an awk threads - these people have taught me plenty).
 
Old 06-08-2010, 07:09 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A bit more cryptic... he he..
Code:
awk '$0 = $(NR < 5)' input.dat
 
Old 06-08-2010, 07:14 AM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,144

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
That being one of the guilty parties I was referring to ...
(what happens if more than 2 fields .... ????)

Last edited by syg00; 06-08-2010 at 07:17 AM.
 
Old 06-08-2010, 07:18 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
hehe ... nice one
 
Old 06-08-2010, 07:20 AM   #12
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
Quote:
Originally Posted by syg00 View Post
Code:
awk 'NR<5 {$2=""}1' input.dat
Side question here. I've seen this final "1" used on the end of an awk expression a few times now, and ran a few tests that tell me it makes awk print out every line, modified or not. But I've yet to track down any documentation that explains what it's doing. Could you fill me in a bit here?
 
Old 06-08-2010, 07:36 AM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by syg00 View Post
That being one of the guilty parties I was referring to ...

Quote:
(what happens if more than 2 fields .... ????)
Well... nothing special. All the fields but the first one are removed from the first 4 lines. Trying to remove only the last field brings to more complicate expressions (and bad programming, too).
 
Old 06-08-2010, 07:41 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Any true statement, ie equal to not 0, is considered to take on the default action which is to print.
 
1 members found this post helpful.
Old 06-08-2010, 07:45 AM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by David the H. View Post
Side question here. I've seen this final "1" used on the end of an awk expression a few times now, and ran a few tests that tell me it makes awk print out every line, modified or not. But I've yet to track down any documentation that explains what it's doing. Could you fill me in a bit here?
You have to take in mind two things:
1) In awk TRUE is represented by a non-null string or a numeric value other than 0, FALSE is the null-string or 0.
2) An awk rule is made of
Code:
pattern { action }
when the action is not specified, the default one is applied (that is print $0).
Said that the following are very simple programs that evaluates the pattern as TRUE and apply the default action:
Code:
awk 1 input.dat
awk 2 input.dat
awk 3.14159265358979 input.dat
where you see that the numeric value is not necessarily 1.

Edit: ...as grail said using much less words!

Last edited by colucix; 06-08-2010 at 07:47 AM.
 
1 members found this post helpful.
  


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:31 AM.

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