Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with printing characters on the same line:

Status
Not open for further replies.

pmcmicha

Technical User
May 25, 2000
353
I have a ksh script setup to pass variables to an awk sub-routine, but I am having problems with the sub-routine trying to print on the same line.

Sub-routine:

OUTPUT() {
export THEYEAR=${YR1}
export THECOUNT=$1

echo $2|/usr/bin/nawk -F&quot;_&quot; '{for (i = ENVIRON[&quot;THECOUNT&quot;]; i <= NF; i++) {N = ENVIRON[&quot;THEYEAR&quot;] &quot;_&quot; $i; {print &quot;Now creating directory: &quot; ENVIRON[&quot;THEYEAR&quot;] &quot;_&quot; $i &quot;.....&quot;}; system(&quot;/usr/bin/mkdir -m 777 &quot; N); {print &quot;Done.&quot;}}}'
}

I would like to print &quot;Now creating the directory: alksfj.....&quot; and then have &quot;Done.&quot; appear after the ....., but I cannot get this to happen. I tried \c like you would normally in ksh, but this didn't work and I can't seem to find an escape character for this in the books I have about awk.


Thanks in advance.
 
printf is your friend!

Also you don't need to use the envirronment variables to pass info into awk. Use &quot;-v awkvar=${shvar}&quot; paradigm and use &quot;awkvar&quot; variable(s) inside awk.
vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks for the quick reply Vlad. Your a walking a encyclopedia of scripting.

I am looking into the &quot;-v awkvar=${shvar}&quot; paradigm and tried this:

OUTPUT() {
export THEYEAR=${YR1}
export THECOUNT=$1
MTIME
echo $2|/usr/bin/nawk -v YR1=${YR1} -v CNT=$1 -F&quot;_&quot; '{for (i = $1; i <= NF; i++) {N = $YR1 &quot;_&quot; $i; {printf &quot;Now creating directory: &quot; $YR1 &quot;_&quot; $i &quot;.....&quot;}; system(&quot;/usr/bin/mkdir -m 777 &quot; N); {printf &quot;Done.\n&quot;}}}'
}

But this didn't work correctly, am I doing something wrong? Also can I call another sub-routine from within this awk statement? Thanks.
 
something like:

OUTPUT() {
export THEYEAR=${YR1}
export THECOUNT=$1
# vlad - what is this MTIME ?
MTIME
echo &quot;${2}&quot; | /usr/bin/nawk -v YR1=&quot;${YR1}&quot; -v CNT=&quot;${1}&quot; -F '_' '{for (i = CNT; i <= NF; i++) {N = YR1 &quot;_&quot; $i; printf(&quot;Now creating directory: %s ......&quot;, N); system(&quot;/usr/bin/mkdir -m 777 &quot; N); {printf &quot;Done.\n&quot;}}'
} vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Vlad,

MTIME is another sub-routine I am calling which I would like to include in the nawk statement.

MTIME() {
print &quot;`/usr/bin/date \&quot;+%T\&quot;`\t\c&quot;
}
 
Okay...so I made some changes to this part of the script, but I need to perform a copy on the first directory being created from the original directory only.

OUTPUT() {
echo $2|/usr/bin/awk -v YR1=&quot;${YR1}&quot; -v CNT=&quot;${1}&quot; -v SDIR=&quot;${2}&quot; -F'_' '{for (i = CNT; i <= (NF - 1); i++) {(N = YR1 &quot;_&quot; $i); system(&quot;/usr/bin/printf \&quot;`/usr/bin/date \&quot;+T\&quot;`\t\&quot;&quot;); printf(&quot;Now creating directory: %s.....&quot;,N); system(&quot;/usr/bin/mkdir -m 777 &quot; N); printf(&quot;Done.\n&quot;);
if (i == CNT) {
printf &quot;Now starting Directory Copy.....&quot;

# I have tried the following:
/usr/bin/cp -rp SDIR N

system(&quot;/usr/bin/cp -rp $2/* N&quot;)
# These do not work and I am unsure of what to try next???

printf &quot;Done.\n&quot;
}
else {
printf &quot;Not Happening.\n&quot;
}
}}'
}
 
Vlad,

Finally figured what I want to do, I would appreciate some feedback on this code:

OUTPUT() {
export F
echo $2|/usr/bin/gawk -v CNT=&quot;${1}&quot; -v SDIR=&quot;${2}&quot; -v YR1=&quot;${YR1}&quot; -F&quot;_&quot; '{for (
i = CNT; i <= (NF - 1); i++)
if (i == CNT) {
{(N = YR1 &quot;_&quot; $i);
printf strftime(&quot;%T&quot;);
printf(&quot;\tNow creating directory: %s.....&quot;,N);
system(&quot;/usr/bin/mkdir -m 777 &quot; N);
printf(&quot;Done.\n&quot;);
}
} else {
{(O = YR1 &quot;_&quot; $i);
printf strftime(&quot;%T&quot;);
printf(&quot;\tNow linking directory: %s to %s.....&quot;,N,O);
system(&quot;/usr/bin/ln -s $F &quot; O);
printf(&quot;Done.\n&quot;);
}
}}'

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top