Replace all but a set of characters in a file with newline
up vote
2
down vote
favorite
how to delete all characters in file except numbers and "." ,
each word (numbers/dot) should be in new line in file see example2
- the solution can be with sed or awk or ksh syntax
remark - the solution must be according to the example 2
example 1
file before edit
192.0.22.1++0.1
e32)5.500.5.5*kjcdr
##@$1.1.1.1+++jmjh
1.1.1.1333
33331.1.1.1
@5.5.5.??????
~3de.ede5.5.5.5
1.1.1.13444r54
192.9.30.174
&&^#%5.5.5.5
:5.5.5.5@%%^^&*
:5.5.5.5:
**22.22.22.22
172.78.0.1()*5.4.3.277
3.3.3ki.3.
example 2 of file after delete all characters except numbers and "." charter , each new word will be in new line
192.0.22.1
0.1
32 5.500.5.5
1.1.1.1
1.1.1.1333
33331.1.1.1
5.5.5.
.
5.5.5.5
1.1.1.13444
54
192.9.30.174
5.5.5.5
5.5.5.5
5.5.5.5
22.22.22.22
172.78.0.1
5.4.3.277
3.3.3 .3.
linux sed awk perl
add a comment |
up vote
2
down vote
favorite
how to delete all characters in file except numbers and "." ,
each word (numbers/dot) should be in new line in file see example2
- the solution can be with sed or awk or ksh syntax
remark - the solution must be according to the example 2
example 1
file before edit
192.0.22.1++0.1
e32)5.500.5.5*kjcdr
##@$1.1.1.1+++jmjh
1.1.1.1333
33331.1.1.1
@5.5.5.??????
~3de.ede5.5.5.5
1.1.1.13444r54
192.9.30.174
&&^#%5.5.5.5
:5.5.5.5@%%^^&*
:5.5.5.5:
**22.22.22.22
172.78.0.1()*5.4.3.277
3.3.3ki.3.
example 2 of file after delete all characters except numbers and "." charter , each new word will be in new line
192.0.22.1
0.1
32 5.500.5.5
1.1.1.1
1.1.1.1333
33331.1.1.1
5.5.5.
.
5.5.5.5
1.1.1.13444
54
192.9.30.174
5.5.5.5
5.5.5.5
5.5.5.5
22.22.22.22
172.78.0.1
5.4.3.277
3.3.3 .3.
linux sed awk perl
1
Your sample output is a bit inconsistent. Why are32
and5.500.5.5
both on line 3? Why is there no3
(from3de
) between the lines for5.5.5.
and.
? Why are3.3.3
and.3.
both on the last line?
– jw013
Nov 15 '12 at 19:15
2
This looks an awful lot like this question on ServerFault -- did you not get a good answer there?
– glenn jackman
Nov 15 '12 at 21:29
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
how to delete all characters in file except numbers and "." ,
each word (numbers/dot) should be in new line in file see example2
- the solution can be with sed or awk or ksh syntax
remark - the solution must be according to the example 2
example 1
file before edit
192.0.22.1++0.1
e32)5.500.5.5*kjcdr
##@$1.1.1.1+++jmjh
1.1.1.1333
33331.1.1.1
@5.5.5.??????
~3de.ede5.5.5.5
1.1.1.13444r54
192.9.30.174
&&^#%5.5.5.5
:5.5.5.5@%%^^&*
:5.5.5.5:
**22.22.22.22
172.78.0.1()*5.4.3.277
3.3.3ki.3.
example 2 of file after delete all characters except numbers and "." charter , each new word will be in new line
192.0.22.1
0.1
32 5.500.5.5
1.1.1.1
1.1.1.1333
33331.1.1.1
5.5.5.
.
5.5.5.5
1.1.1.13444
54
192.9.30.174
5.5.5.5
5.5.5.5
5.5.5.5
22.22.22.22
172.78.0.1
5.4.3.277
3.3.3 .3.
linux sed awk perl
how to delete all characters in file except numbers and "." ,
each word (numbers/dot) should be in new line in file see example2
- the solution can be with sed or awk or ksh syntax
remark - the solution must be according to the example 2
example 1
file before edit
192.0.22.1++0.1
e32)5.500.5.5*kjcdr
##@$1.1.1.1+++jmjh
1.1.1.1333
33331.1.1.1
@5.5.5.??????
~3de.ede5.5.5.5
1.1.1.13444r54
192.9.30.174
&&^#%5.5.5.5
:5.5.5.5@%%^^&*
:5.5.5.5:
**22.22.22.22
172.78.0.1()*5.4.3.277
3.3.3ki.3.
example 2 of file after delete all characters except numbers and "." charter , each new word will be in new line
192.0.22.1
0.1
32 5.500.5.5
1.1.1.1
1.1.1.1333
33331.1.1.1
5.5.5.
.
5.5.5.5
1.1.1.13444
54
192.9.30.174
5.5.5.5
5.5.5.5
5.5.5.5
22.22.22.22
172.78.0.1
5.4.3.277
3.3.3 .3.
linux sed awk perl
linux sed awk perl
edited Nov 15 '12 at 19:22
jw013
35.9k699125
35.9k699125
asked Nov 15 '12 at 12:54
yael
52261631
52261631
1
Your sample output is a bit inconsistent. Why are32
and5.500.5.5
both on line 3? Why is there no3
(from3de
) between the lines for5.5.5.
and.
? Why are3.3.3
and.3.
both on the last line?
– jw013
Nov 15 '12 at 19:15
2
This looks an awful lot like this question on ServerFault -- did you not get a good answer there?
– glenn jackman
Nov 15 '12 at 21:29
add a comment |
1
Your sample output is a bit inconsistent. Why are32
and5.500.5.5
both on line 3? Why is there no3
(from3de
) between the lines for5.5.5.
and.
? Why are3.3.3
and.3.
both on the last line?
– jw013
Nov 15 '12 at 19:15
2
This looks an awful lot like this question on ServerFault -- did you not get a good answer there?
– glenn jackman
Nov 15 '12 at 21:29
1
1
Your sample output is a bit inconsistent. Why are
32
and 5.500.5.5
both on line 3? Why is there no 3
(from 3de
) between the lines for 5.5.5.
and .
? Why are 3.3.3
and .3.
both on the last line?– jw013
Nov 15 '12 at 19:15
Your sample output is a bit inconsistent. Why are
32
and 5.500.5.5
both on line 3? Why is there no 3
(from 3de
) between the lines for 5.5.5.
and .
? Why are 3.3.3
and .3.
both on the last line?– jw013
Nov 15 '12 at 19:15
2
2
This looks an awful lot like this question on ServerFault -- did you not get a good answer there?
– glenn jackman
Nov 15 '12 at 21:29
This looks an awful lot like this question on ServerFault -- did you not get a good answer there?
– glenn jackman
Nov 15 '12 at 21:29
add a comment |
5 Answers
5
active
oldest
votes
up vote
4
down vote
accepted
This is a classic tr
use case, so the simplest way is:
tr -cs '[:digit:].' '[n*]' < input > output
The [:digit:].
argument specifies the characters to match (digits and dot). The [n*]
specifies the characters to replace with (replace everything with newline). The -c
option inverts the first argument since we want everything except digits and dot. The -s
squeezes consecutive newlines from the second string into one.
add a comment |
up vote
1
down vote
grep can do it:
grep -o '[0-9.]+'
add a comment |
up vote
0
down vote
You can use sed
to replace any unwanted character to a newline, and then grep
to get rid of empty lines:
sed 's/[^0-9.]/n/g' | grep .
Note that the result is different from the one you posted: 32 5.500.5.5
is split to two lines, as well as the last line.
Perl solution: it splits each line on unwanted characters, and greps for nonempty lines.
perl -ne 'print "$_n" for grep /./, split /[^0-9.]+/'
1
Note thatn
above in thesed
command example is not standard. The standard syntax to specify a newline character in the RHS of as
command is with a backslash followed by a new line character.
– Stéphane Chazelas
Nov 15 '12 at 19:39
add a comment |
up vote
0
down vote
Here's one way with GNU sed
:
sed ':a;{N;s/[^.0-9]+/n/g};ba' file
Here's how it works:
- Create a branch label;
- Append current/next line to register;
- Branch if not last line;
- Replace all groups that aren't matching with newlines.
Using the branch avoids spurious newlines.
add a comment |
up vote
0
down vote
on RHEL7:
replace any line starting with VALUETOEDIT
sed -i -e 's/^VALUETOEDIT.*/NEWVALUE/g' somefile
New contributor
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',
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%2f55812%2freplace-all-but-a-set-of-characters-in-a-file-with-newline%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
This is a classic tr
use case, so the simplest way is:
tr -cs '[:digit:].' '[n*]' < input > output
The [:digit:].
argument specifies the characters to match (digits and dot). The [n*]
specifies the characters to replace with (replace everything with newline). The -c
option inverts the first argument since we want everything except digits and dot. The -s
squeezes consecutive newlines from the second string into one.
add a comment |
up vote
4
down vote
accepted
This is a classic tr
use case, so the simplest way is:
tr -cs '[:digit:].' '[n*]' < input > output
The [:digit:].
argument specifies the characters to match (digits and dot). The [n*]
specifies the characters to replace with (replace everything with newline). The -c
option inverts the first argument since we want everything except digits and dot. The -s
squeezes consecutive newlines from the second string into one.
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
This is a classic tr
use case, so the simplest way is:
tr -cs '[:digit:].' '[n*]' < input > output
The [:digit:].
argument specifies the characters to match (digits and dot). The [n*]
specifies the characters to replace with (replace everything with newline). The -c
option inverts the first argument since we want everything except digits and dot. The -s
squeezes consecutive newlines from the second string into one.
This is a classic tr
use case, so the simplest way is:
tr -cs '[:digit:].' '[n*]' < input > output
The [:digit:].
argument specifies the characters to match (digits and dot). The [n*]
specifies the characters to replace with (replace everything with newline). The -c
option inverts the first argument since we want everything except digits and dot. The -s
squeezes consecutive newlines from the second string into one.
answered Nov 15 '12 at 19:19
jw013
35.9k699125
35.9k699125
add a comment |
add a comment |
up vote
1
down vote
grep can do it:
grep -o '[0-9.]+'
add a comment |
up vote
1
down vote
grep can do it:
grep -o '[0-9.]+'
add a comment |
up vote
1
down vote
up vote
1
down vote
grep can do it:
grep -o '[0-9.]+'
grep can do it:
grep -o '[0-9.]+'
answered Nov 15 '12 at 21:24
glenn jackman
50k569106
50k569106
add a comment |
add a comment |
up vote
0
down vote
You can use sed
to replace any unwanted character to a newline, and then grep
to get rid of empty lines:
sed 's/[^0-9.]/n/g' | grep .
Note that the result is different from the one you posted: 32 5.500.5.5
is split to two lines, as well as the last line.
Perl solution: it splits each line on unwanted characters, and greps for nonempty lines.
perl -ne 'print "$_n" for grep /./, split /[^0-9.]+/'
1
Note thatn
above in thesed
command example is not standard. The standard syntax to specify a newline character in the RHS of as
command is with a backslash followed by a new line character.
– Stéphane Chazelas
Nov 15 '12 at 19:39
add a comment |
up vote
0
down vote
You can use sed
to replace any unwanted character to a newline, and then grep
to get rid of empty lines:
sed 's/[^0-9.]/n/g' | grep .
Note that the result is different from the one you posted: 32 5.500.5.5
is split to two lines, as well as the last line.
Perl solution: it splits each line on unwanted characters, and greps for nonempty lines.
perl -ne 'print "$_n" for grep /./, split /[^0-9.]+/'
1
Note thatn
above in thesed
command example is not standard. The standard syntax to specify a newline character in the RHS of as
command is with a backslash followed by a new line character.
– Stéphane Chazelas
Nov 15 '12 at 19:39
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use sed
to replace any unwanted character to a newline, and then grep
to get rid of empty lines:
sed 's/[^0-9.]/n/g' | grep .
Note that the result is different from the one you posted: 32 5.500.5.5
is split to two lines, as well as the last line.
Perl solution: it splits each line on unwanted characters, and greps for nonempty lines.
perl -ne 'print "$_n" for grep /./, split /[^0-9.]+/'
You can use sed
to replace any unwanted character to a newline, and then grep
to get rid of empty lines:
sed 's/[^0-9.]/n/g' | grep .
Note that the result is different from the one you posted: 32 5.500.5.5
is split to two lines, as well as the last line.
Perl solution: it splits each line on unwanted characters, and greps for nonempty lines.
perl -ne 'print "$_n" for grep /./, split /[^0-9.]+/'
answered Nov 15 '12 at 13:05
choroba
26.2k44571
26.2k44571
1
Note thatn
above in thesed
command example is not standard. The standard syntax to specify a newline character in the RHS of as
command is with a backslash followed by a new line character.
– Stéphane Chazelas
Nov 15 '12 at 19:39
add a comment |
1
Note thatn
above in thesed
command example is not standard. The standard syntax to specify a newline character in the RHS of as
command is with a backslash followed by a new line character.
– Stéphane Chazelas
Nov 15 '12 at 19:39
1
1
Note that
n
above in the sed
command example is not standard. The standard syntax to specify a newline character in the RHS of a s
command is with a backslash followed by a new line character.– Stéphane Chazelas
Nov 15 '12 at 19:39
Note that
n
above in the sed
command example is not standard. The standard syntax to specify a newline character in the RHS of a s
command is with a backslash followed by a new line character.– Stéphane Chazelas
Nov 15 '12 at 19:39
add a comment |
up vote
0
down vote
Here's one way with GNU sed
:
sed ':a;{N;s/[^.0-9]+/n/g};ba' file
Here's how it works:
- Create a branch label;
- Append current/next line to register;
- Branch if not last line;
- Replace all groups that aren't matching with newlines.
Using the branch avoids spurious newlines.
add a comment |
up vote
0
down vote
Here's one way with GNU sed
:
sed ':a;{N;s/[^.0-9]+/n/g};ba' file
Here's how it works:
- Create a branch label;
- Append current/next line to register;
- Branch if not last line;
- Replace all groups that aren't matching with newlines.
Using the branch avoids spurious newlines.
add a comment |
up vote
0
down vote
up vote
0
down vote
Here's one way with GNU sed
:
sed ':a;{N;s/[^.0-9]+/n/g};ba' file
Here's how it works:
- Create a branch label;
- Append current/next line to register;
- Branch if not last line;
- Replace all groups that aren't matching with newlines.
Using the branch avoids spurious newlines.
Here's one way with GNU sed
:
sed ':a;{N;s/[^.0-9]+/n/g};ba' file
Here's how it works:
- Create a branch label;
- Append current/next line to register;
- Branch if not last line;
- Replace all groups that aren't matching with newlines.
Using the branch avoids spurious newlines.
edited Nov 15 '12 at 19:36
Stéphane Chazelas
297k54562908
297k54562908
answered Nov 15 '12 at 13:12
Chris Down
78.7k13188201
78.7k13188201
add a comment |
add a comment |
up vote
0
down vote
on RHEL7:
replace any line starting with VALUETOEDIT
sed -i -e 's/^VALUETOEDIT.*/NEWVALUE/g' somefile
New contributor
add a comment |
up vote
0
down vote
on RHEL7:
replace any line starting with VALUETOEDIT
sed -i -e 's/^VALUETOEDIT.*/NEWVALUE/g' somefile
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
on RHEL7:
replace any line starting with VALUETOEDIT
sed -i -e 's/^VALUETOEDIT.*/NEWVALUE/g' somefile
New contributor
on RHEL7:
replace any line starting with VALUETOEDIT
sed -i -e 's/^VALUETOEDIT.*/NEWVALUE/g' somefile
New contributor
edited yesterday
jimmij
30.6k869103
30.6k869103
New contributor
answered yesterday
surilin3
1
1
New contributor
New contributor
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%2f55812%2freplace-all-but-a-set-of-characters-in-a-file-with-newline%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
1
Your sample output is a bit inconsistent. Why are
32
and5.500.5.5
both on line 3? Why is there no3
(from3de
) between the lines for5.5.5.
and.
? Why are3.3.3
and.3.
both on the last line?– jw013
Nov 15 '12 at 19:15
2
This looks an awful lot like this question on ServerFault -- did you not get a good answer there?
– glenn jackman
Nov 15 '12 at 21:29