renaming files to use their path in awk
hi guys,
i'm trying to rename a csv file using the path to that file as part of it's name. here's my situation Code:
filename.csv is in path /home/abc1/def1/filename.csv Code:
filename.csv > /home/abc1/def1/abc1_def1_filename.csv |
Examples:
Code:
f="/home/abc1/def1/filename.csv" |
Not sure I follow the question, but in awk the file name is in the variable FILENAME and the split function can be used to break up the parts.
The question is a bit vague on whether this is just an output from the awk script or if you want to redirect it to that new file (the above can do both) |
Given previous posts, I'd say this is using the wrong tool for the job. A spanner can be used to bang in a nail, but it ain't a proper hammer ...
|
Using the variable modifiers in post 2
Code:
pn="/home/abc1/def1/filename.csv" |
hey guys,
MadeInGermany i see what you're doing, and it works for me, thanks! so i've included your code as part of the script that i run Code:
pn="/home/abc/def/filename.csv" Code:
abc_def_filename.csv having learned from this, my next step is that my csv file is imbedded 8 directories deep. so i thought i would just keep creating the dd and dn variables as you have? and end up with something like Code:
pn="/home/abc/def/ghi/jkl/mno/pqr/stu/filename.csv" Code:
then run the awk script with filename.csv > $dn1dn2dn3dn4dn5dn6dn7dn8fn |
If the path is too deep or the depth is unknown, then in bash you can use other variable modifiers
Code:
pn="/home/abc/def/filename.csv" The //string1/string2 substitutes all string1 by string2 (While /string1/string2 would substitute one string1) A / character is conflicting and must be escaped \/ or "/" |
As advised, awk can just do it all:
Code:
$ awk 'BEGIN{fn="/home/abc/def/ghi/jkl/mno/pqr/stu/filename.csv";split(fn,arr,"/");print "your stuff" > arr[2]"_"arr[4]"_"arr[6]".csv"}' Code:
awk '<do some magic>{split(FILENAME,arr,"/"); print > <your array combo here>}' /home/abc/def/ghi/jkl/mno/pqr/stu/filename.csv |
now that i have a unique filename for each file i need to copy all the files into a "common" directory
Code:
/home/abc/def/ghi/123/mno/pqr/stu/filename.csv > /home/common/abc_def_ghi_123_mno_pqr_stu_filename.csv i know, it's a cumbersome way of getting the job done, but i know how to run the second awk script, what it needs, and it does what i need done |
you can split the filename like this:
Code:
pn=/home/abc/def/ghi/123/mno/pqr/stu/filename.csv Code:
pn=/home/abc/def/ghi/123/mno/pqr/stu/filename.csv |
pan64
i don't follow you. once i have a unique filename (using the method that we worked out above) i want to copy that file into a common folder where i will end up having a bunch of files with almost the same names (but slightly different), and then i will run my second awk script on all those files. |
in that case replace the last line (or add):
Code:
awk 'very good script' "$pn" > "$new_pn" |
just doing
Code:
awk 'very good script' filename.csv > /home/common_dir/abc_def_ghi_123_mno_pqr_stu_filename.csv Code:
/home/abc/def/ghi/123/mno/pqr/stu/ |
Assuming you are using gawk, again there is no issue to have multiple scripts.
Simply use the BEGIN_FILE{} declarative, use the split command on FILENAME as suggested and make your new file name and then simply redirect the output to the new file in whatever location you like. Code:
awk 'BEGIN_FILE{split(FILENAME,pieces,"/");filename=<make your new file name here>;path=/path/to/new/location/filename}<do your magic on file while redirecting to "path">' <list of files here using full path name |
All times are GMT -5. The time now is 02:31 AM. |