Edit text file to add space
up vote
3
down vote
favorite
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.
bash 18.04 sed awk
add a comment |
up vote
3
down vote
favorite
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.
bash 18.04 sed awk
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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.
bash 18.04 sed awk
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.
bash 18.04 sed awk
bash 18.04 sed awk
asked Dec 2 at 22:40
harshit
599
599
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
sed 's_.*/_& _'
s
ubstitutes 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
add a comment |
up vote
4
down vote
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...>
add a comment |
up vote
1
down vote
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
- set output field separator to be
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
sed 's_.*/_& _'
s
ubstitutes 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
add a comment |
up vote
4
down vote
accepted
sed 's_.*/_& _'
s
ubstitutes 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
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
sed 's_.*/_& _'
s
ubstitutes 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
sed 's_.*/_& _'
s
ubstitutes 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
edited Dec 2 at 23:02
answered Dec 2 at 22:49
dessert
21.5k55896
21.5k55896
add a comment |
add a comment |
up vote
4
down vote
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...>
add a comment |
up vote
4
down vote
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...>
add a comment |
up vote
4
down vote
up vote
4
down vote
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...>
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...>
edited Dec 2 at 22:59
answered Dec 2 at 22:49
Sergiy Kolodyazhnyy
68.6k9143303
68.6k9143303
add a comment |
add a comment |
up vote
1
down vote
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
- set output field separator to be
add a comment |
up vote
1
down vote
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
- set output field separator to be
add a comment |
up vote
1
down vote
up vote
1
down vote
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
- set output field separator to be
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
- set output field separator to be
answered Dec 3 at 1:41
jonathanjo
1313
1313
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1098007%2fedit-text-file-to-add-space%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown