Compare Field in Current Line with Field in Previous

Hi Guys

I have the following file

Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above:

Basically, I want to print the lines of the file as long as the second field of the current line is equal to the second field of the previous line. And i want to print them in a file called "second field".txt

Any help appreciated. Thanks.

Try:

awk -F, '{print > $2".txt"}' file

Thanks Bartus

I found something similar in the meantime

awk -F',' '{print> $NF}' file

However i'm getting the following error:

awk: too many output files 10

i.e. awk creates rightly the first 10 files but stopped after that

does anybody have any idea?

---------- Post updated at 11:18 AM ---------- Previous update was at 11:15 AM ----------

Ok i fixed it using nawk

---------- Post updated at 11:28 AM ---------- Previous update was at 11:18 AM ----------

Actually it's better with nawk but not entirely fixed

i am getting another error now

nawk: makes too many open files

can anybody help?

What kind of os are you on...your last error makes no sense.

Solaris 10 11/06

On Solaris use nawk instead of awk and how many input/output files are being generated...

thanks shamrock. you probably didn't see my earlier post but i already found that nawk helped

that said i'm having the following issue now:

nawk: makes too many open files

looks like nawk stops after 20 files...

Can you post the command you are using and again how many input files are there and how many output files are being generated...as it is strange that nawk would complain about such a thing.

Close the file descriptor:

awk -F',' '{print> $NF; close($NF)}' file
1 Like

Thanks Franklin

I am not getting the "too many files" error anymore but nawk is only printing one record per file now...The last one of each section.

Any idea guys?:wall:

---------- Post updated at 04:18 AM ---------- Previous update was at 04:04 AM ----------

ok fixed it with ">>" instead of ">"

awk -F',' '{print>> $NF; close($NF)}' file

Many Thanks for the help guys. Much appreciated

---------- Post updated at 04:18 AM ---------- Previous update was at 04:18 AM ----------

ok fixed it with ">>" instead of ">"

awk -F',' '{print>> $NF; close($NF)}' file

Many Thanks for the help guys. Much appreciated