I’ve tried googling for the answer to my question, and i’m sure the answer is out there, I just don’t know what to search. Basically what i’m trying to do is take a line from a file grab the last 2 fields (seperated by “\“) and then print those two fields.
However, I want these fields to be printed out with 2 backslashes in beween them. (i’m working with windows path’s) Here’s my statement
$ line = C:\Windows\System32\folder1\folder2\folder3\executable.exe
$ echo $line | awk -F "\\" '{print $(NF-2)$(NF-0)}'
I want the output to return folder3\\executable.exe
My experience with awk is limited. Thank you in advanced for any help
From the command line:
will give you
for your
lineFSsets the field separator for the line(s) processed,OFSthe output field separators. The double\\are necessary to escape the special meaning of the single\.Note that your assignment to
lineshould enclose the string in""with no spaces before/after the=. I.e.,Alternatively, this script (so.awk)
will give you the same output if invoked as
echo $line | awk -f so.awk data.txtwheredata.txtcontains your one (or possibly more) path(s)Update:
In case you should want to print leading double backslashes:
print "", $(NF-1), $NFwill do the trick by adding an empty string at the start, as mentioned in a helpful comment by @Dennis Williamson