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
  • 84,176
  • 15
  • 116
  • 168
Sergeant
  • 13
  • 2

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,582
  • 5
  • 48
  • 75
  • 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 '18 at 09:35
  • Beware [using gawk -i inplace like that introduces a security vulnerability](/q/749645) 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`. – Stéphane Chazelas Jun 24 '23 at 21:25
2

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

sed -r 's,modules/([^/]*)/.*,&  \1,' file
glenn jackman
  • 84,176
  • 15
  • 116
  • 168
  • 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,889
  • 2
  • 10
  • 22