Script with read/write Files

Hello,

I am a Newbie in ksh Unix Script.

So I must write a ksh/sh script who read character at a position in a File. So also it must read all the lines who belongs at these characters , then write these lines in a another File.

Can you help me , or give little councils to advance with my script.

Thanks a lot...

Hi Steiner,

I am afraid you won't make it in a ksh-shell script.
You need to use Perl for this, it's installed on almost each system by default. It might depend on the OS-type which version you have.

If I understand your question right a solution in Perl would be :

#!/usr/local/bin/perl

$open_file="/path/to/my/file";
$output_file="/path/to/my/output";

open(FH, "< $open_file);
@contents=<FH>;
close(FH);

open(FD, "> $output_file");

foreach $line(@contents) {

if ( $line =~ /thisregex/ ) {
print FD $line;
}

}

close(FD);

Please let me know if you need any additional info. Try to be some little more specific if it didn't solve your problem completely.

@yourservice
David

Hello David,

Thanks for your response , it will be very good idea but i need that the script must be in ksh.
So I have done 10 years VB script , so I have program this script in 30 mn in VB, and I need a very long time in unix script.

So our system is HP-Unix 11i.

I need ksh script , because others users must execute this script, and this script is a little part of a big script who must take files in a lot of directories. I have done this part of the script.

Perhaps do you have Internet Site where I can found those examples of scripts.

So I can program in ksh to read lines of a file , but i don't know how to take a part of the file = characters I have found and write it in a other file.

So thanks you very much for your help... Bye

Hi Steiner,

Sorry to not have been of any help.

I must let you know that I think it is not possible at all what you want. I am afraid that you do need to do it in another language than shell-scripting.
You do should be aware that you can call a perl-script (also users) from an excisting script. f.e. :

#!/bin/ksh
#

perlroutine="set"

if [ -z "$perlroutine" ]
then
exec /path/to/my/perlscript.pl param1 param2 param3
fi

echo "Alright perl routine is completed"
# ---------------------------------------------

#!/usr/local/bin/perl
#
# Perlroutine script henced by ksh-shell script
#

$param1=$1;
$param2=$2;
$param3=$3;

I realy hope someone else will give you another solution in ksh (I'dd be very interested !!), but I am afraid no-one can.

@yourservice
David

I am not real sure of what steiner wants to do. But if I understand that perl script (and I may not), in ksh it would roughly be:

#! /usr/bin/ksh

exec < /path/to/inputfile
exec > /path/to/outfile
wanted="x"
IFS=""
while read line ; do 
      if [[ line = *${wanted}* ]] ; then
           echo "$line"
      fi
done
exit 0

Hello,

Thanks for your help with the few questions I ask to you.

So i have finished the script in ksh for read and write datas from one files and split there in perhaps few anothers files.

So here is the script :

#---------------------------------------------------
# Look for I02 - files in /opt/rfts/basket/VEGA_I02
#---------------------------------------------------
find $INT_DIR -type f | egrep "I02.*.*.x.*" | while read file
do
filename=$( basename $file)
C_NAME=$(echo $filename | cut -c,9-14)
exec <$INT_DIR/${filename}

while read line ; do
if [ $(echo ${#line}) -gt 157 ] ; then
exec 2>$INT_DATA_KCC_DMS$(echo $line | cut -c,158-160)/$TYPE_FILE$(echo $line | cut -c,158-160).$C_NAME
exec 3>$INT_DATA_KCC_DMS$(echo $line | cut -c,158-160)/$TYPE_FILE$(echo $line | cut -c,158-160).$C_NAME.v.00
1
fi
echo $line>&2
done
done

exit 0

So it's work very good. Thanks for all your help !!!