LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-02-2012, 05:47 PM   #1
AHJ
LQ Newbie
 
Registered: Jun 2012
Posts: 6

Rep: Reputation: Disabled
Delete odd (or even) fields with awk


Hi,

I have a tab delimited file that has duplicate columns (hundreds):
> cat realfile
1 1 0 0 1 1
1 1 0 0 0 0
0 0 1 1 1 1
0 0 1 1 0 0
0 0 0 0 0 0


I want to produce:
> cat wishfile
1 0 1
1 0 0
0 1 1
0 1 0
0 0 0

I know I could do
awk '{print $1, $3, $5)' realfile > wishfile

But it seems silly. I know there must be a better way, since we can index the columns, but can we use $X as the index instead of the number? Any other command that works is fine too, it doesn't have to be in awk.

Thanks for taking the time to help me
 
Old 08-02-2012, 08:52 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
I am not sure I understand The first line makes sense if consider only duplicates that are side by side, however the second line is not following the same
process otherwise it would have only 2 numbers on the line. Based on the first line I would expect your output to look like:
Code:
1 0 1
1 0
0 1
0 1 0
0
Please explain further what your requirement is?
 
Old 08-02-2012, 09:33 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920
i think s/he wants to print columns 1 3 5 7 ... (or 2 4 6 8 ...)

i tried this but it is not printing a newline 'tween records:
Code:
awk '{for(x=1;x<=NF;++x)if(x % 2 == 0)print $x}' ahj.txt 
1
0
1
1
0
0
0
1
1
0
1
0
0
0
0
[schneidz@hyper ~]$ awk '{for(x=1;x<=NF;x++)if(x % 2 == 0)print "x = " x " - " $x}' ahj.txt 
x = 2 - 1
x = 4 - 0
x = 6 - 1
x = 2 - 1
x = 4 - 0
x = 6 - 0
x = 2 - 0
x = 4 - 1
x = 6 - 1
x = 2 - 0
x = 4 - 1
x = 6 - 0
x = 2 - 0
x = 4 - 0
x = 6 - 0
 
Old 08-02-2012, 09:52 PM   #4
AHJ
LQ Newbie
 
Registered: Jun 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thank you for trying

Yes, I want to print all odd columns. The test files work with just a few columns and the command that states all the columns to be printed by number.

In the meantime I actually found an option in a classic genetics tool that does this, so my immediate problem is solved, but I would still like to know how to do this. Google tells me that because the indices are not stored somewhere we can get them, it's hard to define a range of fields in awk, maybe this is a surprisingly difficult problem for the same reason?
 
Old 08-03-2012, 01:08 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
How about:
Code:
awk '{for(x=1;x<=NF;x++)if(x % 2)printf "%s", $x (x == NF || x == (NF-1)?"\n":" ")}' ahj.txt
Or with ruby:
Code:
ruby -ape '$_="\n";$F.each_index{|x| $_.insert(0, "#{$F[x]} ") if x % 2 == 1}' ahj.txt
 
1 members found this post helpful.
Old 08-03-2012, 03:59 PM   #6
AHJ
LQ Newbie
 
Registered: Jun 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Wonderful, thank you grail
 
  


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: delete space between two fields while keep the format for other part cristalp Programming 1 10-04-2011 11:29 AM
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
[SOLVED] get fields using awk ashok.g Programming 9 12-09-2009 01:21 AM
modify all fields in awk tostay2003 Programming 16 08-09-2008 01:41 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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