Computing.Net > Forums > Linux > how to split a file with awk

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

how to split a file with awk

Reply to Message Icon

Name: yacob
Date: October 8, 2008 at 09:50:09 Pacific
OS: Linux
CPU/Ram: 2GB
Product: linux
Comment:

Dear All,

I have a file which contains the following records.

3.09 4.21 -20.96 150 130
4.39 5.47 -20.96 150 130
5.62 6.79 -21.00 170 150
6.94 9.00 -21.04 150 130
>
3.09 4.21 -20.96 150 130
4.39 5.47 -20.96 150 130
5.62 6.79 -21.00 170 150
6.94 9.00 -21.04 150 130


I want to read this file and as soon as I reach the sign ">" I want to the awk script to trigger a new file name and grouped all the records as soon as it reaches the ">". The result I want in this case should look like:

file1

3.09 4.21 -20.96 150 130
4.39 5.47 -20.96 150 130
5.62 6.79 -21.00 170 150
6.94 9.00 -21.04 150 130

and

file2
3.09 4.21 -20.96 150 130
4.39 5.47 -20.96 150 130
5.62 6.79 -21.00 170 150
6.94 9.00 -21.04 150 130

How one can do this using awk ?

Thank
you
Yacob Sen



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: October 9, 2008 at 16:07:27 Pacific
Reply:

IMO, awk is not the best tool for this problem, but here is a way:


#!/bin/bash

awk ' BEGIN { x=0 }
{
if(/>/)
{
x=1
continue
}
if ( x == 0)
print $0 > "file1"

if ( x == 1)
print $0 > "file2"
} ' datafile



0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: how to split a file with awk

How to lock a file www.computing.net/answers/linux/how-to-lock-a-file/27440.html

how to create a file in graphical www.computing.net/answers/linux/how-to-create-a-file-in-graphical/23740.html

how to open a pdf files www.computing.net/answers/linux/how-to-open-a-pdf-files/25112.html