How to replace the content to multiple files?How to replace a string on the 5th line of multiple text...
What formula could mimic the following curve?
What is the wife of a henpecked husband called?
How to acknowledge an embarrassing job interview, now that I work directly with the interviewer?
It took me a lot of time to make this, pls like. (YouTube Comments #1)
A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?
What happens if a wizard reaches level 20 but has no 3rd-level spells that they can use with the Signature Spells feature?
Program that converts a number to a letter of the alphabet
How would an AI self awareness kill switch work?
Tikzing a circled star
Does the "particle exchange" operator have any validity?
Why zero tolerance on nudity in space?
Why can a 352GB NumPy ndarray be used on an 8GB memory macOS computer?
How is the Incom shipyard still in business?
How experienced do I need to be to go on a photography workshop?
Quenching swords in dragon blood; why?
"On one hand" vs "on the one hand."
Overfitting and Underfitting
If I delete my router's history can my ISP still provide it to my parents?
Why did the villain in the first Men in Black movie care about Earth's Cockroaches?
When does coming up with an idea constitute sufficient contribution for authorship?
Slow moving projectiles from a hand-held weapon - how do they reach the target?
What's a good word to describe a public place that looks like it wouldn't be rough?
Are Advaita and Karma theory completely contradictory?
What's the rationale behind the objections to these measures against human trafficking?
How to replace the content to multiple files?
How to replace a string on the 5th line of multiple text files?How do I replace multiple lines with single word in file(inplace replace)?Comparing two text filessplit a file based on pre-defined set of rowsReplace string with multiline file contentOnly delete files but not folders with rmcp directories and files, preserving directories and overwriting filesHow to replace a particular field in a file based on the content of another field?multiple file text replace with sedReplace all lines between two patterns (inclusive) with a file content
I have multiple files containing content like the following:
File 1
NC_12548 og789 |nd784 -2 -54 -6
NC_12548 og789 |nd784 -2 -54 -6
NC_12548 og789 |nd784 -2 -54 -6
File2
NC_54456 og789 |nd784 -5 -56 -6
NC_98123 og859 |nd784 -5 -84 -5
NC_689.1 og456 |nd784 -5 -54 +8
File3
NC_54456 og789 |nd784 -5 -56 -6
NC_98123 og859 |nd784 -5 -84 -5
NC_689.1 og456 |nd784 -5 -54 +8
I want to keep the only first to columns (NC_12345 og855) and discard rest of it. How can I do this?
command-line bash text-processing sed perl
New contributor
add a comment |
I have multiple files containing content like the following:
File 1
NC_12548 og789 |nd784 -2 -54 -6
NC_12548 og789 |nd784 -2 -54 -6
NC_12548 og789 |nd784 -2 -54 -6
File2
NC_54456 og789 |nd784 -5 -56 -6
NC_98123 og859 |nd784 -5 -84 -5
NC_689.1 og456 |nd784 -5 -54 +8
File3
NC_54456 og789 |nd784 -5 -56 -6
NC_98123 og859 |nd784 -5 -84 -5
NC_689.1 og456 |nd784 -5 -54 +8
I want to keep the only first to columns (NC_12345 og855) and discard rest of it. How can I do this?
command-line bash text-processing sed perl
New contributor
add a comment |
I have multiple files containing content like the following:
File 1
NC_12548 og789 |nd784 -2 -54 -6
NC_12548 og789 |nd784 -2 -54 -6
NC_12548 og789 |nd784 -2 -54 -6
File2
NC_54456 og789 |nd784 -5 -56 -6
NC_98123 og859 |nd784 -5 -84 -5
NC_689.1 og456 |nd784 -5 -54 +8
File3
NC_54456 og789 |nd784 -5 -56 -6
NC_98123 og859 |nd784 -5 -84 -5
NC_689.1 og456 |nd784 -5 -54 +8
I want to keep the only first to columns (NC_12345 og855) and discard rest of it. How can I do this?
command-line bash text-processing sed perl
New contributor
I have multiple files containing content like the following:
File 1
NC_12548 og789 |nd784 -2 -54 -6
NC_12548 og789 |nd784 -2 -54 -6
NC_12548 og789 |nd784 -2 -54 -6
File2
NC_54456 og789 |nd784 -5 -56 -6
NC_98123 og859 |nd784 -5 -84 -5
NC_689.1 og456 |nd784 -5 -54 +8
File3
NC_54456 og789 |nd784 -5 -56 -6
NC_98123 og859 |nd784 -5 -84 -5
NC_689.1 og456 |nd784 -5 -54 +8
I want to keep the only first to columns (NC_12345 og855) and discard rest of it. How can I do this?
command-line bash text-processing sed perl
command-line bash text-processing sed perl
New contributor
New contributor
edited 4 hours ago
Arslan Tariq
New contributor
asked 4 hours ago
Arslan TariqArslan Tariq
112
112
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
With awk
you can just use |
as column separator and print the first column:
awk -F '|' '{print $1}' file1.txt file2.txt file3.txt
output will be concatenaded. If it's necessary to keep output in separate files, consider using a for loop in shell around awk
# assuming they're all in the same directory, hence `*`
for fname in ./file*.txt ; do
# add extension to current file in "$fname" variable indicate new file
# > does the actual redirection
awk -F '|' '{print $1}' "$fname" > "$fname".new
done
Having new output in .new
might be desirable for backup. Otherwise, we can use sed -i
to perform in-file replacement. Run it without -i
first for test trial
# use file*.txt if they're all in the current directory
sed -i 's/|.*$//' file1.txt file2.txt file3.txt
sed -i 's/(^.*)|.*/1/g' file1.txt file2.txt file3.txt
Another option is via Python:
#!/usr/bin/env python3
import sys
for fname in sys.argv:
with open(fname) as fd_read, open(fname+'.new','w') as fd_write:
for line in fd_read:
fd_write.write(line.split('|')[0] + 'n')
This script is intended to be used as ./script.py file1.txt file2.txt file3.txt
and will write output to new files with .new
extension
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
});
}
});
Arslan Tariq is a new contributor. Be nice, and check out our Code of Conduct.
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%2f1122637%2fhow-to-replace-the-content-to-multiple-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
With awk
you can just use |
as column separator and print the first column:
awk -F '|' '{print $1}' file1.txt file2.txt file3.txt
output will be concatenaded. If it's necessary to keep output in separate files, consider using a for loop in shell around awk
# assuming they're all in the same directory, hence `*`
for fname in ./file*.txt ; do
# add extension to current file in "$fname" variable indicate new file
# > does the actual redirection
awk -F '|' '{print $1}' "$fname" > "$fname".new
done
Having new output in .new
might be desirable for backup. Otherwise, we can use sed -i
to perform in-file replacement. Run it without -i
first for test trial
# use file*.txt if they're all in the current directory
sed -i 's/|.*$//' file1.txt file2.txt file3.txt
sed -i 's/(^.*)|.*/1/g' file1.txt file2.txt file3.txt
Another option is via Python:
#!/usr/bin/env python3
import sys
for fname in sys.argv:
with open(fname) as fd_read, open(fname+'.new','w') as fd_write:
for line in fd_read:
fd_write.write(line.split('|')[0] + 'n')
This script is intended to be used as ./script.py file1.txt file2.txt file3.txt
and will write output to new files with .new
extension
add a comment |
With awk
you can just use |
as column separator and print the first column:
awk -F '|' '{print $1}' file1.txt file2.txt file3.txt
output will be concatenaded. If it's necessary to keep output in separate files, consider using a for loop in shell around awk
# assuming they're all in the same directory, hence `*`
for fname in ./file*.txt ; do
# add extension to current file in "$fname" variable indicate new file
# > does the actual redirection
awk -F '|' '{print $1}' "$fname" > "$fname".new
done
Having new output in .new
might be desirable for backup. Otherwise, we can use sed -i
to perform in-file replacement. Run it without -i
first for test trial
# use file*.txt if they're all in the current directory
sed -i 's/|.*$//' file1.txt file2.txt file3.txt
sed -i 's/(^.*)|.*/1/g' file1.txt file2.txt file3.txt
Another option is via Python:
#!/usr/bin/env python3
import sys
for fname in sys.argv:
with open(fname) as fd_read, open(fname+'.new','w') as fd_write:
for line in fd_read:
fd_write.write(line.split('|')[0] + 'n')
This script is intended to be used as ./script.py file1.txt file2.txt file3.txt
and will write output to new files with .new
extension
add a comment |
With awk
you can just use |
as column separator and print the first column:
awk -F '|' '{print $1}' file1.txt file2.txt file3.txt
output will be concatenaded. If it's necessary to keep output in separate files, consider using a for loop in shell around awk
# assuming they're all in the same directory, hence `*`
for fname in ./file*.txt ; do
# add extension to current file in "$fname" variable indicate new file
# > does the actual redirection
awk -F '|' '{print $1}' "$fname" > "$fname".new
done
Having new output in .new
might be desirable for backup. Otherwise, we can use sed -i
to perform in-file replacement. Run it without -i
first for test trial
# use file*.txt if they're all in the current directory
sed -i 's/|.*$//' file1.txt file2.txt file3.txt
sed -i 's/(^.*)|.*/1/g' file1.txt file2.txt file3.txt
Another option is via Python:
#!/usr/bin/env python3
import sys
for fname in sys.argv:
with open(fname) as fd_read, open(fname+'.new','w') as fd_write:
for line in fd_read:
fd_write.write(line.split('|')[0] + 'n')
This script is intended to be used as ./script.py file1.txt file2.txt file3.txt
and will write output to new files with .new
extension
With awk
you can just use |
as column separator and print the first column:
awk -F '|' '{print $1}' file1.txt file2.txt file3.txt
output will be concatenaded. If it's necessary to keep output in separate files, consider using a for loop in shell around awk
# assuming they're all in the same directory, hence `*`
for fname in ./file*.txt ; do
# add extension to current file in "$fname" variable indicate new file
# > does the actual redirection
awk -F '|' '{print $1}' "$fname" > "$fname".new
done
Having new output in .new
might be desirable for backup. Otherwise, we can use sed -i
to perform in-file replacement. Run it without -i
first for test trial
# use file*.txt if they're all in the current directory
sed -i 's/|.*$//' file1.txt file2.txt file3.txt
sed -i 's/(^.*)|.*/1/g' file1.txt file2.txt file3.txt
Another option is via Python:
#!/usr/bin/env python3
import sys
for fname in sys.argv:
with open(fname) as fd_read, open(fname+'.new','w') as fd_write:
for line in fd_read:
fd_write.write(line.split('|')[0] + 'n')
This script is intended to be used as ./script.py file1.txt file2.txt file3.txt
and will write output to new files with .new
extension
edited 4 hours ago
answered 4 hours ago
Sergiy KolodyazhnyySergiy Kolodyazhnyy
73.4k9153318
73.4k9153318
add a comment |
add a comment |
Arslan Tariq is a new contributor. Be nice, and check out our Code of Conduct.
Arslan Tariq is a new contributor. Be nice, and check out our Code of Conduct.
Arslan Tariq is a new contributor. Be nice, and check out our Code of Conduct.
Arslan Tariq is a new contributor. Be nice, and check out our Code of Conduct.
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.
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%2f1122637%2fhow-to-replace-the-content-to-multiple-files%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