using sed command to read between sections in a given file The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsWhy the inconsistency with using cat vs. echo piped to this sed command?Replace words in file using sed commandRename file using sedRemove and replace between two specific strings using sed commandunix: replace one entire column in one file with a single value from another fileSimulate sed read command with awkBusybox ASH inside initrd has a problem when piping tee to sedsed - calling a variable from a file with multilineremove line if word start in lineshell script /unix commands for text file content editing
My body leaves; my core can stay
Can a flute soloist sit?
Separating matrix elements by lines
What information about me do stores get via my credit card?
Presidential Pardon
How do I design a circuit to convert a 100 mV and 50 Hz sine wave to a square wave?
Simulating Exploding Dice
Why are there uneven bright areas in this photo of black hole?
University's motivation for having tenure-track positions
What was the last x86 CPU that did not have the x87 floating-point unit built in?
Do working physicists consider Newtonian mechanics to be "falsified"?
Why can't wing-mounted spoilers be used to steepen approaches?
Is there a way to generate uniformly distributed points on a sphere from a fixed amount of random real numbers per point?
Is 'stolen' appropriate word?
Why are PDP-7-style microprogrammed instructions out of vogue?
should truth entail possible truth
"... to apply for a visa" or "... and applied for a visa"?
Can the DM override racial traits?
Homework question about an engine pulling a train
Is this wall load bearing? Blueprints and photos attached
Can we generate random numbers using irrational numbers like π and e?
why is the limit of this expression equal to 1?
US Healthcare consultation for visitors
Can each chord in a progression create its own key?
using sed command to read between sections in a given file
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election ResultsWhy the inconsistency with using cat vs. echo piped to this sed command?Replace words in file using sed commandRename file using sedRemove and replace between two specific strings using sed commandunix: replace one entire column in one file with a single value from another fileSimulate sed read command with awkBusybox ASH inside initrd has a problem when piping tee to sedsed - calling a variable from a file with multilineremove line if word start in lineshell script /unix commands for text file content editing
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
File content.
[kafka_properties]
listeners=PLAINTEXT://:KAFKA_CLIENT_PORT
default.replication.factor=2
ssl.client.auth=required
ssl.cipher.suites=["TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
"TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
"TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
"TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",]
ssl.enabled.protocols=TLSV1.2
ssl.secure.random.implem=SHA1PRNG
security.inter.broker.protocol=PLAINTEXT
security.protocol=SSL
ssl.endpoint.identification.algorithm=https
[kafka_ports]
KAFKA_CLIENT_PORT=9082
[zookeeper_properties]
clientPort=ZK_CLIENT_PORT
syncLimit=2
initLimit=5
tickTime=2000
autopurge.snapRetainCount=3
autopurge.purgeInterval=1
admin.serverPort=ZK_SERVER_ADMIN_PORT
I am trying to read the values from every section, e.g. [kafka_properties]
, or [kafka_ports]
using this command:
cat file.txt | sed -n '0,/kafka_properties/d;/[/,$d;/^$/d;p'
And write the values into a different file. It works okay if I don't add the parameter:
ssl.cipher.suites=["TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
"TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
"TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
"TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",]
but after adding the ssl.cipher.suites=
parameter to the file.txt
sed is not working as expected.
Where am I going wrong ?
text-processing sed
New contributor
add a comment |
File content.
[kafka_properties]
listeners=PLAINTEXT://:KAFKA_CLIENT_PORT
default.replication.factor=2
ssl.client.auth=required
ssl.cipher.suites=["TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
"TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
"TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
"TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",]
ssl.enabled.protocols=TLSV1.2
ssl.secure.random.implem=SHA1PRNG
security.inter.broker.protocol=PLAINTEXT
security.protocol=SSL
ssl.endpoint.identification.algorithm=https
[kafka_ports]
KAFKA_CLIENT_PORT=9082
[zookeeper_properties]
clientPort=ZK_CLIENT_PORT
syncLimit=2
initLimit=5
tickTime=2000
autopurge.snapRetainCount=3
autopurge.purgeInterval=1
admin.serverPort=ZK_SERVER_ADMIN_PORT
I am trying to read the values from every section, e.g. [kafka_properties]
, or [kafka_ports]
using this command:
cat file.txt | sed -n '0,/kafka_properties/d;/[/,$d;/^$/d;p'
And write the values into a different file. It works okay if I don't add the parameter:
ssl.cipher.suites=["TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
"TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
"TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
"TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",]
but after adding the ssl.cipher.suites=
parameter to the file.txt
sed is not working as expected.
Where am I going wrong ?
text-processing sed
New contributor
Please edit your question and explain how sed is "not working as expected". What output were you expecting and what output do you actually get?
– terdon♦
Apr 9 at 10:52
add a comment |
File content.
[kafka_properties]
listeners=PLAINTEXT://:KAFKA_CLIENT_PORT
default.replication.factor=2
ssl.client.auth=required
ssl.cipher.suites=["TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
"TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
"TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
"TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",]
ssl.enabled.protocols=TLSV1.2
ssl.secure.random.implem=SHA1PRNG
security.inter.broker.protocol=PLAINTEXT
security.protocol=SSL
ssl.endpoint.identification.algorithm=https
[kafka_ports]
KAFKA_CLIENT_PORT=9082
[zookeeper_properties]
clientPort=ZK_CLIENT_PORT
syncLimit=2
initLimit=5
tickTime=2000
autopurge.snapRetainCount=3
autopurge.purgeInterval=1
admin.serverPort=ZK_SERVER_ADMIN_PORT
I am trying to read the values from every section, e.g. [kafka_properties]
, or [kafka_ports]
using this command:
cat file.txt | sed -n '0,/kafka_properties/d;/[/,$d;/^$/d;p'
And write the values into a different file. It works okay if I don't add the parameter:
ssl.cipher.suites=["TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
"TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
"TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
"TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",]
but after adding the ssl.cipher.suites=
parameter to the file.txt
sed is not working as expected.
Where am I going wrong ?
text-processing sed
New contributor
File content.
[kafka_properties]
listeners=PLAINTEXT://:KAFKA_CLIENT_PORT
default.replication.factor=2
ssl.client.auth=required
ssl.cipher.suites=["TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
"TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
"TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
"TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",]
ssl.enabled.protocols=TLSV1.2
ssl.secure.random.implem=SHA1PRNG
security.inter.broker.protocol=PLAINTEXT
security.protocol=SSL
ssl.endpoint.identification.algorithm=https
[kafka_ports]
KAFKA_CLIENT_PORT=9082
[zookeeper_properties]
clientPort=ZK_CLIENT_PORT
syncLimit=2
initLimit=5
tickTime=2000
autopurge.snapRetainCount=3
autopurge.purgeInterval=1
admin.serverPort=ZK_SERVER_ADMIN_PORT
I am trying to read the values from every section, e.g. [kafka_properties]
, or [kafka_ports]
using this command:
cat file.txt | sed -n '0,/kafka_properties/d;/[/,$d;/^$/d;p'
And write the values into a different file. It works okay if I don't add the parameter:
ssl.cipher.suites=["TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
"TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
"TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
"TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",]
but after adding the ssl.cipher.suites=
parameter to the file.txt
sed is not working as expected.
Where am I going wrong ?
text-processing sed
text-processing sed
New contributor
New contributor
edited 12 hours ago
Rui F Ribeiro
42k1483142
42k1483142
New contributor
asked Apr 9 at 10:45
skssks
82
82
New contributor
New contributor
Please edit your question and explain how sed is "not working as expected". What output were you expecting and what output do you actually get?
– terdon♦
Apr 9 at 10:52
add a comment |
Please edit your question and explain how sed is "not working as expected". What output were you expecting and what output do you actually get?
– terdon♦
Apr 9 at 10:52
Please edit your question and explain how sed is "not working as expected". What output were you expecting and what output do you actually get?
– terdon♦
Apr 9 at 10:52
Please edit your question and explain how sed is "not working as expected". What output were you expecting and what output do you actually get?
– terdon♦
Apr 9 at 10:52
add a comment |
2 Answers
2
active
oldest
votes
Make [
only match at the beginning of the line with ^
:
sed -n '0,/kafka_properties/d;/^[/,$d;/^$/d;p' file.txt
Thanks a ton.. It worked
– sks
Apr 9 at 10:58
add a comment |
For the general case, writing each section to a different file without needing to know the section names, you could do:
awk '/^[/n=$1;gsub(/[][]/,"",n)print >> n".txt"' file
This will create these files from your example:
kafka_ports.txt kakfa_properties.txt zookeeper_properties.txt
Explanation
/^[/n=$1;gsub(/[][]/,"",n)
: if this line starts with a[
, save the 1st field as the variablen
and remove all[
or]
from it.print >> n".txt"
: append the curent line to the filen.txt
wheren
is the name of the section.
Note that this assumes you never have whitespace in a section name. If you do, try this instead:
awk '/^[/n=$0;gsub(/[][]/,"",n); gsub(/ /,"_",n)print >> n".txt"' file
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
);
);
sks 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%2funix.stackexchange.com%2fquestions%2f511429%2fusing-sed-command-to-read-between-sections-in-a-given-file%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
Make [
only match at the beginning of the line with ^
:
sed -n '0,/kafka_properties/d;/^[/,$d;/^$/d;p' file.txt
Thanks a ton.. It worked
– sks
Apr 9 at 10:58
add a comment |
Make [
only match at the beginning of the line with ^
:
sed -n '0,/kafka_properties/d;/^[/,$d;/^$/d;p' file.txt
Thanks a ton.. It worked
– sks
Apr 9 at 10:58
add a comment |
Make [
only match at the beginning of the line with ^
:
sed -n '0,/kafka_properties/d;/^[/,$d;/^$/d;p' file.txt
Make [
only match at the beginning of the line with ^
:
sed -n '0,/kafka_properties/d;/^[/,$d;/^$/d;p' file.txt
answered Apr 9 at 10:55
RoVoRoVo
3,761317
3,761317
Thanks a ton.. It worked
– sks
Apr 9 at 10:58
add a comment |
Thanks a ton.. It worked
– sks
Apr 9 at 10:58
Thanks a ton.. It worked
– sks
Apr 9 at 10:58
Thanks a ton.. It worked
– sks
Apr 9 at 10:58
add a comment |
For the general case, writing each section to a different file without needing to know the section names, you could do:
awk '/^[/n=$1;gsub(/[][]/,"",n)print >> n".txt"' file
This will create these files from your example:
kafka_ports.txt kakfa_properties.txt zookeeper_properties.txt
Explanation
/^[/n=$1;gsub(/[][]/,"",n)
: if this line starts with a[
, save the 1st field as the variablen
and remove all[
or]
from it.print >> n".txt"
: append the curent line to the filen.txt
wheren
is the name of the section.
Note that this assumes you never have whitespace in a section name. If you do, try this instead:
awk '/^[/n=$0;gsub(/[][]/,"",n); gsub(/ /,"_",n)print >> n".txt"' file
add a comment |
For the general case, writing each section to a different file without needing to know the section names, you could do:
awk '/^[/n=$1;gsub(/[][]/,"",n)print >> n".txt"' file
This will create these files from your example:
kafka_ports.txt kakfa_properties.txt zookeeper_properties.txt
Explanation
/^[/n=$1;gsub(/[][]/,"",n)
: if this line starts with a[
, save the 1st field as the variablen
and remove all[
or]
from it.print >> n".txt"
: append the curent line to the filen.txt
wheren
is the name of the section.
Note that this assumes you never have whitespace in a section name. If you do, try this instead:
awk '/^[/n=$0;gsub(/[][]/,"",n); gsub(/ /,"_",n)print >> n".txt"' file
add a comment |
For the general case, writing each section to a different file without needing to know the section names, you could do:
awk '/^[/n=$1;gsub(/[][]/,"",n)print >> n".txt"' file
This will create these files from your example:
kafka_ports.txt kakfa_properties.txt zookeeper_properties.txt
Explanation
/^[/n=$1;gsub(/[][]/,"",n)
: if this line starts with a[
, save the 1st field as the variablen
and remove all[
or]
from it.print >> n".txt"
: append the curent line to the filen.txt
wheren
is the name of the section.
Note that this assumes you never have whitespace in a section name. If you do, try this instead:
awk '/^[/n=$0;gsub(/[][]/,"",n); gsub(/ /,"_",n)print >> n".txt"' file
For the general case, writing each section to a different file without needing to know the section names, you could do:
awk '/^[/n=$1;gsub(/[][]/,"",n)print >> n".txt"' file
This will create these files from your example:
kafka_ports.txt kakfa_properties.txt zookeeper_properties.txt
Explanation
/^[/n=$1;gsub(/[][]/,"",n)
: if this line starts with a[
, save the 1st field as the variablen
and remove all[
or]
from it.print >> n".txt"
: append the curent line to the filen.txt
wheren
is the name of the section.
Note that this assumes you never have whitespace in a section name. If you do, try this instead:
awk '/^[/n=$0;gsub(/[][]/,"",n); gsub(/ /,"_",n)print >> n".txt"' file
answered Apr 9 at 11:17
terdon♦terdon
134k33270450
134k33270450
add a comment |
add a comment |
sks is a new contributor. Be nice, and check out our Code of Conduct.
sks is a new contributor. Be nice, and check out our Code of Conduct.
sks is a new contributor. Be nice, and check out our Code of Conduct.
sks is a new contributor. Be nice, and check out our Code of Conduct.
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.
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%2f511429%2fusing-sed-command-to-read-between-sections-in-a-given-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
Please edit your question and explain how sed is "not working as expected". What output were you expecting and what output do you actually get?
– terdon♦
Apr 9 at 10:52