Split the files using line as a argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split the files using line as a argument
# 1  
Old 05-08-2014
Split the files using line as a argument

Hi Team,
I have a input file like below

Code:
$ cat disklist.txt
disk1
disk2
disk3
disk4
disk5
disk6
disk7
disk8
disk9
diskA
diskB
diskC
diskD

I wanted to split this contents into multiple files and make it as a .sql files like below. One more thing split the contents based on the input arguments of the lines (ex: below one is based on 4 lines in each file). Pls advice how to do it via script. Thanks !

Code:
$ cat asm_disk_add_1.sql
set echo on timing on feedback on
spool alter_diskgroup.add_datadg_disks_1.log
alter diskgroup DATADG add disk
'disk1',
'disk2',
'disk3',
'disk4';
spool off
exit;

Code:
$ cat asm_disk_add_2.sql
set echo on timing on feedback on
spool alter_diskgroup.add_datadg_disks_1.log
alter diskgroup DATADG add disk
'disk5',
'disk6',
'disk7',
'disk8';
spool off
exit;

Code:
$ cat asm_disk_add_3.sql
set echo on timing on feedback on
spool alter_diskgroup.add_datadg_disks_1.log
alter diskgroup DATADG add disk
'disk9',
'diskA',
'diskB',
'diskC';
spool off
exit;

Code:
$ cat asm_disk_add_4.sql
set echo on timing on feedback on
spool alter_diskgroup.add_datadg_disks_1.log
alter diskgroup DATADG add disk
'diskD',
;
spool off
exit;

# 2  
Old 05-08-2014
Code:
awk -v q="'" 'BEGIN{FS="\n"; RS="";
  HEAD = "set echo on timing on feedback on \
  \nspool alter_diskgroup.add_datadg_disks_1.log \
  \nalter diskgroup DATADG add disk";
  TAIL = ";\nspool off \
  \nexit;"}
  {for(i = 1; i <= NF; i+=4)
    {print HEAD > "asm_disk_add_" ++n ".sql"
    print q $i q > "asm_disk_add_" n ".sql"
    for(j = i+1; j <= i+4; j++)
      {if($j != "")
        print ("," q $j q) > "asm_disk_add_" n ".sql"}
    print TAIL > "asm_disk_add_" n ".sql"}}' disklist.txt

---------- Post updated at 01:05 AM ---------- Previous update was at 01:03 AM ----------

Code:
awk -v q="'" 'BEGIN{FS="\n"; RS="";
  HEAD = "set echo on timing on feedback on \
  \nspool alter_diskgroup.add_datadg_disks_1.log \
  \nalter diskgroup DATADG add disk";
  TAIL = ";\nspool off \
  \nexit;"}
  {for(i = 1; i <= NF; i+=4)
    {print HEAD > "asm_disk_add_" ++n ".sql"
    printf q $i q > "asm_disk_add_" n ".sql"
    for(j = i+1; j <= i+4; j++)
      {if($j != "")
        printf (",\n" q $j q) > "asm_disk_add_" n ".sql"}
    print TAIL > "asm_disk_add_" n ".sql"}}' disklist.txt

This User Gave Thanks to SriniShoo For This Post:
# 3  
Old 05-08-2014
Thanks a lot for your reply ... But i got the output like below .. which means one disk is repeated in 2 .sql files. Thanks !!

Code:
$ more asm_disk_add_1.sql
set echo on timing on feedback on
spool alter_diskgroup.add_datadg_disks_1.log
alter diskgroup DATADG add disk
'disk1',
'disk2',
'disk3',
'disk4',
'disk5';  -------> 
spool off
exit;

$ more asm_disk_add_2.sql
set echo on timing on feedback on
spool alter_diskgroup.add_datadg_disks_1.log
alter diskgroup DATADG add disk
'disk5', ------->
'disk6',
'disk7',
'disk8',
'disk9';
spool off
exit;

# 4  
Old 05-08-2014
If you change:
Code:
    for(j = i+1; j <= i+4; j++)

in SriniShoo's awk script to:
Code:
    for(j = i+1; j <= i+3; j++)

it should do what you want on your system. It uses one construct on all of the print and printf statements that is not portable (and will produce syntax errors on some systems).

