Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split Based On Text Change In The First Column 1

Status
Not open for further replies.

azekry

Technical User
Jul 28, 2015
8
EG
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
 
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
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top