Hello, I have to deal with several files that will be named something like this:
E00001.TXT, E00002.TXT etc.
Each file will have a alpha character on the first position of the first line which I want to place in a variable, then delete the entire line leaving the remainder of text. This new file I want to be named the original filename prefixed by the character I previously extracted so the new file might be RE00001.TXT. Usually, out of habit I call tcl scripts to do file manipulation but I wanted to know if there was a fairly starighforward way to do this all in my Unix script (ksh). I don't know much about sed or awk so if you give examples with that and don't mind providing a brief description of what does what that would be helpful. Could the grep command provide all the functionality I'm asking for?
TIA
D
dfb,
See if this works for you:
for mFile in E?????.TXT
do
mFirstChar=`head -1 $mFile | cut -c1`
mNewFile=$mFirstChar$mFile
echo "new file = "$mNewFile
mv $mFile $mNewFile
done
Shell, I like it, very simple, I don't think it addresses the issue of my new file containing everything except the first line though, does it? How can I get the newfile to exclude the first line from the source file, is that the cut command again?
Thx again
Here it is removing the first line:
for mFile in E?????.TXT
do
mFirstChar=`head -1 $mFile | cut -c1`
mNewFile=$mFirstChar$mFile
echo "new file = "$mNewFile
sed '1d' $mFile > $mNewFile
done
Shell, I'll try it, my thanks!!!
awk '
FNR==1 { ch=substr($0,1,1)} #get first character from first line
FNR>1 { # get 2nd line onwards
newfile=ch FILENAME
print > newfile #save 2nd line onwards to newfile.
}
' E*.TXT
You could use sed to do the job:
sed 1d E000* -i
In the above example, all the the files that begin with E000 will have the first line erased.
Hope this helps.