Splitting a file and sending results to another file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Splitting a file and sending results to another file
# 8  
Old 09-21-2010
several problems
1. pass variables into gawk/awk with -v
2. print $0 > somefilename leaves the file descrptor open, if the file has thousands of lines with unique values you have thousands of open file descriptors, which is a bad thing
3 >> acts to open/append - you don't need both >> and > unless you want to create a new file each time.

Code:
#!/bin/bash
set -x

gawk -v dir="/var/local/dsx/csv"  -F, ' {
  ofile=sprintf("%s/%s", dir, $2)
  print $0 >> ofile   # change here use print not printf 

  last1=$1
  last2=$2 
  }
 '  $1


Last edited by jim mcnamara; 09-21-2010 at 09:36 AM..
These 2 Users Gave Thanks to jim mcnamara For This Post:
# 9  
Old 09-21-2010
Thank you, thats exactly what I needed.

---------- Post updated at 07:30 AM ---------- Previous update was at 07:25 AM ----------

one thing though, the outpu looks like this:
4,5,64,5,64,5,64,5,64,5,64,5,64,5,64,5,64,5,64,5,64,5,64,5,6

How do I make it look like my original csv file..

So my csv file looks like this:
Code:
o,e,r
1,2,3
1,2,3
1,2,3
4,5,6
4,5,6
4,5,6
4,5,6
4,5,6
4,5,6

My resultant files will look like this:
Code:
o,e,r

Code:
1,2,3
1,2,3
1,2,3

Code:
4,5,6
4,5,6
4,5,6
4,5,6
4,5,6
4,5,6

# 10  
Old 09-21-2010
See the red text in my post above.
# 11  
Old 09-21-2010
Okay one last question ...

The files get generated to the correct directory, how do I append a file extension .. so I want to create the files in my csv directoy as csv files.

I tried a few things but I get syntax errors

---------- Post updated at 08:20 AM ---------- Previous update was at 08:00 AM ----------

I have managed to get that part of it now.
What I need to do is concatente the first file as a header to the other files
so first file:
Code:
o,e,r

second file:
Code:
1,2,3
1,2,3
1,2,3

Code:
cat file2 file2

o,e,r
1,2,3
1,2,3
1,2,3

code so far

Code:
#!/bin/bash
set -x

gawk -v dir="/var/local/dsx/csv"  -v csv="csv" -F, ' {
  ofile=sprintf("%s/%s", dir, $3 )
  ofile=sprintf("%s.%s", ofile, csv)
  print $0 >> ofile

  last1=$1
  last2=$2 
  }
 '  $1

# 12  
Old 09-21-2010
Code:
gawk -v dir="/var/local/dsx/csv"   -F, ' {
  ofile=sprintf("%s/%s.csv", dir, $3 )
  
  print $0 >> ofile

  last1=$1
  last2=$2 
  }
 '  $1

try that.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create a text file and a pdf file from Linux command results.

Hello. The task : Using multiple commands like : gdisk -l $SOME_DISK >> $SOME_FILEI generate some text file. For readiness I must insert page break. When the program is finished I want to convert the final text file to a pdf file. When finished, I got two files : One text file and One pdf... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Beginners Questions & Answers

Splitting the file based on two fields - Fixed length file

Hi , I am having a scenario where I need to split the file based on two field values. The file is a fixed length file. ex: AA0998703000000000000190510095350019500010005101980301 K 0998703000000000000190510095351019500020005101480 ... (4 Replies)
Discussion started by: saj
4 Replies

3. Shell Programming and Scripting

Splitting a text file into smaller files with awk, how to create a different name for each new file

Hello, I have some large text files that look like, putrescine Mrv1583 01041713302D 6 5 0 0 0 0 999 V2000 2.0928 -0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 5.6650 0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 3.5217 ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

4. Shell Programming and Scripting

Execution of loop :Splitting a single file into multiple .dat file

hdr=$(cut -c1 $path$file|head -1)#extract header”H” trl=$(cut -c|path$file|tail -1)#extract trailer “T” SplitFile=$(cut -c 50-250 $path 1$newfile |sed'$/ *$//' head -1')# to trim white space and extract table name If; then # start loop if it is a header While read I #read file Do... (4 Replies)
Discussion started by: SwagatikaP1
4 Replies

5. Shell Programming and Scripting

Splitting XML file on basis of line number into multiple file

Hi All, I have more than half million lines of XML file , wanted to split in four files in a such a way that top 7 lines should be present in each file on top and bottom line of should be present in each file at bottom. from the 8th line actual record starts and each record contains 15 lines... (14 Replies)
Discussion started by: ajju
14 Replies

6. UNIX for Dummies Questions & Answers

Extracting data from one file, based on another file (splitting)

Dear All, I have two files but want to extract data from one based on another... can you please help me file 1 David Tom Ellen and file 2 David|0010|testnamez|resultsz David|0004|testnamex|resultsx Tom|0010|testnamez|resultsz Tom|0004|testnamex|resultsx Ellen|0010|testnamez|resultsz... (12 Replies)
Discussion started by: A-V
12 Replies

7. UNIX for Dummies Questions & Answers

Pipe binary file matches grep results to file

I am using grep to match a pattern, but the output is strange. $ grep -r -o "pattern" * Gives me: Binary file foo1 matches Binary file foo2 matches Binary file foo3 matches To find the lines before/after, I then have to use the following on each file: $ strings foo1 | grep -A1 -B1... (0 Replies)
Discussion started by: chipperuga
0 Replies

8. Shell Programming and Scripting

Splitting a file in to multiple files and passing each individual file to a command

I have an input file with contents like: MainFile.dat: 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 ... (4 Replies)
Discussion started by: rkrish
4 Replies

9. Shell Programming and Scripting

File splitting, naming file according to internal field

Hi All, I have a rather stange set of requirements that I'm hoping someone here could help me with. We receive a file that is actually a concatenation of 4 files (don't believe this would change, but ideally the solution would handle n files). The super-file looks like:... (7 Replies)
Discussion started by: Leedor
7 Replies

10. UNIX for Dummies Questions & Answers

Splitting a file based on record sin another file

All, We receive a file with a large no of records (records can vary) and we have to split it into two files based on another file. e.g. File1: UHDR 2008112 "25187","00000022","00",21-APR-1991,"" ,"D",-000000519,+0000000000,"C", ,+000000000,+000000000,000000000,"2","" ,21-APR-1991... (7 Replies)
Discussion started by: er_ashu
7 Replies
Login or Register to Ask a Question