LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-27-2007, 11:57 PM   #1
ovince
Member
 
Registered: Mar 2007
Posts: 77

Rep: Reputation: 15
selective printing out


Hi,

I have a large file with about 160 columns and I would like to modify it a bit. Namely I do not need some columns. I do not need columns

from 12 to 18

from 28 to 33

from 36 to 44

and so on


I started with awk like:

awk 'BEGIN{FS="|"} {for(i=1;i<=NF;i++) print $1, $2

and stoped because I realised that I do not want to write all coulmns in this awk command. Is there a way to print out just certain columns writing intervals? Something like:


awk 'BEGIN{FS="|"} {for(i=1;i<=NF;i++) print $1-$11, $28-$33 ....}'

I prefere awk solution since I need to do some arithmetic operation on some columns BUT other non-awk solutions are also welcome


thanks
oliver
 
Old 04-28-2007, 12:27 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
This won't help with the arithmetic, but

Code:
man 1 cut
was designed for exactly this.

Hope this helps.
 
Old 04-28-2007, 01:30 PM   #3
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
based on
Quote:
I do not need columns
from 12 to 18
from 28 to 33
from 36 to 44
and so on
, this works:
Code:
awk -F\|                   \
'{for (i=1;i<=NF;i++)      \
   if (                    \
        (i<12) ||          \
        (i>18 && i<28) ||  \
        (i>33 && i<36) ||  \
        (i>44)             \
      ) printf "%3d", $i;  \
   print ""}'
Well, technically this is what worked:
Code:
echo {1..50} |  awk        \
'{for (i=1;i<=NF;i++)      \
   if (                    \
        (i<12) ||          \
        (i>18 && i<28) ||  \
        (i>33 && i<36) ||  \
        (i>44)             \
      ) printf "%3d", $i;  \
   print ""}'                       

  1  2  3  4  5  6  7  8  9 10 11 19 20 21\
 22 23 24 25 26 27 34 35 45 46 47 48 49 50
Note: I folded the output (I added the '\') to reduce horizontal scrolling when folks read this.
 
  


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
X selective translucency? shishimo Linux - Software 1 06-22-2005 07:31 PM
selective batch copying chakkerz Linux - Newbie 2 06-16-2004 01:47 AM
protocol selective malfunction? CloudBuilder Linux - Networking 0 03-22-2004 04:36 PM
Selective startup LemonSHARK Red Hat 1 03-02-2004 05:25 PM
How selective is your memory? xailer Linux - Newbie 12 12-03-2003 11:36 PM

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

All times are GMT -5. The time now is 07:19 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration