NAWK command help

gaugeta
gaugeta used Ask the Experts™
on
Hi experts...
I am a newbie to UNIX.I found an Nawk command and have no idea what it means.Can anyone explain this command bit by bit
nawk -F "|" '{i=1;while(i<=NF){gsub(/^[ \t]+|[ \t]+$/,"", $i); if (length($i)>0) printf("%s|",$i) ; if (i==NF){printf("\n")};  i++}}' $rej_list | sed -e 's/.$//' >! $rej_list_fmt
Please help...

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Hi experts can anyone help me on this...
Most Valuable Expert 2013
Top Expert 2013
Commented:
Hi,

this command works line by line on a file containing pipe-delimited fields (-F"|") whose name is stored in the variable $rej_list.

A "while" loop using the variable "i" is constructed over all the fields $i in a line. $i contains $1, $2.. up to the last field where i=NF, the nawk-internal variable containing the Number of Fields. $1, $2 ... represent the fields themselves.

The loop itself is built with the instructions "i=1" (set starting value), "while(i<=NF)"  (loop while i is less or equal to the number of fields in the line) and "i++" (increment the variable i by one)

For each field $i global substitutions ("gsub") are made. replacing one or several TAB characters (\t) at the start of a field (^) as well as one or several TAB characters (\t) at the end of a field ($) with the empty string (""), thus effectively removing them.

If the length ("length") of the resulting string is greater than zero (i.e. the field did not contain just TABs) this string is printed out, the original delimiter ("|") which has got lost during nawk processing (the delimiter is not considered being part of the data by nawk) is reinserted. Additionally, when the last field of each line has been printed out (i==NF), a newline character (\n) is added.

"printf" which is used to display the data is quite similar to printf in C. See "man printf".

This output is piped through sed which removes the last character (the extra pipe resulting from printf("%s|") from the end of each line.

Finally, the whole output is redirected to a file whose name is stored in the variable $rej_list_fmt.

It seems that this command will need the C-shell (csh) or similar to run successfully, because e.g. ksh or bash don't recognize the ">!" redirection format (meaning: create file if it doesn't exist yet, this format is more or less obsolete, even in csh).


wmp

Author

Commented:
@woolmilkporc:This is run in tcsh shell.Is tcsh shell obsolete?
Most Valuable Expert 2013
Top Expert 2013

Commented:
No, tcsh is not at all obsolete.

csh is rather old and a bit inconvenient, but is not obsolete either.

What I meant to say is that the redirection format ">!" is no longer mandatory for non-existing output files in neither shell, but can still be used in csh and tcsh nonetheless. ksh and bash don't recognize this format, however.

Thx for the points!

wmp

Author

Commented:
Thx for the reply.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial