1

I created a bash script that parses ASCII files into a comma delimited output. It's worked great. Now, a new file layout for these files is being gradually introduced.

My script has now two parsing functions (one per layout) that I want to call depending on a specific marker that is present in the ASCII file header. The script is structured thusly:

#!/bin/bash

function parseNewfile() {...parse stuff...return stuff...}
function parseOldfile() {...parse stuff...return stuff...}

#loop thru ASCII files array
i=0
while [ $i -lt $len ]; do
    #check if file contains marker for new layout
        grep CSVHeaderBox output_$i.ASC
        #calls parsing function based on exit code
        if [ $? -eq 0 ]
        then
        CXD=`parseNewfile`
        else
        CXD=`parseOldfile`
        fi

    echo ${array[$i]}| awk -v cxd=`echo $CXD` ....
        let i++
done>>${outdir}/outfile.csv
...

The script does not err out. It always calls the original function "parseOldfile" and ignores the new one. Even when I specifically feed my script with several files with the new layout.

What I am trying to do seem very trivial. What am I missing here?

EDIT: Samples of old and new file layouts.

1) OLD File Layout

F779250B
=====BOX INFORMATION=====
Model         = R15-100
Man Date      = 07/17/2002
BIST Version  = 3.77
SW Version    = 0x122D
SW Name       = v1b1645
HW Version    = 1.1
Receiver ID   = 00089787556

=====DISK INFORMATION=====
....

2) NEW File Layout

F779250B
=====BOX INFORMATION=====
Model         = HR22-100
Man Date      = 07/17/2008
BIST Version  = 7.55
SW Version    = 0x066D
SW Name       = v18m1fgu
HW Version    = 2.3
Receiver ID   = 028910170936

CSVHeaderBox:Platform,ManufactureDate,BISTVersion,SWVersion,SWName,HWRevision,RID
CSVValuesBox:HR22-100,20080717,7.55,0x66D,v18m1fgu,2.3,028910170936

=====DISK INFORMATION=====
....
5
  • 1
    You need to provide a reproducible example. The grep + if works for a simple example. Commented Jun 25, 2013 at 17:16
  • grep doesn't seem to find the string CSVHeaderBox in the file output_$i.ASC. Show sample content of such a file. Commented Jun 25, 2013 at 17:16
  • Do you see CSVHeaderBox in the output? Commented Jun 25, 2013 at 17:17
  • 2
    You'll want to add -q and -s to grep's options. Commented Jun 25, 2013 at 17:18
  • @AnsgarWiechers: I'll provide one in my original post. Stand by. Commented Jun 25, 2013 at 18:26

3 Answers 3

1

This may not solve your problem, but a potential performance boost: instead of

    grep CSVHeaderBox output_$i.ASC
    #calls parsing function based on exit code
    if [ $? -eq 0 ]

use

    if grep -q CSVHeaderBox output_$i.ASC

qrep -q will exit successfully on the first match, so it doesn't have to scan the whole file. Plus you don't have to bother with the $? var.

Don't do this:

awk -v cxd=`echo $CXD`

Do this:

awk -v cxd="$CXD"
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure if this solves the OP's requirement.
What's the need for awk if your function knows how to parse the file?

#/bin/bash

function f1() {
    echo "f1() says $@"
}

function f2() {
    echo "f2() says $@"
}

FUN="f1"
${FUN} "foo"
FUN="f2"
${FUN} "bar"

1 Comment

Awk is here to clean up the output of the function before writing it to my CSV file.
0

I am bit embarrassed to write this but I solved my "problem".

After gedit (I am on Ubuntu) err-ed out several dozen times about "Trailing spaces", I copied and pasted my code into a new file and re-run my script.

It worked.

I have no explanation why.

Thanks to everyone for taking the time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.