PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > How to use grep function for searching LIKE result
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

How to use grep function for searching LIKE result

Réponse
 
LinkBack Outils de la discussion
Vieux 17/08/2006, 16h08   #1
bonnie.tangyn@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to use grep function for searching LIKE result

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

  Réponse avec citation
Vieux 18/08/2006, 01h05   #2
Barry Margolin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to use grep function for searching LIKE result

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 ***
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h57.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,08052 seconds with 10 queries