Edit text file to add space
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 |
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 |
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 '18 at 22:40
harshit
1149
1149
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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 |
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 |
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 |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 23:02
answered Dec 2 '18 at 22:49
dessert
22k56198
22k56198
add a comment |
add a comment |
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 |
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 |
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 '18 at 22:59
answered Dec 2 '18 at 22:49
Sergiy Kolodyazhnyy
69.4k9144306
69.4k9144306
add a comment |
add a comment |
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 |
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 |
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 '18 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