Script with read/write Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script with read/write Files
# 1  
Old 07-21-2003
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...
# 2  
Old 07-21-2003
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
# 3  
Old 07-21-2003
Response to your help

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
# 4  
Old 07-21-2003
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
# 5  
Old 07-21-2003
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:
Code:
#! /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

# 6  
Old 07-25-2003
Here the script in ksh for read/write file

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 !!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read and write operations on files.

Dears. kindly guide !!! I have data, which is delimited by | . it should contain 26 columns, but one column data contain | makes few row to 27 columns. I want to find rows have 27 columns and then concatenate the specific columns to single column to make it 26 columns. Kindly help, Can... (3 Replies)
Discussion started by: sadique.manzar
3 Replies

2. Shell Programming and Scripting

How to read and write last modified timestamp to files?

Need help reading file last modified date in format: Filename (relative path);YYYYMMDDHHMMSS And then write it back. My idea is to backup it to a text file to restore later. Checked this command but does not work: Getting the Last Modification Timestamp of a File with Stat $ stat -f... (5 Replies)
Discussion started by: Tribe
5 Replies

3. Shell Programming and Scripting

Need a UNIX/perl script to read and write the data

Hi, I have on Designdocument in that information is stored with in tabular format.I need Perl/unix script to read and write the data using perl script? Regards, Ravi (4 Replies)
Discussion started by: toravi.pentaho
4 Replies

4. Shell Programming and Scripting

Need a perl script to read and write the data

Hi, I have on Designdocument in that information is stored with in tabular format.I need Perlscript to read and write the datausing perl script? Regards, Ravi (0 Replies)
Discussion started by: toravi.pentaho
0 Replies

5. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

6. Programming

read/write files

Hi all, I have a problem with some read/write functions. I have a .bin file which contains a lot of structures as follows: struct alumno { char id; char apellido1; char apellido2; char nombre; float nota1p; float nota2p; float notamedia; char photofilename; }; What I have... (3 Replies)
Discussion started by: Attenea
3 Replies

7. Shell Programming and Scripting

Find all files with group read OR group write OR user write permission

I need to find all the files that have group Read or Write permission or files that have user write permission. This is what I have so far: find . -exec ls -l {} \; | awk '/-...rw..w./ {print $1 " " $3 " " $4 " " $9}' It shows me all files where group read = true, group write = true... (5 Replies)
Discussion started by: shunter63
5 Replies

8. Shell Programming and Scripting

SED command for read and write to different files

Hi I need some help on SED command I am writing a shell script which does the following: 1. Read one line at a time from a file abc.txt which has millions of lines 2. Prefix each line read with some text " 3. Post fix each line read with a quote " 4. Write the new modified... (11 Replies)
Discussion started by: gaurav_1711
11 Replies

9. UNIX for Dummies Questions & Answers

how to read or write device files

hi everybody, i am working in device drivers.As a beginner to this field ,i dont know how to read or write device files. Will copy_to_user and copy_from_user help me? I have created a device file using mknod command .Can anybody help me in this regard :confused thanks in advance sriram (1 Reply)
Discussion started by: sriram.ec
1 Replies

10. UNIX for Dummies Questions & Answers

How to read and write files one line at a time.

Hi! All! I am wirting a shell script in which i want to read one line at a time from the file and write it simultaneouly to other file one line at a time. Please let me know about some shell utility which can help me out. Thanx. If further clarifications are needed then please let me know... (2 Replies)
Discussion started by: s_chopra
2 Replies
Login or Register to Ask a Question