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

Split Based On Text Change In The First Column

Split Based On Text Change In The First Column

Split Based On Text Change In The First Column

(OP)
I have an ascii file with 5 columns. In the first column after many rows the text changes to a different text e,g test1 and then after some rows test2 and so on. Is there a way to separate into different files based on that change e.g all the the 5 columns of test1 are saved to a file and then test2 and so on whenever the text in the first column changes.

Thanks

RE: Split Based On Text Change In The First Column

Next time you should post the example iinput file and show some code you tried.

I suppose that your imput file is sorted by the first column and looks like:
azekry.txt

CODE

foo 2 3 4 5
foo 2 3 4 5
foo 2 3 4 5
bar 2 3 4 5
bar 2 3 4 5
baz 2 3 4 5
baz 2 3 4 5
baz 2 3 4 5
baz 2 3 4 5 
then the following script
azekry.awk

CODE

# Run:
# awk -f azekry.awk azekry.txt
{
  if (value[$1]) {
    value[$1] = value[$1]++
  }
  else {
    value[$1] = 1
    fname = "azekry_"$1".txt"
    #print fname
  }
  print $0 > fname
} 
writes 3 new files:
azekry_foo.txt

CODE

foo 2 3 4 5
foo 2 3 4 5
foo 2 3 4 5 
azekry_bar.txt

CODE

bar 2 3 4 5
bar 2 3 4 5 
azekry_baz.txt

CODE

baz 2 3 4 5
baz 2 3 4 5
baz 2 3 4 5
baz 2 3 4 5 

RE: Split Based On Text Change In The First Column

(OP)
Thank you very much for your kind help this is really great. i apologize for not posting an example file. As for code i am really just starting to learn.

RE: Split Based On Text Change In The First Column

you are welcome

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