|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I am required to map file code, which is in another file, by using
filename. But, my generated txt filenames are in the following format: student_name20060811.txt, school_address20060711.txt, home_address20050901.txt... I tried to use grep function and then append the file code in the txt file. My problem is the grep function has to match student_name20060811.txt and then get the ABC001 file code. It can't get student_name as key only. Could you give me some advices on my code. I got a file code reference table and its format is as follow; student_name ABC001 school_address CDE002 home_address EFG003 .... My shell script code: #bin/sh .... filecode_connect = $filelocation/referencefilename.cfg .... 'bcp views/stored procedures data and output as .txt files .... for f in *.txt do mv -i "$f" "$f.bak" { mappedcode = `grep $f $filecode_connect | cut -f2 -d" "` printf "$mappedcode" "$(date +%y%m%d)" cat "$f.bak" printf "%s\n" $(( $(wc -l < "$f.bak") + 2 )) } >"$f" done My desired output format for student_name file for example: ABC001060811 bcp output data ^^^^^^504 (<- number of rows count, ^ is space) Cheers Bon |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
In article <1155827289.320771.99230@m79g2000cwm.googlegroups. com>,
bonnie.tangyn@gmail.com wrote: > I am required to map file code, which is in another file, by using > filename. But, my generated txt filenames are in the following format: > student_name20060811.txt, school_address20060711.txt, > home_address20050901.txt... I tried to use grep function and then > append the file code in the txt file. > > My problem is the grep function has to match student_name20060811.txt > and then get the ABC001 file code. It can't get student_name as key > only. Could you give me some advices on my code. > > I got a file code reference table and its format is as follow; > student_name ABC001 > school_address CDE002 > home_address EFG003 > ... > > My shell script code: You've got some basic problems understanding shell syntax. > > #bin/sh That should be #!/bin/sh > ... > filecode_connect = $filelocation/referencefilename.cfg You can't have spaces around the = in an assignment. > ... > 'bcp views/stored procedures data and output as .txt files > ... > for f in *.txt > do > mv -i "$f" "$f.bak" > { > mappedcode = `grep $f $filecode_connect | cut -f2 -d" "` # Remove the yyyymmdd.txt suffix from the filename key=${f/????????.txt/} mappedcode=`grep $key $filecode_connect | cut -f2 -d" "` > printf "$mappedcode" "$(date +%y%m%d)" printf "%s%s\n" $mappedcode `date +%y%m%d` > cat "$f.bak" > printf "%s\n" $(( $(wc -l < "$f.bak") + 2 )) To get the desired spacing, change the printf format to "%9d\n". > } >"$f" > done > > My desired output format for student_name file for example: > ABC001060811 > bcp output data > ^^^^^^504 (<- number of rows count, ^ is space) > > Cheers > Bon -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** *** PLEASE don't copy me on replies, I'll read them in the group *** |
|
![]() |
| Outils de la discussion | |
|
|