Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Another scripting help please, splitting files by range, maybe I don't need a script :( Post 303044575 by newbie_01 on Wednesday 26th of February 2020 09:13:44 PM
Old 02-26-2020
Another scripting help please, splitting files by range, maybe I don't need a script :(

Hi,


I requested the Windows SysAdmin to give me all available config files from a server.



Instead of sending it into several files, he combine all of them into one single file leaving it for me to split them up myself.


I was able to do so via a script below:



Code:
$ cat ./split_file.ksh
#!/bin/ksh
#

myPID=$$
grep -in "C:" file_to_split.txt > $myPID.tmp.00
cat $myPID.tmp.00

last_number=`tail -1 $myPID.tmp.00 | awk -F":" '{ print $1 }'`
switch=0
count=0

echo
echo "myPID = $myPID"
echo "last_number = $last_number"
echo

while read num
do
   if [[ $switch = 0 ]] ; then
      end=1
      switch=1
      continue
   else
      let count=$count+1
      start=$end
      end=`echo $num | awk -F":" '{ print $1-1 }'`
      echo "- Printing ${start} to ${end} ..."
      sed -n "${start},${end}p" file_to_split.txt > file_to_split.file.${count}.txt
      let end=$end+1

      if [[ $end = $last_number ]] ; then
         let count=$count+1
         start=$end
         echo "- Printing ${start} to the end ... "
         sed -n "${start},\$p" file_to_split.txt > file_to_split.file.${count}.txt
      fi
   fi
done < $myPID.tmp.00

rm $myPID.tmp*
echo
ls -l file_to_split.file.*txt
echo

wc -l file_to_split.txt
echo
wc -l file_to_split.file.*txt

exit 0

Sample output running the script is as below:


Code:
$ ./split_file.ksh
1:C:\Oracle\product\12.1.0\client_1_32Full\NETWORK\ADMIN\tnsnames.ora
6673:C:\Oracle\product\12.1.0\client_1_32Full\NETWORK\ADMIN\SAMPLE\tnsnames.oRA
13345:C:\Oracle\product\12.1.0\client_1_64Full\network\admin\tnsnames.ora
20017:C:\Oracle\product\12.1.0\client_1_64Full\network\admin\sample\tnsnames.oRA
26689:C:\Oracle\product\12.1.0\TNSNAMES\tnsnames.ora
33361:C:\Support\tnsnames.ora
39897:C:\Support\CHG0082341\tnsnames.ora

myPID = 26172
last_number = 39897

- Printing 1 to 6672 ...
- Printing 6673 to 13344 ...
- Printing 13345 to 20016 ...
- Printing 20017 to 26688 ...
- Printing 26689 to 33360 ...
- Printing 33361 to 39896 ...
- Printing 39897 to the end ...

-rw-r--r-- 1 oracle oinstall 363739 Feb 27 14:28 file_to_split.file.1.txt
-rw-r--r-- 1 oracle oinstall 363746 Feb 27 14:28 file_to_split.file.2.txt
-rw-r--r-- 1 oracle oinstall 363739 Feb 27 14:28 file_to_split.file.3.txt
-rw-r--r-- 1 oracle oinstall 363746 Feb 27 14:28 file_to_split.file.4.txt
-rw-r--r-- 1 oracle oinstall 363718 Feb 27 14:28 file_to_split.file.5.txt
-rw-r--r-- 1 oracle oinstall 352223 Feb 27 14:28 file_to_split.file.6.txt
-rw-r--r-- 1 oracle oinstall 363706 Feb 27 14:28 file_to_split.file.7.txt

46568 file_to_split.txt

   6672 file_to_split.file.1.txt
   6672 file_to_split.file.2.txt
   6672 file_to_split.file.3.txt
   6672 file_to_split.file.4.txt
   6672 file_to_split.file.5.txt
   6536 file_to_split.file.6.txt
   6672 file_to_split.file.7.txt
  46568 total

Is there an awk one-liner way of achieving the same result? Smilie
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

automated ftp script from unix -date range of files

Hi , I need some help to finish my ftp script and i need to find the last one weeks of fles updated in the sepecific directory and see those end with Z and ftp them to my backup server. Any help is appreciated. Thanks, Ravi :) (1 Reply)
Discussion started by: koduri0475
1 Replies

2. Programming

automated ftp script from unix -date range of files

Hi , I need some help to finish my ftp script and i need to find the last one weeks of fles updated in the sepecific directory and see those end with Z and ftp them to my backup server. Any help is appreciated. Thanks, Ravi :) (1 Reply)
Discussion started by: koduri0475
1 Replies

3. UNIX for Advanced & Expert Users

automated ftp script from unix -date range of files

Hi , I need some help to finish my ftp script and i need to find the last one weeks of fles updated in the sepecific directory and see those end with Z and ftp them to my backup server. Any help is appreciated. Thanks, Ravi :) (1 Reply)
Discussion started by: koduri0475
1 Replies

4. Shell Programming and Scripting

Script that can Copy a Range of files from Tape to Hard disk

Hi: I am a trying to write a script using a loop i guess. What I need to do is write a script that can allow my users to load a tape and copy a range of files from a tape to the hard disk. By range I mean, I would like the users to pick a range of numbers like files 3 - 8 and the script will... (0 Replies)
Discussion started by: msjazzie
0 Replies

