The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to Extract time from log files and write to a excel santosham UNIX for Dummies Questions & Answers 4 06-09-2008 06:30 AM
Script to Extract time from log files and write to a excel santosham UNIX for Advanced & Expert Users 0 06-06-2008 10:27 AM
shell script to remove old files and write to a log file yabai Shell Programming and Scripting 3 05-21-2008 12:52 AM
how to read or write device files sriram.ec UNIX for Dummies Questions & Answers 1 01-03-2006 01:27 PM
How to read and write files one line at a time. s_chopra UNIX for Dummies Questions & Answers 2 04-18-2001 06:39 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-21-2003
Registered User
 

Join Date: Jul 2003
Location: Hambach - France
Posts: 29
Stumble this Post!
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...
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-21-2003
davidg's Avatar
Registered User
 

Join Date: Jul 2003
Location: Holland
Posts: 207
Stumble this Post!
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
Reply With Quote
  #3 (permalink)  
Old 07-21-2003
Registered User
 

Join Date: Jul 2003
Location: Hambach - France
Posts: 29
Stumble this Post!
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
Reply With Quote
  #4 (permalink)  
Old 07-21-2003
davidg's Avatar
Registered User
 

Join Date: Jul 2003
Location: Holland
Posts: 207
Stumble this Post!
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
Reply With Quote
  #5 (permalink)  
Old 07-21-2003
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,449
Stumble this Post!
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
Reply With Quote
  #6 (permalink)  
Old 07-25-2003
Registered User
 

Join Date: Jul 2003
Location: Hambach - France
Posts: 29
Stumble this Post!
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 !!!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0