Visit Our UNIX and Linux User Community


Seperating Files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Seperating Files
# 1  
Old 01-14-2002
Seperating Files

I wonder if there is any method that can effectively cut a file into 2 or more files... with the title on top...

like this Original File :

TITLE 1
001 ABC
002 DEF
003 GHI
004 JKL


into 4 file :
File 1:

TITLE 1
001 ABC


File 2:

TITLE 1
002 DEF



ETC...
# 2  
Old 01-14-2002
Code:

#! /bin/ksh

FILE_NAME="list"

HEADER=`head -1 $FILE_NAME`
TOTAL_LINES=`expr \`cat $FILE_NAME | wc -l\` - 1`

i=1
while [ $i -le $TOTAL_LINES ]
do
        i=`expr $i + 1`

        TEMP_FILE_NAME="file_`expr $i - 1`"
        TEMP_VALUE=`head -$i list | tail -1`

        echo "$HEADER\n$TEMP_VALUE" > "$TEMP_FILE_NAME"
done

Is this what you want?
shaik786
# 3  
Old 01-14-2002
Here is an awk solution that will generate your files while reading the original file only once:

Code:
#!/bin/sh
awk 'BEGIN {getline hdr}
{i++
 print hdr >  "file" i
 print $0  >> "file" i
 close("file" i)
}' file.in

The output files are named file1, file2, file3, etc, but they can be named whatever, including using info from the input line.
Jimbo
# 4  
Old 01-16-2002
Question

i'm a self-learning new UNIX user
i'm writing my 3rd UNIX script in my life...
but i found myself fall into syndex errors...
this script is use for seperate file from a ftp server...
i need to cut it small into 1000 lines each
with keeping the first and last line of the original file in each new file
All the Files in xyz12345.txt with cuts into xyz2345a.txt , xyz2345b.txt ...etc
the line number will be rearranged from 001,...999.

Code:
#! /bin/ksh
for filename in *
do
	nline=wc -l $filename
	if [$nline -gt 1000] 
	then
		headLine= `head -1 $filename`
		tailLine= `tail -1 $filename`
		endCode= `abcdefghijklmnopqrstuvwxyz`
		counter= 1
		countLine= 2
		# For initialization before the first running of the while loop
		newFileName=`echo $filename| cut -c1-3,5-8``echo $endCode | cut -c$counter``echo $filename| cut -c9-12`
		echo $headLine > $newFileName
		
		while [$countLine -le $nline]
		do
			newFileName=`echo $filename| cut -c1-3,5-8``echo $endCode | cut -c$counter``echo $filename| cut -c9-12`
		
			typeset -Z3 headVar=0 ## no typeset in SCO?!
			echo ${headVar} 
			headVar=`expr $headVar + 1` 
			echo $headVar`head -$countLine filename | tail -1 | cut -c4- ` >> newFileName
			newFileNoLine= wc -l newFileName
			if [$newFileNoLine -eq 999] 
			then
				echo $tailLine >> newFileName
				counter=`expr $counter +1`
				headVar= 1
			fi
		done
	fi
done

hope ano can point out my mistakes and my misunderstanding. BIG THANKS

Last edited by biglemon; 01-16-2002 at 04:32 AM..
# 5  
Old 01-16-2002
First of all the "split" command could do this for you. But here are a few of your errors...

nline=wc -l $filename
You need backticks or use $(nline=$wc -l $filename)

headLine= `head -1 $filename`
No space bewteen the equal sign and the backtick.

endCode= `abcdefghijklmnopqrstuvwxyz`
Use forward quotes unless you really have a program called abcdefghijklmnopqrstuvwxyz that you want to run. And again lose that space after the equal sign.

if [$nline -gt 1000]
That needs to be "if [ $nline -gt 1000 ]" You must have white space before and after [ and ] so that the shell sees them as a word.

echo $tailLine >> newFileName
newFileName is a variable. If you want the contents of a variable, you must use a dollar sign in front of it.
# 6  
Old 01-16-2002
It looks like you should be incrementing $countLine. And your

