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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to get the file name and put this in the file itself

Status
Not open for further replies.

smehta

Programmer
Mar 4, 2003
5
US
I need to do the following. I have the following file called
emp-name-irving-kentucky.grammar

The original contents are:
([
(elbert freeman){<vid bkaa9v2>}
(marque worrell){<vid b3ygmys>}
(ronney riddell){<vid b3zbgjq>}
]
?please~0.001)

I need the file to be transformed to the followinG;
((irving kentucky)[
(elbert freeman){<vid bkaa9v2>}
(marque worrell){<vid b3ygmys>}
(ronney riddell){<vid b3zbgjq>}
]
?please~0.001)

So basicallly I am taking the city and state out of filename
emp-name-irving-kentucky.grammar and putting it as
(irving kentucky) on the top of the same file....

How can this be done?

Also once I am done with file I need to do this for all file under one directory and concatenate all of these
city state files to produce one file.

 
For one file:

nawk -f townState.awk emp-name-irving-kentucky.grammar


#------------------------ townState.awk ----------------

BEGIN {
split(FILENAME, townState, &quot;-&quot;);
town=townState[3];
state=substr(townState[4], 1, length(townState[4]) - index(townState[4],&quot;.&quot;)+1
);

# printf(&quot;town->[%s] state->[%s]\n&quot;, town, state);
}

/^\(\[/ { printf(&quot;((%s %s)[\n&quot;, town, state); next }

{ print }

#--------------------------------------------------

For multiple file, write a shell script to loop through all the desired files. SOmething like that:

#!/bin/ksh

for iter in `ls *grammar`
do
nawk -f townState.awk ${iter} > ${iter}NEW
done;


vlad
 
Great script Vlad!

Here's a fix and an addition. The fix is for getting the state. It happened to work for Kentucky but not for Ohio. The second is a way of applying this to multiple files in pure awk and saving to new files directly. This could go straight to stdout by taking out the &quot; > outfile&quot; and, optionally, removing the setOutput call and function.

/^\(\[/ {
setOutput()
setTownState()
printf(&quot;((%s %s)[\n&quot;, town, state) > outfile
next
}
{ print > outfile }
function setOutput(){
if( NR > 1 ) close(output)
outfile=substr(FILENAME,1,index(FILENAME,&quot;.&quot;)) &quot;new&quot;
}
function setTownState(){
split(FILENAME, townState, &quot;-&quot;);
town=townState[3];
state=substr(townState[4], 1, index(townState[4],&quot;.&quot;)-1);
}
Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
ND,

thanks for the fix. Here are 2 fixes to your fixes ;)
I think they are appropriate:

1. Append output to a file
2. setOuput ONLY if at the first record if a current file
3. Derive town/state ONLY if at the first record in a current file.

NOTE: I guess the whole thing can be rewritten with the &quot;first-record-in-the-current-file&quot; logic as that's the boundary condition for all other changes.

ND, thanks again for the fixes. BTW, is the &quot;sed&quot; implementation version coming up? ;) [just kiddin']

vlad


/^\(\[/ {
setOutput()
setTownState()
printf(&quot;((%s %s)[\n&quot;, town, state) >> outfile
next
}
{ print >> outfile }

function setOutput(){
if( FNR == 1 ) {
close(outfile)
outfile=substr(FILENAME,1,index(FILENAME,&quot;.&quot;)) &quot;new&quot;
}
}
function setTownState(){
if (FNR == 1)
split(FILENAME, townState, &quot;-&quot;);

town=townState[3];
state=substr(townState[4], 1, index(townState[4],&quot;.&quot;)-1);
}
 
That's great! I always forget about NFR.

One more fix: functions setOutput and setTownState should always be called at line 1 of each file in case /^\(\[/ doesn't occur on line 1 of each. To get:

FNR == 1 {setOutput(); setTownState()}
/^\(\[/ {
setOutput()
setTownState()
printf(&quot;((%s %s)[\n&quot;, town, state) >> outfile
next
}
{ print >> outfile }

function setOutput(){
close(outfile)
outfile=substr(FILENAME,1,index(FILENAME,&quot;.&quot;)) &quot;new&quot;
}
function setTownState(){
split(FILENAME, townState, &quot;-&quot;);
town=townState[3];
state=substr(townState[4], 1, index(townState[4],&quot;.&quot;)-1);
}

p.s. sed would be easy, but needs help from the shell to get the input filename(s). Maybe when I have time I'll see if I'm right about the 'easy', if someone doesn't beat me to it. Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
These are good but they replace all the occurences of the character ([ with file name and I just need this to be done only once at the begining of the file. Thanks alot anyways. Could anyone suggest the modification
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top