Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 08-27-2007, 08:59 AM   #1
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
Senior Member
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

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

Rep: Reputation: 15
Could You try this:
If tab separated
cut -f -100 file > first
cut -f 101- file > second
or, if space separated
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
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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

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



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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:30 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration