How to extract work in line string.?

Hello all,

Soon I will be receiving a new file. I've asked the source system to put "TRAILER.1+0000007+1" for the trailer to indicate full receipt of file. I need to know how to separate TRAILER so I can use it in a if statement.

I used the tail command but not sure how to incorporate awk or sed. I just want to word TRAILER and I also want to separate 0000007 which is the file line count.

/xtimport/fi> tail -1 /xtimport/fi/FBOC810G* | awk '{print $1;}'
"TRAILER.1+0000007+1"

Try this...

tail -1 file | awk -F. '{sub("\"","");print $1}' | while read var; do
    if [ $var = "TRAILER" ]; do
       <do stuff here...>
    fi
done

This works perfectly. Can you help me understand this piece a little more for learning sake? Thanks much for all help.

awk -F. '{sub("\"","");print $1}'
tail -1 filename | awk -F[\"\.] '{print $1}'

This code didn't work. Can you help me understand from -F [\"\.]

tail -1 filename | awk -F[\"\.] '{print $1}'
  • It mean input field separator either double quotes (") or dot (.)

' - Print field 1

Could you please post error which you are facing with the earlier solution ?

Its not printing TRAILER. No error.

$ ./james.sh
tail -1 FBOC810G_140305.CSV | awk -F["\.] '{print }'

You missed one back slash and "$2"

tail -1 FBOC810G_140305.CSV | awk -F[\"\.] '{print $2}'

Here is an alternative way to do it just using awk (without tail ). Unless your input files are BIG, it is probably faster to let awk read the entire input file than it is to start up another process to run tail .

#!/bin/ksh
if [ $# -ne 1 ] || [ ! -r "$1" ] || [ ! -f "$1" ]
then    printf 'Usage: %s readable_regular_file\n' "${0##*/}" >&2
        exit 2
fi
awk -F '[.+"]' '
FNR == 1 { f = FILENAME }
{          t = $2;n = $4 }
END {      if(t == "TRAILER") {
                print f, "transfer complete,", n + 0, "lines."
                exit 0
           }
           print f, "transfer incomplete."
           exit 1
}' "$1"

With an input file named complete.txt containing:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
"TRAILER.1+0000007+1"

and a file named incomplete.txt containing the same 1st six lines, but missing the trailer line, running the above script (assuming you name it checker ) as:

./checker complete.txt

produces the output:

complete.txt transfer complete, 7 lines

with exit code 0, and running the command:

./checker incomplete.txt

produces the output:

incomplete.txt transfer incomplete.

with exit code 1.

Pravin27,
I changed the print to $2 and it worked. Not sure why "TRAILER.1+0000007+1" it would be position $2?

tail -1 FBOC810G_140305.CSV | awk -F[\"\.] '{print $2}'
TRAILER

Sorry .. I have corrected my earlier post . I was taking input as

echo "TRAILER.1+0000007+1" | awk -F[\"\.] '{print $1}'

instead

echo '"TRAILER.1+0000007+1"' | awk -F[\"\.] '{print $2}'

Thanks,
Pravin

With double quote " and period . as field separators, and the input above: the first field is the empty string before the first double quote, the second field is TRAILER , the third field is 1+0000007+1 and the fourth field is the empty string after the the trailing double quote.