5. Shell Programming and Scripting

Script to move files based on Pattern don't work

Hi people, i need you help on this if you can. I have a script that does the move command when it searches for a that doesn't match the pattern. This Pattern is on a list. When it run's and doesn´t found no files but it haves 2 more folders it moves the folders too. Ex:... (1 Reply)
Discussion started by: osramos
1 Replies

6. Shell Programming and Scripting

Need script to select multiple files from archive directory based on the date range

hi all, here is the description to my problem. input parameters: $date1 & $date2 based on the range i need to select the archived files from the archived directory and moved them in to working directory. can u please help me in writing the code to select the multiple files based on the... (3 Replies)
Discussion started by: bbc17484
3 Replies

7. Shell Programming and Scripting

Splitting the files via shell script

Hi all, We have 102 flat files created by Informatica from 102 tables. These 102 files contain pharmcy details. There are a total of 450 pharmcyids.The naming convention for the flat file is ODS_<TABLE NAME>_yyyymmdd_timestamp.dat. Each flat file may contain data for 450 pharmacies which is... (2 Replies)
Discussion started by: Maya_Pillai
2 Replies

8. UNIX for Advanced & Expert Users

Help with ksh script to list, then cp files from a user input date range

Hi, I'm quite new to ksh scripting, can someone help me with this. Requirements: I need to create a script that list the files from a user input date range. e. g. format of file: *c1*log.2012-12-22-14-00* *c1*log.2012-12-22-14-00* *c1*log.2012-12-22-14-00*... (1 Reply)
Discussion started by: chococrunch6
1 Replies

9. Shell Programming and Scripting

Splitting a file and creating new files using Perl script

Hi All, I am new to Scripting language. I want to split a file and create several subfiles using Perl script. Example : File format : Sourcename ID Date Nbr SU IMYFDJ 9/17/2012 5552159976555 SU BWZMIG 9/14/2012 1952257857887 AR PEHQDF 11/26/2012 ... (13 Replies)
Discussion started by: Deepak9870
13 Replies

10. Shell Programming and Scripting

Script for splitting file of records into multiple files

Hello I have a file of following format HDR 1234 abc qwerty abc def ghi jkl HDR 4567 xyz qwerty abc def ghi jkl HDR 890 mno qwerty abc def ghi jkl HDR 1234 abc qwerty abc def ghi jkl HDR 1234 abc qwerty abc def ghi jkl -Need to split this into multiple files based on tag... (8 Replies)
Discussion started by: wincrazy
8 Replies
jrunscript(1)						      General Commands Manual						     jrunscript(1)

NAME
jrunscript - command line script shell SYNOPSIS
jrunscript [ options ] [ arguments... ] PARAMETERS
options Options, if used, should follow immediately after the command name. arguments Arguments, if used, should follow immediately after options or command name. DESCRIPTION
jrunscript is a command line script shell. jrunscript supports both an interactive (read-eval-print) mode and a batch (-f option) mode of script execution. This is a scripting language independent shell. By default, JavaScript is the language used, but the -l option can be used to specify a different language. Through Java to scripting language communication, jrunscript supports "exploratory programming" style. NOTE: This tool is experimental and may not be available in future versions of the JDK. OPTIONS
-classpath path Specify where to find the user's .class files that are accessed by the script. -cp path This is a synonym for -classpath path -Dname=value Set a Java system property. -Jflag Pass flag directly to the Java virtual machine on which jrunscript is run. -l language Use the specified scripting language. By default, JavaScript is used. Note that to use other scripting languages, you also need to spec- ify the corresponding script engine's jar file using -cp or -classpath option. -e script Evaluate the given script. This option can be used to run "one liner" scripts specified completely on the command line. -encoding encoding Specify the character encoding used while reading script files. -f script-file Evaluate the given script file (batch mode). -f - Read and evaluate a script from standard input (interactive mode). -help Output help message and exit. -? Output help message and exit. -q List all script engines available and exit. ARGUMENTS
If [arguments...] are present and if no -e or -f option is used, then the first argument is the script file and the rest of the arguments, if any, are passed as script arguments. If [arguments..] and -e or -f option are used, then all [arguments..] are passed as script argu- ments. If [arguments..], -e and -f are missing, interactive mode is used. Script arguments are available to a script in an engine variable named "arguments" of type String array. EXAMPLES
Executing inline scripts jrunscript -e "print('hello world')" jrunscript -e "cat('http://java.sun.com')" Use specified language and evaluate given script file jrunscript -l js -f test.js Interactive mode jrunscript js>print('hello world'); hello world js>34 + 55 89 js> thread(function() { print('hello world'); } hello world js> Run script file with script arguments jrunscript test.js arg1 arg2 arg3 test.js is script file to execute and arg1, arg2 and arg3 are passed to script as script arguments. Script can access these using "argu- ments" array. SEE ALSO
If JavaScript is used, then before evaluating any user defined script, jrunscript initializes certain built-in functions and objects. These JavaScript built-ins are documented in jsdocs. 06 Aug 2006 jrunscript(1)
All times are GMT -4. The time now is 01:02 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy