How to replace the content to multiple files?
Clash Royale CLAN TAG#URR8PPP
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 two columns (NC_12345 og855) and discard rest of it. How can I do this?
command-line bash text-processing sed perl
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 two columns (NC_12345 og855) and discard rest of it. How can I do this?
command-line bash text-processing sed perl
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 two columns (NC_12345 og855) and discard rest of it. How can I do this?
command-line bash text-processing sed perl
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 two 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
edited Mar 3 at 9:00
Arslan Tariq
asked Mar 3 at 3:52
Arslan TariqArslan Tariq
212
212
add a comment |
add a comment |
2 Answers
2
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
1
cut -d| -f1
may be an alternative toawk -F| 'print $1'
– jno
Mar 3 at 8:54
@jno Yes, that's an option as well. Feel free to post that as your own answer
– Sergiy Kolodyazhnyy
Mar 3 at 8:59
add a comment |
For cutting out some text, I always think of cut
first, separating by |
as in jno's comment:
cut -d| -f1 file
Or it looks like the format is always 8 characters, then 2 spaces, then 5 more characters, so you could just cut the first 15 characters with
cut -c 1-15 file
Or you could separate fields by spaces, but since there's two spaces between the first & second fields that means you'd cut out fields 1, 2, and 3:
cut -d" " -f 1-3 file
Cut doesn't have in-place file editing like sed
, but you can output to a new file, then move over the original file, for example
for file in file1,file2,file3
do
cut -d" " -f 1-3 "$file" > "$file.2"
mv "$file.2" "$file"
done
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%2f1122637%2fhow-to-replace-the-content-to-multiple-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
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
1
cut -d| -f1
may be an alternative toawk -F| 'print $1'
– jno
Mar 3 at 8:54
@jno Yes, that's an option as well. Feel free to post that as your own answer
– Sergiy Kolodyazhnyy
Mar 3 at 8:59
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
1
cut -d| -f1
may be an alternative toawk -F| 'print $1'
– jno
Mar 3 at 8:54
@jno Yes, that's an option as well. Feel free to post that as your own answer
– Sergiy Kolodyazhnyy
Mar 3 at 8:59
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 Mar 3 at 4:20
answered Mar 3 at 3:58
Sergiy KolodyazhnyySergiy Kolodyazhnyy
74.7k9155325
74.7k9155325
1
cut -d| -f1
may be an alternative toawk -F| 'print $1'
– jno
Mar 3 at 8:54
@jno Yes, that's an option as well. Feel free to post that as your own answer
– Sergiy Kolodyazhnyy
Mar 3 at 8:59
add a comment |
1
cut -d| -f1
may be an alternative toawk -F| 'print $1'
– jno
Mar 3 at 8:54
@jno Yes, that's an option as well. Feel free to post that as your own answer
– Sergiy Kolodyazhnyy
Mar 3 at 8:59
1
1
cut -d| -f1
may be an alternative to awk -F| 'print $1'
– jno
Mar 3 at 8:54
cut -d| -f1
may be an alternative to awk -F| 'print $1'
– jno
Mar 3 at 8:54
@jno Yes, that's an option as well. Feel free to post that as your own answer
– Sergiy Kolodyazhnyy
Mar 3 at 8:59
@jno Yes, that's an option as well. Feel free to post that as your own answer
– Sergiy Kolodyazhnyy
Mar 3 at 8:59
add a comment |
For cutting out some text, I always think of cut
first, separating by |
as in jno's comment:
cut -d| -f1 file
Or it looks like the format is always 8 characters, then 2 spaces, then 5 more characters, so you could just cut the first 15 characters with
cut -c 1-15 file
Or you could separate fields by spaces, but since there's two spaces between the first & second fields that means you'd cut out fields 1, 2, and 3:
cut -d" " -f 1-3 file
Cut doesn't have in-place file editing like sed
, but you can output to a new file, then move over the original file, for example
for file in file1,file2,file3
do
cut -d" " -f 1-3 "$file" > "$file.2"
mv "$file.2" "$file"
done
add a comment |
For cutting out some text, I always think of cut
first, separating by |
as in jno's comment:
cut -d| -f1 file
Or it looks like the format is always 8 characters, then 2 spaces, then 5 more characters, so you could just cut the first 15 characters with
cut -c 1-15 file
Or you could separate fields by spaces, but since there's two spaces between the first & second fields that means you'd cut out fields 1, 2, and 3:
cut -d" " -f 1-3 file
Cut doesn't have in-place file editing like sed
, but you can output to a new file, then move over the original file, for example
for file in file1,file2,file3
do
cut -d" " -f 1-3 "$file" > "$file.2"
mv "$file.2" "$file"
done
add a comment |
For cutting out some text, I always think of cut
first, separating by |
as in jno's comment:
cut -d| -f1 file
Or it looks like the format is always 8 characters, then 2 spaces, then 5 more characters, so you could just cut the first 15 characters with
cut -c 1-15 file
Or you could separate fields by spaces, but since there's two spaces between the first & second fields that means you'd cut out fields 1, 2, and 3:
cut -d" " -f 1-3 file
Cut doesn't have in-place file editing like sed
, but you can output to a new file, then move over the original file, for example
for file in file1,file2,file3
do
cut -d" " -f 1-3 "$file" > "$file.2"
mv "$file.2" "$file"
done
For cutting out some text, I always think of cut
first, separating by |
as in jno's comment:
cut -d| -f1 file
Or it looks like the format is always 8 characters, then 2 spaces, then 5 more characters, so you could just cut the first 15 characters with
cut -c 1-15 file
Or you could separate fields by spaces, but since there's two spaces between the first & second fields that means you'd cut out fields 1, 2, and 3:
cut -d" " -f 1-3 file
Cut doesn't have in-place file editing like sed
, but you can output to a new file, then move over the original file, for example
for file in file1,file2,file3
do
cut -d" " -f 1-3 "$file" > "$file.2"
mv "$file.2" "$file"
done
edited Mar 3 at 11:05
answered Mar 3 at 10:58
Xen2050Xen2050
6,92622344
6,92622344
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.
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