1

I have a file with 19k lines like these:

a96abbb5da0985983e113a9a7484c063  management/modules/membership/file1
d7f7dd7e3ede3e323fc0e09381f16caf  management/modules/invoices/path/file2

However, I would like to append the module name at the end of the line. So the result would be:

a96abbb5da0985983e113a9a7484c063  management/modules/membership/file1  membership
d7f7dd7e3ede3e323fc0e09381f16caf  management/modules/invoices/path/file2  invoices

Is there a way to do this with for example sed or something similar? Preferably without an extra file, using -i.

The module name would always be in the path in this format: modules/MODULENAME

glenn jackman
  • 85,964

3 Answers3

3

awk solution.

$ awk -F/ '{print $0"  "$3}' file
a96abbb5da0985983e113a9a7484c063  management/modules/membership/file1  membership
d7f7dd7e3ede3e323fc0e09381f16caf  management/modules/invoices/path/file2  invoices
$

If using gnu awk you can use the inplace feature, rather than having to output to a second file. e.g. awk -i inplace -F/ '{print $0" "$3}' file

steve
  • 21,892
2

with sed, grab the next slash-separated field, and stick it at the end:

sed -r 's,modules/([^/]*)/.*,&  \1,' file
glenn jackman
  • 85,964
  • 1
    Thank you, I used this as inspiration by adding the -i flag and changing modules to /modules/ since it was also picking up the node_modules path which wasn't my intention :) – Sergeant Aug 22 '18 at 09:29
0

Try

sed -i 'h; s#^.*modules/# #; s#/.*$##; H; x; s#\n##;' file
RudiC
  • 8,969