copy certain spaces from a file
I have a file which looks like this
18DMA H 9996 0.886 5.687 5.320
18DMA H 9997 1.019 5.764 5.247
18DMA Np 9998 0.947 5.584 5.151
18DMA H 9999 1.033 5.541 5.113
18DMA Cn10000 0.880 5.674 5.050
18DMA H10001 0.831 5.616 4.971
18DMA H10002 0.814 5.751 5.091
18DMA H10003 0.957 5.735 5.003
18DMA Cn10004 0.837 5.486 5.185
The desire output is to delete column 3 however since from a certain row/line and next there is no a space between atom name and number I cannot make the deletion by column. Is there any way to make the deletion by selecting certain number of characters? The desire output should be
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185
text-processing
add a comment |
I have a file which looks like this
18DMA H 9996 0.886 5.687 5.320
18DMA H 9997 1.019 5.764 5.247
18DMA Np 9998 0.947 5.584 5.151
18DMA H 9999 1.033 5.541 5.113
18DMA Cn10000 0.880 5.674 5.050
18DMA H10001 0.831 5.616 4.971
18DMA H10002 0.814 5.751 5.091
18DMA H10003 0.957 5.735 5.003
18DMA Cn10004 0.837 5.486 5.185
The desire output is to delete column 3 however since from a certain row/line and next there is no a space between atom name and number I cannot make the deletion by column. Is there any way to make the deletion by selecting certain number of characters? The desire output should be
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185
text-processing
clarify your atom name and number
– RomanPerekhrest
1 hour ago
My problem is in line 18DMA Cn10000 0.880 5.674 5.050 since there is no space between Cn and 1000 so I cannot proceed with copying the desire column. Somehow I need instead of copying a column to copy certain characters to make it work
– Dimitris Mintis
1 hour ago
add a comment |
I have a file which looks like this
18DMA H 9996 0.886 5.687 5.320
18DMA H 9997 1.019 5.764 5.247
18DMA Np 9998 0.947 5.584 5.151
18DMA H 9999 1.033 5.541 5.113
18DMA Cn10000 0.880 5.674 5.050
18DMA H10001 0.831 5.616 4.971
18DMA H10002 0.814 5.751 5.091
18DMA H10003 0.957 5.735 5.003
18DMA Cn10004 0.837 5.486 5.185
The desire output is to delete column 3 however since from a certain row/line and next there is no a space between atom name and number I cannot make the deletion by column. Is there any way to make the deletion by selecting certain number of characters? The desire output should be
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185
text-processing
I have a file which looks like this
18DMA H 9996 0.886 5.687 5.320
18DMA H 9997 1.019 5.764 5.247
18DMA Np 9998 0.947 5.584 5.151
18DMA H 9999 1.033 5.541 5.113
18DMA Cn10000 0.880 5.674 5.050
18DMA H10001 0.831 5.616 4.971
18DMA H10002 0.814 5.751 5.091
18DMA H10003 0.957 5.735 5.003
18DMA Cn10004 0.837 5.486 5.185
The desire output is to delete column 3 however since from a certain row/line and next there is no a space between atom name and number I cannot make the deletion by column. Is there any way to make the deletion by selecting certain number of characters? The desire output should be
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185
text-processing
text-processing
edited 1 hour ago
Kusalananda
121k16228372
121k16228372
asked 1 hour ago
Dimitris Mintis
475
475
clarify your atom name and number
– RomanPerekhrest
1 hour ago
My problem is in line 18DMA Cn10000 0.880 5.674 5.050 since there is no space between Cn and 1000 so I cannot proceed with copying the desire column. Somehow I need instead of copying a column to copy certain characters to make it work
– Dimitris Mintis
1 hour ago
add a comment |
clarify your atom name and number
– RomanPerekhrest
1 hour ago
My problem is in line 18DMA Cn10000 0.880 5.674 5.050 since there is no space between Cn and 1000 so I cannot proceed with copying the desire column. Somehow I need instead of copying a column to copy certain characters to make it work
– Dimitris Mintis
1 hour ago
clarify your atom name and number
– RomanPerekhrest
1 hour ago
clarify your atom name and number
– RomanPerekhrest
1 hour ago
My problem is in line 18DMA Cn10000 0.880 5.674 5.050 since there is no space between Cn and 1000 so I cannot proceed with copying the desire column. Somehow I need instead of copying a column to copy certain characters to make it work
– Dimitris Mintis
1 hour ago
My problem is in line 18DMA Cn10000 0.880 5.674 5.050 since there is no space between Cn and 1000 so I cannot proceed with copying the desire column. Somehow I need instead of copying a column to copy certain characters to make it work
– Dimitris Mintis
1 hour ago
add a comment |
3 Answers
3
active
oldest
votes
Assuming you don't have <TAB>
s but multiple spaces as field separators, and by looking at and counting your sample data, I came up with
$ sed -E 's/^(.{15}).{5}/1/' file
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185
It's using a "back reference" for the first 15 characters to restore them using 1
in the replacement part of the s
ubstitute command.
super!! many thanks!
– Dimitris Mintis
1 hour ago
add a comment |
Use cut
in character mode:
cut -c1-15,21-
you may need to tweak the exact character numbers.
Again, this assumes the input doesn't use TABs (t
characters) as delimiters (which it probably doesn't, since then you wouldn't have the problem of the joined fields in the first place).
add a comment |
$ awk -v OFS='t' 'NF == 5 { sub("[0-9]*$", "", $2) } NF == 6 { $0 = $1 OFS $2 OFS $4 OFS $5 OFS $6 } { print }' file
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185
This short awk
program will do different things to the input line depending on whether it contains 5 or 6 whitespace-delimited fields.
If it contains five fields, it removes all digits from the end of the second fields and leaves the rest as it is. If it contains six fields, it rewrites the line but omits the third field.
The output will be tab-delimited (or delimited by whatever you set OFS
to on the command line).
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f490597%2fcopy-certain-spaces-from-a-file%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
Assuming you don't have <TAB>
s but multiple spaces as field separators, and by looking at and counting your sample data, I came up with
$ sed -E 's/^(.{15}).{5}/1/' file
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185
It's using a "back reference" for the first 15 characters to restore them using 1
in the replacement part of the s
ubstitute command.
super!! many thanks!
– Dimitris Mintis
1 hour ago
add a comment |
Assuming you don't have <TAB>
s but multiple spaces as field separators, and by looking at and counting your sample data, I came up with
$ sed -E 's/^(.{15}).{5}/1/' file
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185
It's using a "back reference" for the first 15 characters to restore them using 1
in the replacement part of the s
ubstitute command.
super!! many thanks!
– Dimitris Mintis
1 hour ago
add a comment |
Assuming you don't have <TAB>
s but multiple spaces as field separators, and by looking at and counting your sample data, I came up with
$ sed -E 's/^(.{15}).{5}/1/' file
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185
It's using a "back reference" for the first 15 characters to restore them using 1
in the replacement part of the s
ubstitute command.
Assuming you don't have <TAB>
s but multiple spaces as field separators, and by looking at and counting your sample data, I came up with
$ sed -E 's/^(.{15}).{5}/1/' file
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185
It's using a "back reference" for the first 15 characters to restore them using 1
in the replacement part of the s
ubstitute command.
answered 1 hour ago
RudiC
3,9941312
3,9941312
super!! many thanks!
– Dimitris Mintis
1 hour ago
add a comment |
super!! many thanks!
– Dimitris Mintis
1 hour ago
super!! many thanks!
– Dimitris Mintis
1 hour ago
super!! many thanks!
– Dimitris Mintis
1 hour ago
add a comment |
Use cut
in character mode:
cut -c1-15,21-
you may need to tweak the exact character numbers.
Again, this assumes the input doesn't use TABs (t
characters) as delimiters (which it probably doesn't, since then you wouldn't have the problem of the joined fields in the first place).
add a comment |
Use cut
in character mode:
cut -c1-15,21-
you may need to tweak the exact character numbers.
Again, this assumes the input doesn't use TABs (t
characters) as delimiters (which it probably doesn't, since then you wouldn't have the problem of the joined fields in the first place).
add a comment |
Use cut
in character mode:
cut -c1-15,21-
you may need to tweak the exact character numbers.
Again, this assumes the input doesn't use TABs (t
characters) as delimiters (which it probably doesn't, since then you wouldn't have the problem of the joined fields in the first place).
Use cut
in character mode:
cut -c1-15,21-
you may need to tweak the exact character numbers.
Again, this assumes the input doesn't use TABs (t
characters) as delimiters (which it probably doesn't, since then you wouldn't have the problem of the joined fields in the first place).
answered 1 hour ago
peterph
23k24457
23k24457
add a comment |
add a comment |
$ awk -v OFS='t' 'NF == 5 { sub("[0-9]*$", "", $2) } NF == 6 { $0 = $1 OFS $2 OFS $4 OFS $5 OFS $6 } { print }' file
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185
This short awk
program will do different things to the input line depending on whether it contains 5 or 6 whitespace-delimited fields.
If it contains five fields, it removes all digits from the end of the second fields and leaves the rest as it is. If it contains six fields, it rewrites the line but omits the third field.
The output will be tab-delimited (or delimited by whatever you set OFS
to on the command line).
add a comment |
$ awk -v OFS='t' 'NF == 5 { sub("[0-9]*$", "", $2) } NF == 6 { $0 = $1 OFS $2 OFS $4 OFS $5 OFS $6 } { print }' file
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185
This short awk
program will do different things to the input line depending on whether it contains 5 or 6 whitespace-delimited fields.
If it contains five fields, it removes all digits from the end of the second fields and leaves the rest as it is. If it contains six fields, it rewrites the line but omits the third field.
The output will be tab-delimited (or delimited by whatever you set OFS
to on the command line).
add a comment |
$ awk -v OFS='t' 'NF == 5 { sub("[0-9]*$", "", $2) } NF == 6 { $0 = $1 OFS $2 OFS $4 OFS $5 OFS $6 } { print }' file
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185
This short awk
program will do different things to the input line depending on whether it contains 5 or 6 whitespace-delimited fields.
If it contains five fields, it removes all digits from the end of the second fields and leaves the rest as it is. If it contains six fields, it rewrites the line but omits the third field.
The output will be tab-delimited (or delimited by whatever you set OFS
to on the command line).
$ awk -v OFS='t' 'NF == 5 { sub("[0-9]*$", "", $2) } NF == 6 { $0 = $1 OFS $2 OFS $4 OFS $5 OFS $6 } { print }' file
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185
This short awk
program will do different things to the input line depending on whether it contains 5 or 6 whitespace-delimited fields.
If it contains five fields, it removes all digits from the end of the second fields and leaves the rest as it is. If it contains six fields, it rewrites the line but omits the third field.
The output will be tab-delimited (or delimited by whatever you set OFS
to on the command line).
edited 1 hour ago
answered 1 hour ago
Kusalananda
121k16228372
121k16228372
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f490597%2fcopy-certain-spaces-from-a-file%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
clarify your atom name and number
– RomanPerekhrest
1 hour ago
My problem is in line 18DMA Cn10000 0.880 5.674 5.050 since there is no space between Cn and 1000 so I cannot proceed with copying the desire column. Somehow I need instead of copying a column to copy certain characters to make it work
– Dimitris Mintis
1 hour ago