nline=`wc -l $filename`

(I added the backquotes) will return both the count and the filename. To get just the count, do:

nline=`cat $filename | wc -l`

You are cutting the first 3 characters of the original line. If this is the original sequence, when it exceeds 999 then you will need logic to cut more than just 3 characters.

If it were not for your requirement to insert a newly generated sequence at front of each line, each loop could grab next set of 999 lines at once with

tail +$startpoint $filename | head -999 >> $newFileName

As is, for each 1000 lines processed, you will be opening and reading one input file 1000 times, and opening and appending to an output file 1000 times. And creation of lots of processes, times 1000.

That's very heavy. An awk script could do what you want with ease and efficiency.
Jimbo
# 7  
Old 01-16-2002
O...thank you everyone for helping....
i have figure out the problem...
i think i can finish the rest...

btw... any good (new) UNIX tutorial Book that u can introduce to me?

Last edited by biglemon; 01-17-2002 at 02:44 AM..
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #535
Difficulty: Medium
In C, the Boolean type false is associated with zero while any non-zero value is associated with true.
True or False?

9 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. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

3. Shell Programming and Scripting

How to create zip/gz/tar files for if the files are older than particular days in UNIX or Linux?

I need a script file for backup (zip or tar or gz) of old log files in our unix server (causing the space problem). Could you please help me to create the zip or gz files for each log files in current directory and sub-directories also? I found one command which is to create gz file for the... (4 Replies)
Discussion started by: Mallikgm
4 Replies

4. Shell Programming and Scripting

need a shell script to extract the files from source file and check whether those files existonserve

Hi, I am new to shell scripting.Please help me on this.I am using solaris 10 OS and shell i am using is # echo $0 -sh My requirement is i have source file say makefile.I need to extract files with extensions (.c |.cxx |.h |.hxx |.sc) from the makefile.after doing so i need to check whether... (13 Replies)
Discussion started by: muraliinfy04
13 Replies

5. Shell Programming and Scripting

How to extract data from indexed files (ISAM files) maintained in an unix server.

Hi, Could someone please assist on a quick way of How to extract data from indexed files (ISAM files) maintained in an UNIX(AIX) server.The file data needs to be extracted in flat text file or CSV or excel format . Usually we have programs in microfocus COBOL to extract data, but would like... (2 Replies)
Discussion started by: devina
2 Replies

6. Shell Programming and Scripting

How to retrieve all the linked script files/ctl files/sql files?

Hi I am going to migrate our datawarehouse system from HP Tru 64 Unix to the Red Hat Linux. Inside the box, it is running around 40 cron jobs; inside each cron job, it is calling other shell script files, and the shell script files may again call other shell script files or ctl files(for... (1 Reply)
Discussion started by: franksubramania
1 Replies

7. UNIX for Dummies Questions & Answers

seperating records with numbers from a set of numbers

I have two files one (numbers file)contains the numbers(approximately 30000) and the other file(record file) contains the records(approximately 40000)which may or may not contain the numbers from that file. I want to seperate the records which has the field 1=(any of the number from numbers... (15 Replies)
Discussion started by: Shiv@jad
15 Replies

8. Shell Programming and Scripting

Seperating one word into columns

Hi, Our issue is that we want to separate a string of characters, like xyxyxyxxxxyyxyxy into a column, x y x y ... I'm sure there's an easy awk fix for this, but being inexperienced, I figured I would ask on here. Thanks a lot in advance! (4 Replies)
Discussion started by: NovaGhostii
4 Replies

9. Shell Programming and Scripting

seperating the words from a line??

The line is like this +abc+def+mgh+ddsdsd+sa i.e. words seperated by +. There is a plus in the beginning. i want to conver this line to abc, def, mgh, ddsdsd, sa please provide the logic in the form of a shell script Thanks in advance (3 Replies)
Discussion started by: skyineyes
3 Replies

Featured Tech Videos