Parse a String for a Specific Word

Hello,

I'm almost there with scripting, and I've looked at a few examples that could help me out here. But I'm still at a lost where to start. I'm looking to parse each line in the log file below and save the output like below.

Log File

AABBCGCAT022|242|3
AABBCGCAT023|243|4
PLATODCAT024|244|5
PLATODCAT025|245|6
SOCRATCAT026|246|7
SOCRATCAT027|247|8

Desired Output

GCAT022
      AABBC        3
GCAT023
     AABBC        4
File name to save as /tmp/aabb.csv

DCAT024
      PLATO       5
DCAT025
      PLATO       6
File name to save as /tmp/plat.csv

TCAT026
      SOCRA      7
TCAT027
      SOCRA      8
File name to save as /tmp/socra.csv

Try this:

awk -F\| '{print substr($1,6,7) RS "\t" substr($1,1,5) "\t" $3 > tolower(substr($1,1,4))".csv"}' file

Thanks Franklin! It works as expect! I just have to figure out expressions then I might be able do what you did there, LOL!

---------- Post updated at 10:55 AM ---------- Previous update was at 06:47 AM ----------

Franklin,

My apologies, I noticed the following error, and it's not outputing a portion of the log cause I fail to mention I'm running Solaris.

awk: too many output files 10
record number 157

There are too many files open, try this:

awk -F\| '{
  fn=tolower(substr($1,1,4)) ".csv"
  print substr($1,6,7) RS "\t" substr($1,1,5) "\t" $3 > fn
  close(fn)
}' file

Use nawk or /usr/xpg4/bin/awk on Solaris.

nawk works, however for each file create it only has the first line from the Log File in them.

NAWK OUTPUT

      
the saved file /tmp/aabb.csv only has one line in it  like below.
GCAT022
      AABBC        3
the saved /tmp/plat.csv file only has one line in it like below.
DCAT024
      PLATO       5

the saved file /tmp/socra.csvonly has one line in it like below.

TCAT026
      SOCRA      7

Please see above for Desired Output.

Thanks for your help again Franklin!

try this:

print substr($1,6,7) RS "\t" substr($1,1,5) "\t" $3 >> fn

Yes! Works perfectly now! I did read about the switch to append to a current file, guess I won't forget it now. Thanks for all your help!