awk splits file in to multiple - how to get filenames in variables withing ksh?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk splits file in to multiple - how to get filenames in variables withing ksh?
# 1  
Old 03-17-2010
awk splits file in to multiple - how to get filenames in variables withing ksh?

Hi all,

I'm using awk in a .ksh script to split one file by line prefix into different files (up to 4). The files are named after the prefix in the line, and a sequence no.
Is there any way to get the filenames in to variables too? I need _ftpfile1, _ftpfile2, _ftpfile3 and _ftpfile4 which are the results of the split from file _ftpfile.

Any ideas? Is this possible withing the awk? Or do I need to somehow search in the directory for the files?

Thanks,
Michael

Code:
awk '{close(f);f=$1}{print > f".txt"}' $_ftpfile

# 2  
Old 03-17-2010
Quote:
Originally Posted by spidermike
Hi all,

I'm using awk in a .ksh script to split one file by line prefix into different files (up to 4). The files are named after the prefix in the line, and a sequence no.
Is there any way to get the filenames in to variables too? I need _ftpfile1, _ftpfile2, _ftpfile3 and _ftpfile4 which are the results of the split from file _ftpfile.

Any ideas? Is this possible withing the awk? Or do I need to somehow search in the directory for the files?

Thanks,
Michael

Code:
awk '{close(f);f=$1}{print > f".txt"}' $_ftpfile

Code:
awk '{close(f);f=$1}{print > (f "_" FILENAME ".txt")}' $_ftpfile

# 3  
Old 03-17-2010
But this only creates the split files with the original filename of the file splitted in the new filenames?

The result is the files as shown in the directory:

BND000000838_BHD000000838.TXT.3292379.txt
BDD000000838_BHD000000838.TXT.3292379.txt
BAD000000838_BHD000000838.TXT.3292379.txt

instead of

BND000000838.txt
BDD000000838.txt
BAD000000838.txt

which it was before?

What I need is the filenames assigned to a variable in the script, like

Code:
_ftpfile1 = BND000000838.txt
_ftpfile2 = BDD000000838.txt
_ftpfile3 = BAD000000838.txt

to be able to do further processing within the .ksh like preparing them for ftp transfer etc.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning multiple variables in ksh script

Is it possible to have a user input multiple words in one line and have the script assign each word a variable? I'm stuck please assist. Example using "BILL JOHN SARA JILL" as what the user could type: printf "Enter account names: " BILL JOHN SARA JILL read input (9 Replies)
Discussion started by: seekryts15
9 Replies

2. Shell Programming and Scripting

Issue with running multiple commands withing su command

RHEL 6.2/Bash shell root user will be executing the below script. It switches to oracle user and expect to do the following things A. Source the environment variables for BATGPRD Database (the file used for sourcing is shown below after the script) B. Shutdown the DB from sqlplus -- The... (13 Replies)
Discussion started by: omega3
13 Replies

3. Shell Programming and Scripting

Grep and replace multiple strings in a file with multiple filenames in a file

Hi, I have a file containing list of strings like i: Pink Yellow Green and I have file having list of file names in a directory j : a b c d Where j contains of a ,b,c,d are as follows a: Pink (3 Replies)
Discussion started by: madabhg
3 Replies

4. Solaris

[Solved] Using awk withing a shell script

I am trying to use an awk command within a ksh script but it is not working - I am able to run from command line with no problem. It does not err out - it just does not produce a file with final count. awk "{s+=$0} END {print s}" es.out > es.cntAny help would be greatly appreciated. Thanks (6 Replies)
Discussion started by: bjdamon
6 Replies

5. Shell Programming and Scripting

How to store results of multiple sql queries in shell variables in ksh?

Hi, I have a script where I make a sqlplus connection. In the script I have multiple sql queries within that sqlplus connection. I want the result of the queries to be stored in shell variables declared earlier. I dont want to use procedures. Is there anyway else. Thanks in advance.. Cheers (6 Replies)
Discussion started by: gonchusirsa
6 Replies

6. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

7. Shell Programming and Scripting

Extract variables from filenames and output to file

I need some help. I have a list of files (thousands) and would like to extract some variables from the file name and save that to a file The list of files look like: I am trying to write the following script but I am stuck at how I can get thevariables 'doy' and 'yr' from each file and then... (5 Replies)
Discussion started by: malandisa
5 Replies

8. Shell Programming and Scripting

extract multiple cloumns from multiple files; skip rows and include filenames; awk

Hello, I am trying to write a bash shell script that does the following: 1.Finds all *.txt files within my directory of interest 2. reads each of the files (25 files) one by one (tab-delimited format and have the same data format) 3. skips the first 10 rows of the file 4. extracts and... (4 Replies)
Discussion started by: manishabh
4 Replies

9. Shell Programming and Scripting

accessing ksh variables from awk

hi everybody! i am running this ksh script for replacing a set of strings by another set of new ones. i am getting both these from a file. also, the strings that i want to replace, are sub-strings(can occur more than once in each chunk) in a big chunk of data that i have bulk-copied(bcp utility)... (1 Reply)
Discussion started by: trupti wagh
1 Replies

10. Shell Programming and Scripting

passing variables to awk from ksh script

I'm trying to write a ksh script that uses awk, but I want to pass variables to awk. For example (not working): if ];then searchstr=$1 lsof -i | awk '{if($9~/SEARCHSTR/) print $2} SEARCHSTR=$searchstr' else echo "usage: $0 <search string>" fi I tried several options. Is it... (3 Replies)
Discussion started by: rein
3 Replies
Login or Register to Ask a Question