Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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"
fn=${pn##*/}
dir=${pn%/$fn}
dn8=${dir##*/}
dd8=${dir%/$dn8}
dn7=${dd8##*/}
dd7=${dd8/$dn7}
dn6=${dd7##*/}
dd6=${dd7/$dn6}
dn5=${dd6##*/}
dd5=${dd6/$dn5}
.
.
.
to eventually build
dn1dn2dn3dn4dn5dn6dn7dn8fn = ${dn1}_${dn2}_${dn3}_${dn4}_${dn5}_${dn6}_${dn7}_${dn8}_${fn}
Code:
then run the awk script with filename.csv > $dn1dn2dn3dn4dn5dn6dn7dn8fn
or even pick and choose dn3dn5dn6dn7fn=${dn3}_${dn5}_${dn6}_${dn7}_${fn}
and then filename.csv > $dn3dn5dn6dn7fn
anyways, it's pretty ugly, but it works maybe there's an easier way?
The # is like the ## but * wildcards expand to the minimum.
The //string1/string2 substitutes all string1 by string2
(While /string1/string2 would substitute one string1)
A / character is conflicting and must be escaped \/ or "/"
Last edited by MadeInGermany; 05-18-2022 at 01:16 PM.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.