3

I have a text file which looks like this

../pool/main/a/aalib/libaa1_1.4p5-44build2_amd64.deb
../pool/main/a/accountsservice/accountsservice_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/accountsservice/gir1.2-accountsservice-1.0_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/accountsservice/libaccountsservice0_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/acl/acl_2.2.52-3build1_amd64.deb
../pool/main/a/acl/libacl1_2.2.52-3build1_amd64.deb
../pool/main/a/acl/libacl1-dev_2.2.52-3build1_amd64.deb
../pool/main/a/acpid/acpid_2.0.28-1ubuntu1_amd64.deb
../pool/main/a/acpi-support/acpi-support_0.142_amd64.deb
../pool/main/a/adduser/adduser_3.116ubuntu1_all.deb
../pool/main/a/adium-theme-ubuntu/adium-theme-ubuntu_0.3.4-0ubuntu4_all.deb
../pool/main/a/advancecomp/advancecomp_2.1-1_amd64.deb
../pool/main/a/adwaita-icon-theme/adwaita-icon-theme_3.28.0-1ubuntu1_all.deb
../pool/main/a/aisleriot/aisleriot_3.22.5-1_amd64.deb
../pool/main/a/alabaster/python3-alabaster_0.7.8-1_all.deb
../pool/main/a/alembic/alembic_0.9.3-2ubuntu1_all.deb
../pool/main/a/alembic/python3-alembic_0.9.3-2ubuntu1_all.deb
../pool/main/a/alembic/python-alembic_0.9.3-2ubuntu1_all.deb
../pool/main/a/alsa-driver/alsa-base_1.0.25+dfsg-0ubuntu5_all.deb
../pool/main/a/alsa-driver/linux-sound-base_1.0.25+dfsg-0ubuntu5_all.deb

I want to add space between last slash and name of deb package file e.g.

../pool/main/a/aalib/ libaa1_1.4p5-44build2_amd64.deb

I tried using sed but couldn't get it right.

3 Answers 3

4
sed 's_.*/_& _'

substitutes everything until the last slash (.*/, regexes are greedy) with itself followed by a space (&␣) in every line.

Example run

Provided your text file is called file and resides in the current working directory:

$ <file sed 's_.*/_& _'
../pool/main/a/aalib/ libaa1_1.4p5-44build2_amd64.deb
../pool/main/a/accountsservice/ accountsservice_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/accountsservice/ gir1.2-accountsservice-1.0_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/accountsservice/ libaccountsservice0_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/acl/ acl_2.2.52-3build1_amd64.deb
../pool/main/a/acl/ libacl1_2.2.52-3build1_amd64.deb
../pool/main/a/acl/ libacl1-dev_2.2.52-3build1_amd64.deb
../pool/main/a/acpid/ acpid_2.0.28-1ubuntu1_amd64.deb
../pool/main/a/acpi-support/ acpi-support_0.142_amd64.deb
../pool/main/a/adduser/ adduser_3.116ubuntu1_all.deb
../pool/main/a/adium-theme-ubuntu/ adium-theme-ubuntu_0.3.4-0ubuntu4_all.deb
../pool/main/a/advancecomp/ advancecomp_2.1-1_amd64.deb
../pool/main/a/adwaita-icon-theme/ adwaita-icon-theme_3.28.0-1ubuntu1_all.deb
../pool/main/a/aisleriot/ aisleriot_3.22.5-1_amd64.deb
../pool/main/a/alabaster/ python3-alabaster_0.7.8-1_all.deb
../pool/main/a/alembic/ alembic_0.9.3-2ubuntu1_all.deb
../pool/main/a/alembic/ python3-alembic_0.9.3-2ubuntu1_all.deb
../pool/main/a/alembic/ python-alembic_0.9.3-2ubuntu1_all.deb
../pool/main/a/alsa-driver/ alsa-base_1.0.25+dfsg-0ubuntu5_all.deb
../pool/main/a/alsa-driver/ linux-sound-base_1.0.25+dfsg-0ubuntu5_all.deb

To alter the file in-place, use:

sed -i 's_.*/_& _' file
4

What can be done is to use pattern grouping and take advantage of greedy globbing:

$ echo '../pool/main/a/aalib/libaa1_1.4p5-44build2_amd64.deb' | 
> sed -r 's/(^.*\/)(.*\.deb)/\1 \2/'
../pool/main/a/aalib/ libaa1_1.4p5-44build2_amd64.deb

The parenthesis correspond to \integer items in replacement pattern. Thus first parenthesis will greedily match everything to the last slash and the second - everything after that

With input file in the question:

$ sed -r 's/(^.*\/)(.*\.deb)/\1 \2/' pool.txt
../pool/main/a/aalib/ libaa1_1.4p5-44build2_amd64.deb
../pool/main/a/accountsservice/ accountsservice_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/accountsservice/ gir1.2-accountsservice-1.0_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/accountsservice/ libaccountsservice0_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/acl/ acl_2.2.52-3build1_amd64.deb
../pool/main/a/acl/ libacl1_2.2.52-3build1_amd64.deb
<...and so on and so forth...>
0
1

And for good measure an awk version

$ awk -F/ '{ OFS="/"; $NF=" "$NF; print; }' /tmp/XXXX
[...]
../pool/main/a/acl/ libacl1_2.2.52-3build1_amd64.deb
../pool/main/a/acl/ libacl1-dev_2.2.52-3build1_amd64.deb
[...]
  • -F/ set the field separator to be /
  • for each line of input, taken from /tmp/XXXX which contains your data
    • set output field separator to be / as well
    • NF contains the number of fields, so $NF is the last field
    • Set the last field to be a space string concatenated with the original last field value
    • and print

You must log in to answer this question.

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