INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Separating a Large File

Separating a Large File

(OP)
Hello Guys, please help me. I'm an awk begginner whit this problem:

I have a large file like this:

DSV1 UWI: 3060000134 Depth: Conf. Factor:
DSV2 Common: K1292
DSV3 Survey Name: DevSurvey Method:
DSV4 Company: UNKNOWN Date:
DSV5 Remarks:
DSV6 Measr. Depth Deviation Direction
DSV7 0.0000 0.0000 180.0000
DSV7 30.0000 0.5000 196.7200
DSV7 60.0000 0.5000 349.6000
DSV1 UWI: 3060000134 Depth: Conf. Factor:
DSV2 Common: K1292
DSV3 Survey Name: Nuevos_Dir_Nov04 Method:
DSV4 Company: UNKNOWN Date:
DSV5 Remarks:
DSV6 Measr. Depth Deviation Direction
DSV7 0.0000 0.0000 0.0000
DSV7 0.0001 0.0000 0.0000
DSV7 51.0000 0.1200 170.4900
DSV7 81.0000 0.0900 188.6600
DSV7 111.0000 0.0700 128.3600
DSV7 141.0000 0.4300 263.2100
DSV7 171.0000 0.6200 102.3500
DSV1 UWI: 3060000988 Depth: Conf. Factor:
DSV2 Common: K1296
DSV3 Survey Name: RMA_21.84_13102010 Method:
DSV4 Company: UNKNOWN Date:
DSV5 Remarks:
DSV6 Measr. Depth Deviation Direction
DSV7 0.0000 0.0000 0.0000
DSV7 30.0000 0.5000 196.7200
DSV7 60.0000 0.5000 348.4000


which I need to separate into several files. The file name will be taken
from the record DSV2, but if the name is repeated then the filename should
have a different name. The delimiters are DSV1 and DSV7(which repeats).

Desired output:
Filename: K1292.dat
DSV1 UWI: 3060000500 Depth: Conf. Factor:
DSV2 Common: K1292
DSV3 Survey Name: DevSurvey Method:
DSV4 Company: UNKNOWN Date:
DSV5 Remarks:
DSV6 Measr. Depth Deviation Direction
DSV7 0.0000 0.0000 180.0000
DSV7 30.0000 0.5000 196.7200
DSV7 60.0000 0.5000 349.6000

Filename: K1292_v2.dat
DSV1 UWI: 3060000134 Depth: Conf. Factor:
DSV2 Common: K1292
DSV3 Survey Name: Nuevos_Dir_Nov04 Method:
DSV4 Company: UNKNOWN Date:
DSV5 Remarks:
DSV6 Measr. Depth Deviation Direction
DSV7 0.0000 0.0000 0.0000
DSV7 0.0001 0.0000 0.0000
DSV7 51.0000 0.1200 170.4900
DSV7 81.0000 0.0900 188.6600
DSV7 111.0000 0.0700 128.3600
DSV7 141.0000 0.4300 263.2100
DSV7 171.0000 0.6200 102.3500

Filename: K1296.dat
DSV1 UWI: 3060000988 Depth: Conf. Factor:
DSV2 Common: K1296
DSV3 Survey Name: RMA_21.84_13102010 Method:
DSV4 Company: UNKNOWN Date:
DSV5 Remarks:
DSV6 Measr. Depth Deviation Direction
DSV7 0.0000 0.0000 0.0000
DSV7 30.0000 0.5000 196.7200
DSV7 60.0000 0.5000 348.4000

I'm starting with:

awk '/DSV1/,/DSV7/{
if ($1 ~ /DSV2/ )

but I don't know how to hande repeated values of DSV7.



Thanks in advance Guys.

RE: Separating a Large File

Something like this

CODE

# Run: awk -f split_file.awk split_file.txt
BEGIN {
}

/DSV1/ {
  if (filename) {
    # ouput the lines
    print "Writing file: " filename
    print lines > filename
    lines = ""  
  }
}
/DSV2/ {
  filename = $3
  filenames[filename] += 1
  if (filenames[filename] > 1)  {
    filename = filename "_v" filenames[filename] 
  }
}
{
  lines = lines $0 "\n"
}

END {
  if (filename) {
    # ouput the lines
    print "Writing file: " filename
    print lines > filename
  }  
} 

Output:

CODE

$ awk -f split_file.awk split_file.txt
Writing file: K1292
Writing file: K1292_v2
Writing file: K1296 
writes files:

K1292

CODE

DSV1 UWI: 3060000134 Depth: Conf. Factor:
DSV2 Common: K1292
DSV3 Survey Name: DevSurvey Method:
DSV4 Company: UNKNOWN Date:
DSV5 Remarks:
DSV6 Measr. Depth Deviation Direction
DSV7 0.0000 0.0000 180.0000
DSV7 30.0000 0.5000 196.7200
DSV7 60.0000 0.5000 349.6000 

K1292_v2

CODE

DSV1 UWI: 3060000134 Depth: Conf. Factor:
DSV2 Common: K1292
DSV3 Survey Name: Nuevos_Dir_Nov04 Method:
DSV4 Company: UNKNOWN Date:
DSV5 Remarks:
DSV6 Measr. Depth Deviation Direction
DSV7 0.0000 0.0000 0.0000
DSV7 0.0001 0.0000 0.0000
DSV7 51.0000 0.1200 170.4900
DSV7 81.0000 0.0900 188.6600
DSV7 111.0000 0.0700 128.3600
DSV7 141.0000 0.4300 263.2100
DSV7 171.0000 0.6200 102.3500 

K1296

CODE

DSV1 UWI: 3060000988 Depth: Conf. Factor:
DSV2 Common: K1296
DSV3 Survey Name: RMA_21.84_13102010 Method:
DSV4 Company: UNKNOWN Date:
DSV5 Remarks:
DSV6 Measr. Depth Deviation Direction
DSV7 0.0000 0.0000 0.0000
DSV7 30.0000 0.5000 196.7200
DSV7 60.0000 0.5000 348.4000 

RE: Separating a Large File

I overlooked that you want output with extension *.dat
So this is the corrected source

CODE

# Run: awk -f split_file.awk split_file.dat
/DSV1/ {
  if (filename) {
    # ouput the lines
    print "Writing file: " filename
    print lines > filename
    lines = ""  
  }
}
/DSV2/ {
  filename = $3
  filenames[filename] += 1
  if (filenames[filename] > 1)  {
    filename = filename "_v" filenames[filename] 
  }
  filename = filename ".dat"
}
{
  lines = lines $0 "\n"
}

END {
  if (filename) {
    # ouput the lines
    print "Writing file: " filename
    print lines > filename
  }  
} 

RE: Separating a Large File

(OP)
WOW Guys, you are Good !!!. I have a lot to learn about awk.

Both Codes work good.

I'll try to understand what they do.


Thank You very much.



Greetings.


Raul

RE: Separating a Large File

I have lot to learn from PHV too smile

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close