LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-27-2007, 08:59 AM   #1
Hebron
LQ Newbie
 
Registered: Aug 2007
Posts: 1

Rep: Reputation: 0
awk command to split file


Hello, I have a file containing more than 200 columns. I need to split in into 2 files, both containing a little more than 100 column. I know you can use the command: awk '{print $1,$2, etc}' < input file > output file But doing it like this mean I have to type in all colums in the command ($1,$2,$3 etc). Is there a way to split the files into a certain amount of columns by giving a range in the command? Many thanks for help!!
 
Old 08-27-2007, 09:32 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
cut -f -100
cut -f 100-
man cut
 
Old 08-27-2007, 09:54 AM   #3
lakris
Member
 
Registered: Sep 2004
Location: Stockholm, Sweden
Distribution: Ubuntu, RedHat, SuSe, Debian, Slax
Posts: 102

Rep: Reputation: 15
Could You try this:
If tab separated
Quote:
cut -f -100 file > first
cut -f 101- file > second
or, if space separated
Quote:
cut -d" " -f -100 file > first
cut -d" " -f 101- file > second
haven't tried it myself yet...
 
Old 08-27-2007, 08:35 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Or, if the separator can be either SPACE or TAB (or both
plus something else), in awk
Code:
BEGIN{
  FS="add your separator(s) of choice"
  numf=numFields
}
{
  for(i=1;i<=NF;i++) {
    if(i <= numf) { 
      printf "%s ", $i >> "file1" 
      if(i==numf){ 
        printf "\n" >> "file1"
      }
    } else { 
      printf "%s ", $i >> "file2" 
      if(i==NF){ 
        printf "\n" >> "file2"
      }
    }
  }
}
Save as something.awk

Run like so:
Code:
awk -v numFields=102 -f something.awk your-file
Untested, as always, and w/o any warranty.


Cheers,
Tink
 
  


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
Split large file in several files using scripting (awk etc.) chipix Programming 14 10-29-2007 12:16 PM
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 11:10 AM
How to split file , .. awk or split ERBRMN Linux - General 9 08-15-2006 01:02 AM
awk messing up trying to split a unicode line by whitespace hedpe Programming 1 08-05-2006 12:10 PM
split files using awk (or similar) lgualteri Programming 1 06-13-2005 10:17 AM


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