AWK split

Dear colleagues! I want to create a script which will take each file from the list and then parse it filename with awk/split. I do it this way:

for file in `cat /$FileListFN`; do
     echo `awk   '
                 {N=split(FILENAME,FNParts,"_")}
                 {for (i=1; i<=N; i++)
                    {print FNParts " | "}
                 }
                 ' $file`
done

I use this file list as a source :

BD52_New_28_04_09.xml
BD5A_New_28_04_09.xml
BD62_New_28_04_09.xml
BD6A_New_28_04_09.xml
BD72_New_28_04_09.xml
BD7A_New_28_04_09.xml
BD82_New_28_04_09.xml
BD8A_New_28_04_09.xml

But when I run it I get the following:

BD52 | New | 28 | 04 | 09.xml | BD52 | New | 28 | 04 | 09.xml | BD52 | New | 28 | 04 | 09.xml |
BD5A | New | 28 | 04 | 09.xml | BD5A | New | 28 | 04 | 09.xml | BD5A | New | 28 | 04 | 09.xml |
BD62 | New | 28 | 04 | 09.xml | BD62 | New | 28 | 04 | 09.xml | BD62 | New | 28 | 04 | 09.xml |
BD6A | New | 28 | 04 | 09.xml | BD6A | New | 28 | 04 | 09.xml | BD6A | New | 28 | 04 | 09.xml |
BD72 | New | 28 | 04 | 09.xml | BD72 | New | 28 | 04 | 09.xml | BD72 | New | 28 | 04 | 09.xml |
BD7A | New | 28 | 04 | 09.xml | BD7A | New | 28 | 04 | 09.xml | BD7A | New | 28 | 04 | 09.xml |
BD82 | New | 28 | 04 | 09.xml | BD82 | New | 28 | 04 | 09.xml | BD82 | New | 28 | 04 | 09.xml |
BD8A | New | 28 | 04 | 09.xml | BD8A | New | 28 | 04 | 09.xml | BD8A | New | 28 | 04 | 09.xml |

Why my strings repeat three times? I did everything as it was shown in examples. I try to do it on Solaris.

Thank you in advance!

What is the content of the variables $FileListFN and $file and what should be the desired output?

Regards

$FileListFN contains the full name of list-file. $file is a current processing file. Idea is the following - I want to have two arrays, one with parsed filename (FNPart) and second with parsed filename template (Tag). Then I could produce the following string <Tag[1]><FNPart[1]></Tag[1]> ... <Tag[n]><FNPart[n]></Tag[n]> and then insert it into each XML between first "><" combination. So I don't want to output arrays, I want to produce the full string and then use it.

---------- Post updated at 03:08 PM ---------- Previous update was at 02:56 PM ----------

By the word, now I know what happened - each my XML has exactly three strings inside, so FILENAME was parsed three times...

This should be sufficient:

awk '$1=$1' FS="_" OFS=" | " file

If u just want to replace '_' with '|' can be done following ways..

cat filename|sed 's/_/|/g' OR sed 's/_/|/g' filename

Thank you very much for your answers! Now I am trying to use a variable from main script in awk program. I passed this variable into my awk program, but it doesn't work correctly. The script is given below:
awk '{print $FNT}' $file -v FNT=$FileNameTemplate
file here is a real XML fliename, FileNameTemplate is something like JFJF_New_07_09_09. I expect this script to output the value of the variable, but it outputs my XML file and then waits for an input, doing nothing and repeating my input after Enter button pressed... Please tell me where is my mistake and what should I read before I will ask such questions.

Not quite clear with what exactly you want but try this...

awk -v FNT=${FileNameTemplate} '{print FNT}' $file

Malcomex999, sorry, your method doesn't work, shell says:

awk: syntax error near line 1
awk: bailing out near line 1

for each file.

as always.... if on Solaris, use either /usr/bin/nawk or /usr/xpg4/bin/awk.
To pass shell variable to awk/nawk:

nawk -v FNT="${FileNameTemplate}" '{print FNT}' $file
OR
nawk '{print FNT}' FNT="${FileNameTemplate}" $file

Yes, it is Solaris... I will try this and answer ASAP, thank you!

---------- Post updated Sep 8th, 2009 at 10:54 AM ---------- Previous update was Sep 7th, 2009 at 08:43 PM ----------

Hi vgersh99!
Nothing changed, depending on position of "-v ..." group in command line I have two states: one, file is printed on display and then awk stays in input mode and repeats all input to display, and second - syntax error.

---------- Post updated at 10:57 AM ---------- Previous update was at 10:54 AM ----------

By the word, even when I use not a variable but a value (-v FNT="ID_Action_DD_MM_YY"), situation is the same.

I've found my mustake, I always used $ with a variable name in awk script. Without it everything works fine, thank you all!:b:

The case is closed!