Variable of Path directory is not parsing in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable of Path directory is not parsing in awk
# 1  
Old 04-20-2016
Variable of Path directory is not parsing in awk

Hi All,

i had to split one files into 10 equally. For that i have coded below awk.
Code:
OUTPUT_FILE=/home/sit/path/Files/file_EXPORT.lst
DIR_NM=`dirname ${OUTPUT_FILE}`

awk -v CURR_DATE="$(date +'%d-%m-%Y-%H-%M')" -v pth=$DIR_NM '{print >> pth/"tgt_file_name"CURR_DATE"_"NR%10 }' ${OUTPUT_FILE}

but the pth is not parsing or i don't know what is happening i am getting below error
Code:
 fatal: division by zero attempted

but when i remove pth or directory it runs fine and generate files in current directory. like below.
Code:
awk -v CURR_DATE="$(date +'%d-%m-%Y-%H-%M')" -v pth=$DIR_NM '{print >> "tgt_file_name"CURR_DATE"_" NR%10 }' ${OUTPUT_FILE}

but i want to create file in some directory only and i cannot hard code it.
Kindly help on this.

Last edited by looney; 04-20-2016 at 09:13 AM..
# 2  
Old 04-20-2016
What does $DIR_NM evaluate to? Moreover what is the dirname "command" -- A script?
# 3  
Old 04-20-2016
Code:
dirname

will extract directory path from complete file path eg
Code:
dirname /home/admin/filename

evaluate to
Code:
/home/admin

i am putting this directory path into $DIR_NM
# 4  
Old 04-20-2016
Hello looney,

Not completely sure about your whole requirement, could you please change pth/"tgt_file_name"CURR_DATE"_"NR%10 to following into your code and let me know how it goes please.
Code:
 pth"/tgt_file_name"CURR_DATE"_"NR%10

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 04-20-2016
One more query, there will be around 10 million records in source file, that must be distributed/split exactly in 10 files. Since i have used awk print >>. Do i need to close it also. ?
Thanks
# 6  
Old 04-20-2016
Hello looney,

Quote:
One more query, there will be around 10 million records in source file
Yes, you could do division by int(NR/1000000) to make only 10 files as per your requirement. You could double check it as I don't have 10 million line Input_file with me, so logic is simple like if you wanted to make 10 files just divide the total number of lines by the number of files which you need and you will get per file total number of lines then.
Quote:
Do i need to close it also. ?
Yes, we do need to close the files for example close(f) where f is a variable for the filename.
I hope this helps you.

Thanks,
R. Singh
# 7  
Old 04-20-2016
If you want to split the source file in to 10 equal pieces, and the files renumbered for easy identification.

split -l 1000000 --numeric-suffixes --suffix-length=3 Source_file target_file.

Last edited by ashish_samarth; 04-20-2016 at 04:57 PM.. Reason: Split by line number mentioned was not correct
This User Gave Thanks to ashish_samarth For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What is the difference ../directory path and ./directory path in ksh?

What is the difference ../directory path and ./directory path in ksh? (1 Reply)
Discussion started by: TestKing
1 Replies

2. Shell Programming and Scripting

Parse Directory path - awk

Hi All, Need some help in parsing a directory listing .. output into 2 files Input file level1,/level2/level3/level4/ora001,10,IBB23 level1,/level2/level3/level4/ora001/blu1,,IBB23 level1,/level2/level3/level4/ora001/clu1,,IBB23 level1,/level2/level3/level4/ora002,,IBB24... (10 Replies)
Discussion started by: greycells
10 Replies

3. UNIX for Advanced & Expert Users

How can i assign directory path to a variable in perl?

Hai how can I assign directory path to a variable in perl Thanks&Regards kiran (3 Replies)
Discussion started by: kiran425
3 Replies

4. UNIX for Dummies Questions & Answers

How can i assign directory path to a variable in perl?

Hai how can I assign directory path to a variable in perl Thanks&Regards kiran (2 Replies)
Discussion started by: kiran425
2 Replies

5. UNIX for Dummies Questions & Answers

Find command fails when a space is in the directory path variable

I have a script like this running under OS X 10.8. The problem arises when the find command encounters a space in the path name. I need the "dir" variable as I'll be extending the script to more general use. #!/bin/bash CFS=$IFS IFS=$(echo) set dir = "/Users/apta/Library/Mail\... (3 Replies)
Discussion started by: apta
3 Replies

6. Shell Programming and Scripting

sed/awk for extracting directory from file path

Hi, I have following path: set file_path = D:/forums/prac/somedir/new1/file1.txt or set file_path = E:/new/forums1/prac/somedir/new2/file2.txt I need to grep "somedir" from file path. In this case preceding directory "prac" remains same for both the paths, but directories preceding... (7 Replies)
Discussion started by: sarbjit
7 Replies

7. Shell Programming and Scripting

Custom directory path variable

I'm trying to write my first shell script and got a bit stuck with this: I've got myscript.sh that executes from /fromhere. If the script is run with the syntax ./myscript.sh tothere: I need to make a variable inside the script containing /fromhere/tothere ...and if the script is run with... (10 Replies)
Discussion started by: Chronomaly
10 Replies

8. Shell Programming and Scripting

Help with directory path parsing

I've been working on a UNIX script (csh) that will be starting a java application The goal is to get the version number and location (needed by the application) from the path of the script Example: Location of the script= /apps/myapp/versionNum/script/start.csh I need:... (8 Replies)
Discussion started by: brianjbrady
8 Replies

9. Shell Programming and Scripting

Variable directory name in path

I need to write a login script for multiple computers, however, one of the directories in question will have a different name from computer to computer. ~/Library/Application\ Support/Firefox/Profiles/<unique filename>.default/myfile For the directory named <unique filename>.default , I... (2 Replies)
Discussion started by: glev2005
2 Replies

10. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies
Login or Register to Ask a Question