An alternative approach that should work on any standards-conforming version of awk is:
Code:
awk -v sq="'" '
BEGIN {	H =   "set echo on timing on feedback on\n"
	H = H "spool alter_diskgroup.add_datadg_disks_1.log\n"
	H = H "alter diskgroup DATADG add disk"
	T =   "spool off\n"
	T = T "exit;"
}
{	n = NR % 4
	if(n == 1) {
		of = "asm_disk_add_" (NR + 3) / 4 ".sql"
		print H > of
		printf("%s", sq $1 sq) > of
	} else	printf(",\n%s", sq $1 sq) > of
	if(n == 0) {
		printf(";\n%s\n", T) > of
		close(of)
	}
}
END {	if(n != 0) {
		printf(";\n%s\n", T) > of
		close(of)
	}
}' disklist.txt

On a system that accepts SriniShoo's script, these two scripts will produce the same output (with the change noted above) except that this script doesn't add trailing spaces to some lines like SriniShoo's script does.

If you want to try this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-08-2014
Thanks a lot ... for your help !!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Automate splitting of files , scp files as each split completes and combine files on target server

i use the split command to split a one terabyte backup file into 10 chunks of 100 GB each. The files are split one after the other. While the files is being split, I will like to scp the files one after the other as soon as the previous one completes, from server A to Server B. Then on server B ,... (2 Replies)
Discussion started by: malaika
2 Replies

2. UNIX for Beginners Questions & Answers

Split and Rename Split Files

Hello, I need to split a file by number of records and rename each split file with actual filename pre-pended with 3 digit split number. What I have tried is the below command with 2 digit numeric value split -l 3 -d abc.txt F (# Will Produce split Files as F00 F01 F02) How to produce... (19 Replies)
Discussion started by: techedipro
19 Replies

3. Shell Programming and Scripting

Split a file into multiple files based on line numbers and first column value

Hi All I have one query,say i have a requirement like the below code should be move to diffent files whose maximum lines can be of 10 lines.Say in the below example,it consist of 14 lines. This should be moved logically using the data in the fisrt coloumn to file1 and file 2.The data of first... (2 Replies)
Discussion started by: sarav.shan
2 Replies

4. Shell Programming and Scripting

command-line line 0: Missing yes/no argument

Hi Guys When I run the below command ssh -o 'PasswordAuthentication yes' -o 'PreferredAuthentications publickey' -i $HOME/.ssh/id_dsa Server_Name I found the below error ommand-line line 0: Missing yes/no argument Kindly help me to sort out Double post, continued... (0 Replies)
Discussion started by: Pratik4891
0 Replies

5. Shell Programming and Scripting

shell script for ftp files passed in command line argument

i want to write a shell script function that will ftp the files passed in the command line . i have written a shell script for ftp but how will it do for all files passed in command line argument , i am passing 4 files as argument ./ftp.sh file1 file2 file3 file4 code written by me... (5 Replies)
Discussion started by: rateeshkumar
5 Replies

6. Shell Programming and Scripting

Split line to multiple files Awk/Sed/Shell Script help

Hi, I need help to split lines from a file into multiple files. my input look like this: 13 23 45 45 6 7 33 44 55 66 7 13 34 5 6 7 87 45 7 8 8 9 13 44 55 66 77 8 44 66 88 99 6 I want to split every 3 lines from this file to be written to individual files. (3 Replies)
Discussion started by: saint2006
3 Replies

7. Shell Programming and Scripting

split single line into two line or three lines

Dear All, I want to split single line into two line or three lines wherever “|” separated values comes using Input line test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087, output shoule be test,DEMTEMPUT20100404010012,,,,,,,,0070086, test,DEMTEMPUT20100404010012,,,,,,,,0070087, (14 Replies)
Discussion started by: arvindng
14 Replies

8. Programming

Command Line Argument

Hi, I have a very simple C program which will run in UNIX. When i am passing * as the command line argument, i am gettig the below output. Program: #include <stdio.h> #include "mylibrary.h" int **environ; int main(int argc,char *argv) { int i; printf("\nHello... (2 Replies)
Discussion started by: dsudipta
2 Replies

9. Shell Programming and Scripting

Split a line on positions before reading complete line

Hi, I want to split before reading the complete line as the line is very big and its throwing out of memory. can you suggest. when i say #cat $inputFile | while read eachLine and use the eachLine to split its throwing out of memory as the line size is more than 10000000 characters. Can you... (1 Reply)
Discussion started by: vijaykrc
1 Replies

10. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies
Login or Register to Ask a Question