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!

NAWK Small Script 1

Status
Not open for further replies.

beaster

Technical User
Aug 20, 2001
225
US
My on going project with my script has me stumped again. It took a
bit longer this time though.

I have a file called pre_master_build, and I have numerous files called
file1, file2, file3, ect. The number of files called "file#" may different
each time.

I just need the master file pre_master_build cat into a new file called pre_build#
for however many files called file there were.

So if I have:
file1
file2
file3
file4

I would end up with:
pre_build1
pre_build2
pre_build3
pre_build4

and they would all have duplicate contents in them taken from the pre_master_build file.


As always Thanks!
Beaster
 
here's a ksh script to start you with - watch out for line wraps:

#------------------------------------------------
#!/bin/ksh

for i in *file*
do
outFile=$(echo ${i} | sed -e 's/.*\([0-9][0-9]*\)$/pre_build\1/g');
# echo "file->[${i}] outFile->[${outFile}]"
cat pre_master_build > ${outFile}
done;
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Either way:

> what is *file* ??????????????????

getting a listing of all the files with 'file' anywhere in their name.

> what is this s__t: ${i}

> /*
> ** replace the first _ with'h'
> ** the secont with 'i'
> */

You could've written a sed expression for that, couldn't you?

> don't you know when [{}] are needed ??? && NOT ???

I sure do. I made it a good coding practice to ALWAYS surround any references to variable names with [{}] - either needed OR not. It makes the code easier to read and DOES not confuse anything [or anybody..... well....... I shouldn't have said that] and makes the code easier to read when you're concatinating vars.

> and in sed:
> don't you know that ' prevent expansion of $
> i did not check this statement, but this is sure KO:
> outFile=$(echo ${i} | sed -e > 's/.*\([0-9][0-9]*\)$/pre_build\1/g');

> will NEVER expand $pre_build to its value !!!! because of '

Well..... if you read the sed statement a little MORE closely [not that you need to, but....], the &quot;$&quot; specifies the EndOfLine and was NOT meant to be to variable reference.

For the rest, I'd let others judge the delivery methods.
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
I ran it just as above and got this response:

bham_mta@wepmas1> sh -v tek-tips.sh
#------------------------------------------------
#!/bin/ksh

for i in *file*
do
outFile=$(etek-tips.sh: syntax error at line 6: `outFile=$' unexpected
bham_mta@wepmas1>
 
1. remove the first '#---------' line - make sure that you have #!/bin/ksh as the FIRST line of your script - this specifies the shell interpreter to use [/bin/ksh in this case]

2. call script as: tek-tips.sh
3. make sure you're in the same directory where you have your 'files' to be processed. The script assumes the same 'pwd' vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
I made a change and enclosed the outfile in ` `
I now get:

bham_mta@wepmas1> sh -v tek-tips.sh
#!/bin/ksh

for i in *file*
do
outFile= `(echo ${i} | sed -e 's/.*\([0-9][0-9]*\)$/pre_build\1/g');`
# echo &quot;file->[${i}] outFile->[${outFile}]&quot;
cat pre_master_build > ${outFile}
done;tek-tips.sh: pre_build1: not found
 
This is my original solution - NO wrapped lines.
Pls run it as orinally posted:

#!/bin/ksh

for i in *file*
do
outFile=$(echo ${i} | sed -e 's/.*\([0-9][0-9]*\)$/pre_build\1/g');
# echo &quot;file->[${i}] outFile->[${outFile}]&quot;
cat pre_master_build > ${outFile}
done; vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
i=0
t=&quot;pre_build&quot;
tO=&quot;pre_master_build&quot;

if [ -e &quot;$PWD/tO&quot; ] ;
then
for all in $PWD/*file*
do
i=`expr i + 1`
t=&quot;$t$i&quot;
cat $tO > $t && echo &quot;$?, $_&quot;
done
else
echo &quot;Unable to stat $tO in $PWD&quot;
exit 1 > /dev/null
fi

A bash version of vlads script to
see if this works any better for you.
 
Oops, before &quot;done&quot; reinitialize the
file name &quot;t&quot; back to the original.
code..
t=&quot;pre_build&quot;
done
 
Thanks vlad, it worked when I copied it exactly as your post.

Regards,
Beaster
 
I am running just as Vlad posted from September, and now that I am going through everything line by line, I found that is only creates 0-9 files.

I really need it to create as many files as it needs. Maybe 10, maybe 100 like my other post.

Can one of you look at it below and advise me what I should add to it?

#!/bin/ksh

for i in *file*
do
outFile=$(echo ${i} | sed -e 's/.*\([0-9][0-9]*\)$/pre_build\1/g');
# echo &quot;file->[${i}] outFile->[${outFile}]&quot;
cat pre_master_build > ${outFile}
done;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top