How to find and replace using sed on records starting with Specific numberReplace non-printable characters in...
Improve quality of image bars
Does inertia keep a rotating object rotating forever, or something else?
How to have a continuous player experience in a setting that's likely to favor TPKs?
Wordplay subtraction paradox
Kepler space telescope undetected planets
Credit card details stolen every 1-2 years. What am I doing wrong?
Is it rude to refer to janitors as 'floor people'?
How many bits in the resultant hash will change, if the x bits are changed in its the original input?
Generating a PIN from cryptographic bytes
Can a Resident Assistant be told to ignore a lawful order?'
What is the point of a constraint expression on a non-templated function?
Operation Unz̖̬̜̺̬a͇͖̯͔͉l̟̭g͕̝̼͇͓̪͍o̬̝͍̹̻
How to honestly answer questions from a girlfriend like "How did you find this place" without giving the impression I'm always talking about my exes?
A scene of Jimmy diversity
Increasing muscle power without increasing volume
What were the problems on the Apollo 11 lunar module?
Does the Intel 8085 CPU use real memory addresses?
How can the electric potential be zero at a point where the electric field isn't, if that field can give a test charge kinetic energy?
What does it actually mean to have two time dimensions?
How Can I Process Untrusted Data Sources Securely?
How can a drink contain 1.8 kcal energy while 0 g fat/carbs/protein?
Why do so many pure math PhD students drop out or leave academia, compared to applied mathematics PhDs?
Did Voldemort kill his father before finding out about Horcruxes?
Wordplay addition paradox
How to find and replace using sed on records starting with Specific number
Replace non-printable characters in perl and sedSed Replace at a specific group of positionsHow to replace variables strings with special characters in sedsearch and Replace substring in mac address in a text fileReplace a pattern in File1 and replace it with corresponding matched pattern + column in File2using sed to replace a string in files which are in different foldersAdd a new line (n) in front of every forward arrow (>)Replace string in specific line at specific position of fixed-length fileStorage disk info - Replace multiple Input values to output fileReplace substring of characters with awk and sed
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I Have a fixed width file with the below format
012019-06-03070005000799111160300000030XXXXXXX0700000000030
012019-06-03070005000799165030700000030XXXXXXX0700000000030
012019-06-03070005000799175500700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000207000991755007000000300
022019-06-030007276384I06000000000000107000991755007000000300
012019-06-03070005000799175840700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000407000991758407000000300
022019-06-030007276384I06000000000000307000991758407000000300
012019-06-03070005000799194080700000030XXXXXXX0700000000030
012019-06-03070005000790035750700000030XXXXXXX0700000000030
012019-06-03070005000790036660700000030XXXXXXX0700000000030
i need work on the lines starting with 02 and have to replace the characters from 20th position to 50th position with "MMMM" preserving the spaces till the 50th position.
My output should look like this
i tried sed -Ee '/^02"s/((.20).{30}/1$(printf "%-30s" MMMM)/"'
, which replaces on the records starting with 01 and 02 both. but I want to work on records starting with 02.
linux shell-script sed
New contributor
add a comment |
I Have a fixed width file with the below format
012019-06-03070005000799111160300000030XXXXXXX0700000000030
012019-06-03070005000799165030700000030XXXXXXX0700000000030
012019-06-03070005000799175500700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000207000991755007000000300
022019-06-030007276384I06000000000000107000991755007000000300
012019-06-03070005000799175840700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000407000991758407000000300
022019-06-030007276384I06000000000000307000991758407000000300
012019-06-03070005000799194080700000030XXXXXXX0700000000030
012019-06-03070005000790035750700000030XXXXXXX0700000000030
012019-06-03070005000790036660700000030XXXXXXX0700000000030
i need work on the lines starting with 02 and have to replace the characters from 20th position to 50th position with "MMMM" preserving the spaces till the 50th position.
My output should look like this
i tried sed -Ee '/^02"s/((.20).{30}/1$(printf "%-30s" MMMM)/"'
, which replaces on the records starting with 01 and 02 both. but I want to work on records starting with 02.
linux shell-script sed
New contributor
the desired output should be cross-checked... It's not only doing for line starts with "02".
– msp9011
23 mins ago
add a comment |
I Have a fixed width file with the below format
012019-06-03070005000799111160300000030XXXXXXX0700000000030
012019-06-03070005000799165030700000030XXXXXXX0700000000030
012019-06-03070005000799175500700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000207000991755007000000300
022019-06-030007276384I06000000000000107000991755007000000300
012019-06-03070005000799175840700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000407000991758407000000300
022019-06-030007276384I06000000000000307000991758407000000300
012019-06-03070005000799194080700000030XXXXXXX0700000000030
012019-06-03070005000790035750700000030XXXXXXX0700000000030
012019-06-03070005000790036660700000030XXXXXXX0700000000030
i need work on the lines starting with 02 and have to replace the characters from 20th position to 50th position with "MMMM" preserving the spaces till the 50th position.
My output should look like this
i tried sed -Ee '/^02"s/((.20).{30}/1$(printf "%-30s" MMMM)/"'
, which replaces on the records starting with 01 and 02 both. but I want to work on records starting with 02.
linux shell-script sed
New contributor
I Have a fixed width file with the below format
012019-06-03070005000799111160300000030XXXXXXX0700000000030
012019-06-03070005000799165030700000030XXXXXXX0700000000030
012019-06-03070005000799175500700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000207000991755007000000300
022019-06-030007276384I06000000000000107000991755007000000300
012019-06-03070005000799175840700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000407000991758407000000300
022019-06-030007276384I06000000000000307000991758407000000300
012019-06-03070005000799194080700000030XXXXXXX0700000000030
012019-06-03070005000790035750700000030XXXXXXX0700000000030
012019-06-03070005000790036660700000030XXXXXXX0700000000030
i need work on the lines starting with 02 and have to replace the characters from 20th position to 50th position with "MMMM" preserving the spaces till the 50th position.
My output should look like this
i tried sed -Ee '/^02"s/((.20).{30}/1$(printf "%-30s" MMMM)/"'
, which replaces on the records starting with 01 and 02 both. but I want to work on records starting with 02.
linux shell-script sed
linux shell-script sed
New contributor
New contributor
edited 30 mins ago
msp9011
5,3695 gold badges42 silver badges69 bronze badges
5,3695 gold badges42 silver badges69 bronze badges
New contributor
asked 48 mins ago
ArunArun
11 bronze badge
11 bronze badge
New contributor
New contributor
the desired output should be cross-checked... It's not only doing for line starts with "02".
– msp9011
23 mins ago
add a comment |
the desired output should be cross-checked... It's not only doing for line starts with "02".
– msp9011
23 mins ago
the desired output should be cross-checked... It's not only doing for line starts with "02".
– msp9011
23 mins ago
the desired output should be cross-checked... It's not only doing for line starts with "02".
– msp9011
23 mins ago
add a comment |
1 Answer
1
active
oldest
votes
Try this,
Option 1: As per your desired output
sed "s/(.{21})(.{30})(.*)/1$(printf "%-30s" MMMM)3/" file
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
022019-06-03000727638MMMM 7000000300
022019-06-03000727638MMMM 7000000300
012019-06-03070005000MMMM 00000030
022019-06-03000727638MMMM 7000000300
022019-06-03000727638MMMM 7000000300
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
(.{21})
first 21 characters will be stored in1
(.{30})
next 30 charecters will be stored in2
(.{30})
remaining stored in3
- Then it will substitute 123 with 1$(printf "%-30s" MMMM)3
Option 2: As per your context
sed "/^02/ s/(.{19})(.{31})(.*)/1$(printf "%-30s" MMMM)3/" file
012019-06-03070005000799111160300000030XXXXXXX0700000000030
012019-06-03070005000799165030700000030XXXXXXX0700000000030
012019-06-03070005000799175500700000030XXXXXXX0700000000030
022019-06-030007276MMMM 07000000300
022019-06-030007276MMMM 07000000300
012019-06-03070005000799175840700000030XXXXXXX0700000000030
022019-06-030007276MMMM 07000000300
022019-06-030007276MMMM 07000000300
012019-06-03070005000799194080700000030XXXXXXX0700000000030
012019-06-03070005000790035750700000030XXXXXXX0700000000030
012019-06-03070005000790036660700000030XXXXXXX0700000000030
/^02/
will do the replacement only if the line starts with "02"
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
});
}
});
Arun 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%2f530771%2fhow-to-find-and-replace-using-sed-on-records-starting-with-specific-number%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
Try this,
Option 1: As per your desired output
sed "s/(.{21})(.{30})(.*)/1$(printf "%-30s" MMMM)3/" file
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
022019-06-03000727638MMMM 7000000300
022019-06-03000727638MMMM 7000000300
012019-06-03070005000MMMM 00000030
022019-06-03000727638MMMM 7000000300
022019-06-03000727638MMMM 7000000300
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
(.{21})
first 21 characters will be stored in1
(.{30})
next 30 charecters will be stored in2
(.{30})
remaining stored in3
- Then it will substitute 123 with 1$(printf "%-30s" MMMM)3
Option 2: As per your context
sed "/^02/ s/(.{19})(.{31})(.*)/1$(printf "%-30s" MMMM)3/" file
012019-06-03070005000799111160300000030XXXXXXX0700000000030
012019-06-03070005000799165030700000030XXXXXXX0700000000030
012019-06-03070005000799175500700000030XXXXXXX0700000000030
022019-06-030007276MMMM 07000000300
022019-06-030007276MMMM 07000000300
012019-06-03070005000799175840700000030XXXXXXX0700000000030
022019-06-030007276MMMM 07000000300
022019-06-030007276MMMM 07000000300
012019-06-03070005000799194080700000030XXXXXXX0700000000030
012019-06-03070005000790035750700000030XXXXXXX0700000000030
012019-06-03070005000790036660700000030XXXXXXX0700000000030
/^02/
will do the replacement only if the line starts with "02"
add a comment |
Try this,
Option 1: As per your desired output
sed "s/(.{21})(.{30})(.*)/1$(printf "%-30s" MMMM)3/" file
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
022019-06-03000727638MMMM 7000000300
022019-06-03000727638MMMM 7000000300
012019-06-03070005000MMMM 00000030
022019-06-03000727638MMMM 7000000300
022019-06-03000727638MMMM 7000000300
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
(.{21})
first 21 characters will be stored in1
(.{30})
next 30 charecters will be stored in2
(.{30})
remaining stored in3
- Then it will substitute 123 with 1$(printf "%-30s" MMMM)3
Option 2: As per your context
sed "/^02/ s/(.{19})(.{31})(.*)/1$(printf "%-30s" MMMM)3/" file
012019-06-03070005000799111160300000030XXXXXXX0700000000030
012019-06-03070005000799165030700000030XXXXXXX0700000000030
012019-06-03070005000799175500700000030XXXXXXX0700000000030
022019-06-030007276MMMM 07000000300
022019-06-030007276MMMM 07000000300
012019-06-03070005000799175840700000030XXXXXXX0700000000030
022019-06-030007276MMMM 07000000300
022019-06-030007276MMMM 07000000300
012019-06-03070005000799194080700000030XXXXXXX0700000000030
012019-06-03070005000790035750700000030XXXXXXX0700000000030
012019-06-03070005000790036660700000030XXXXXXX0700000000030
/^02/
will do the replacement only if the line starts with "02"
add a comment |
Try this,
Option 1: As per your desired output
sed "s/(.{21})(.{30})(.*)/1$(printf "%-30s" MMMM)3/" file
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
022019-06-03000727638MMMM 7000000300
022019-06-03000727638MMMM 7000000300
012019-06-03070005000MMMM 00000030
022019-06-03000727638MMMM 7000000300
022019-06-03000727638MMMM 7000000300
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
(.{21})
first 21 characters will be stored in1
(.{30})
next 30 charecters will be stored in2
(.{30})
remaining stored in3
- Then it will substitute 123 with 1$(printf "%-30s" MMMM)3
Option 2: As per your context
sed "/^02/ s/(.{19})(.{31})(.*)/1$(printf "%-30s" MMMM)3/" file
012019-06-03070005000799111160300000030XXXXXXX0700000000030
012019-06-03070005000799165030700000030XXXXXXX0700000000030
012019-06-03070005000799175500700000030XXXXXXX0700000000030
022019-06-030007276MMMM 07000000300
022019-06-030007276MMMM 07000000300
012019-06-03070005000799175840700000030XXXXXXX0700000000030
022019-06-030007276MMMM 07000000300
022019-06-030007276MMMM 07000000300
012019-06-03070005000799194080700000030XXXXXXX0700000000030
012019-06-03070005000790035750700000030XXXXXXX0700000000030
012019-06-03070005000790036660700000030XXXXXXX0700000000030
/^02/
will do the replacement only if the line starts with "02"
Try this,
Option 1: As per your desired output
sed "s/(.{21})(.{30})(.*)/1$(printf "%-30s" MMMM)3/" file
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
022019-06-03000727638MMMM 7000000300
022019-06-03000727638MMMM 7000000300
012019-06-03070005000MMMM 00000030
022019-06-03000727638MMMM 7000000300
022019-06-03000727638MMMM 7000000300
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
(.{21})
first 21 characters will be stored in1
(.{30})
next 30 charecters will be stored in2
(.{30})
remaining stored in3
- Then it will substitute 123 with 1$(printf "%-30s" MMMM)3
Option 2: As per your context
sed "/^02/ s/(.{19})(.{31})(.*)/1$(printf "%-30s" MMMM)3/" file
012019-06-03070005000799111160300000030XXXXXXX0700000000030
012019-06-03070005000799165030700000030XXXXXXX0700000000030
012019-06-03070005000799175500700000030XXXXXXX0700000000030
022019-06-030007276MMMM 07000000300
022019-06-030007276MMMM 07000000300
012019-06-03070005000799175840700000030XXXXXXX0700000000030
022019-06-030007276MMMM 07000000300
022019-06-030007276MMMM 07000000300
012019-06-03070005000799194080700000030XXXXXXX0700000000030
012019-06-03070005000790035750700000030XXXXXXX0700000000030
012019-06-03070005000790036660700000030XXXXXXX0700000000030
/^02/
will do the replacement only if the line starts with "02"
edited 2 mins ago
answered 10 mins ago
msp9011msp9011
5,3695 gold badges42 silver badges69 bronze badges
5,3695 gold badges42 silver badges69 bronze badges
add a comment |
add a comment |
Arun is a new contributor. Be nice, and check out our Code of Conduct.
Arun is a new contributor. Be nice, and check out our Code of Conduct.
Arun is a new contributor. Be nice, and check out our Code of Conduct.
Arun 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%2f530771%2fhow-to-find-and-replace-using-sed-on-records-starting-with-specific-number%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
the desired output should be cross-checked... It's not only doing for line starts with "02".
– msp9011
23 mins ago