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

3 Answers 3

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

2
  • 1
    I'm not sure what to do with answers that are most probably correct and helpful as well, don't think I can accept two answers :) Ah well, upvoting it is the least I could do.
    – Sergeant
    Aug 22, 2018 at 9:35
  • Beware using gawk -i inplace like that introduces a security vulnerability unless you modify $AWKPATH not to include . or make sure you run that command from within a working directory where nobody could create a file called inplace or inplace.awk. Jun 24, 2023 at 21:25
2

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

sed -r 's,modules/([^/]*)/.*,&  \1,' file
1
  • 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, 2018 at 9:29
0

Try

sed -i 'h; s#^.*modules/# #; s#/.*$##; H; x; s#\n##;' file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .