Replace string in specific line at specific position of fixed-length fileJoining two files with unique...
Is there an application which does HTTP PUT?
Mindfulness of Watching Youtube
Is it a good idea to copy a trader when investing?
Two (probably) equal real numbers which are not proved to be equal?
Why does this pattern in powers happen?
Exactly which act of bravery are Luke and Han awarded a medal for?
Is it possible to do moon sighting in advance for 5 years with 100% accuracy?
My perfect evil overlord plan... or is it?
Using mean length and mean weight to calculate mean BMI?
Why is it wrong to *implement* myself a known, published, widely believed to be secure crypto algorithm?
"I can't place her": How do Russian speakers express this idea colloquially?
Are there vaccine ingredients which may not be disclosed ("hidden", "trade secret", or similar)?
Gift for mentor after his thesis defense?
How to adjust Venn Diagram for A^c and A - B
Opposite party turned away from voting when ballot is all opposing party
Creating Stored Procedure in local db that references tables in linked server
Illegal assignment from Id to List
Why is the episode called "The Last of the Starks"?
How is it believable that Euron could so easily pull off this ambush?
Are wands in any sort of book going to be too much like Harry Potter?
GLM: Modelling proportional data - account for variation in total sample size
Whose birthyears are canonically established in the MCU?
As a small race with a heavy weapon, does enlage remove the disadvantage?
How to animate petals opening
Replace string in specific line at specific position of fixed-length file
Joining two files with unique identifierFormat output to a specific line lengthReplace or append a line with string within a file/usr/bin/awk: Argument list too longReplace a string with “string|string”replace a string 6 line after matching stringChange the position of string in lineFinding a specific character at one to many positions in a fileHow to delete line if pattern is at specific positionpass arguments to the date command in LHS of sed
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have a fixed-length data file where records are tagged (first 6 characters have a certain string that identifies the record) and I need to replace text from a specific line and a specific position on that line.
HEADER 123456
SHIPTO CODE 123 LANE HOUSTON TX
ITEM ACME BRICK
For example, I would like to replace the ship-to code in the above data. The data can be found in positions 10-17 of the ship-to record (line starts with "SHIPTO"). I'd like to use sed or awk (or other commands) but update the data with a single command line. If there is more than one SHIPTO line, I am ok with updating them all.
text-processing awk sed
New contributor
anterys is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I have a fixed-length data file where records are tagged (first 6 characters have a certain string that identifies the record) and I need to replace text from a specific line and a specific position on that line.
HEADER 123456
SHIPTO CODE 123 LANE HOUSTON TX
ITEM ACME BRICK
For example, I would like to replace the ship-to code in the above data. The data can be found in positions 10-17 of the ship-to record (line starts with "SHIPTO"). I'd like to use sed or awk (or other commands) but update the data with a single command line. If there is more than one SHIPTO line, I am ok with updating them all.
text-processing awk sed
New contributor
anterys is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
(1) I’m not sure what you mean by a “fixed-length file”. (2) Positions 10-17 of the ship-to record areCODE. Is that really what you mean?
– Scott
3 hours ago
add a comment |
I have a fixed-length data file where records are tagged (first 6 characters have a certain string that identifies the record) and I need to replace text from a specific line and a specific position on that line.
HEADER 123456
SHIPTO CODE 123 LANE HOUSTON TX
ITEM ACME BRICK
For example, I would like to replace the ship-to code in the above data. The data can be found in positions 10-17 of the ship-to record (line starts with "SHIPTO"). I'd like to use sed or awk (or other commands) but update the data with a single command line. If there is more than one SHIPTO line, I am ok with updating them all.
text-processing awk sed
New contributor
anterys is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have a fixed-length data file where records are tagged (first 6 characters have a certain string that identifies the record) and I need to replace text from a specific line and a specific position on that line.
HEADER 123456
SHIPTO CODE 123 LANE HOUSTON TX
ITEM ACME BRICK
For example, I would like to replace the ship-to code in the above data. The data can be found in positions 10-17 of the ship-to record (line starts with "SHIPTO"). I'd like to use sed or awk (or other commands) but update the data with a single command line. If there is more than one SHIPTO line, I am ok with updating them all.
text-processing awk sed
text-processing awk sed
New contributor
anterys is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
anterys is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 3 hours ago
Jeff Schaller♦
45.5k1165148
45.5k1165148
New contributor
anterys is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 3 hours ago
anterysanterys
1
1
New contributor
anterys is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
anterys is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
(1) I’m not sure what you mean by a “fixed-length file”. (2) Positions 10-17 of the ship-to record areCODE. Is that really what you mean?
– Scott
3 hours ago
add a comment |
(1) I’m not sure what you mean by a “fixed-length file”. (2) Positions 10-17 of the ship-to record areCODE. Is that really what you mean?
– Scott
3 hours ago
(1) I’m not sure what you mean by a “fixed-length file”. (2) Positions 10-17 of the ship-to record are
CODE . Is that really what you mean?– Scott
3 hours ago
(1) I’m not sure what you mean by a “fixed-length file”. (2) Positions 10-17 of the ship-to record are
CODE . Is that really what you mean?– Scott
3 hours ago
add a comment |
2 Answers
2
active
oldest
votes
Given the input file:
HEADER 123456
SHIPTO CODE 123 LANE HOUSTON TX
ITEM ACME BRICK
And the desire to change the field after 'CODE' where the first field is 'SHIPTO':
$ awk 'BEGIN { OFS = "t" } $1 != "SHIPTO" { print } $1 == "SHIPTO" { $3="0000"; print }' input
HEADER 123456
SHIPTO CODE 0000 LANE HOUSTON TX
ITEM ACME BRICK
If your fields are fixed-length and space-delimited and not tab delimited you can use formatted print strings (e. g. printf( "%20st" "$1" )).
add a comment |
Using sed:
$ sed -E '/^SHIPTO/ s/(.{9}).{8}/1NuValue /' file
HEADER 123456
SHIPTO NuValue 123 LANE HOUSTON TX
ITEM ACME BRICK
How it works:
/^SHIPTO/
This selects lines that start with
SHIPTO. The substitute command which follows will only be applied to these lines.
s/(.{9}).{8}/1NuValue /
This matches the first 9+8 characters of the line and saves the first 9 characters of the line in group 1. These characters are replaced by group 1,
1, and your new value (which, to keep the format, should be 8 characters.
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
});
}
});
anterys 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%2f517659%2freplace-string-in-specific-line-at-specific-position-of-fixed-length-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
Given the input file:
HEADER 123456
SHIPTO CODE 123 LANE HOUSTON TX
ITEM ACME BRICK
And the desire to change the field after 'CODE' where the first field is 'SHIPTO':
$ awk 'BEGIN { OFS = "t" } $1 != "SHIPTO" { print } $1 == "SHIPTO" { $3="0000"; print }' input
HEADER 123456
SHIPTO CODE 0000 LANE HOUSTON TX
ITEM ACME BRICK
If your fields are fixed-length and space-delimited and not tab delimited you can use formatted print strings (e. g. printf( "%20st" "$1" )).
add a comment |
Given the input file:
HEADER 123456
SHIPTO CODE 123 LANE HOUSTON TX
ITEM ACME BRICK
And the desire to change the field after 'CODE' where the first field is 'SHIPTO':
$ awk 'BEGIN { OFS = "t" } $1 != "SHIPTO" { print } $1 == "SHIPTO" { $3="0000"; print }' input
HEADER 123456
SHIPTO CODE 0000 LANE HOUSTON TX
ITEM ACME BRICK
If your fields are fixed-length and space-delimited and not tab delimited you can use formatted print strings (e. g. printf( "%20st" "$1" )).
add a comment |
Given the input file:
HEADER 123456
SHIPTO CODE 123 LANE HOUSTON TX
ITEM ACME BRICK
And the desire to change the field after 'CODE' where the first field is 'SHIPTO':
$ awk 'BEGIN { OFS = "t" } $1 != "SHIPTO" { print } $1 == "SHIPTO" { $3="0000"; print }' input
HEADER 123456
SHIPTO CODE 0000 LANE HOUSTON TX
ITEM ACME BRICK
If your fields are fixed-length and space-delimited and not tab delimited you can use formatted print strings (e. g. printf( "%20st" "$1" )).
Given the input file:
HEADER 123456
SHIPTO CODE 123 LANE HOUSTON TX
ITEM ACME BRICK
And the desire to change the field after 'CODE' where the first field is 'SHIPTO':
$ awk 'BEGIN { OFS = "t" } $1 != "SHIPTO" { print } $1 == "SHIPTO" { $3="0000"; print }' input
HEADER 123456
SHIPTO CODE 0000 LANE HOUSTON TX
ITEM ACME BRICK
If your fields are fixed-length and space-delimited and not tab delimited you can use formatted print strings (e. g. printf( "%20st" "$1" )).
answered 3 hours ago
DopeGhotiDopeGhoti
47.7k56194
47.7k56194
add a comment |
add a comment |
Using sed:
$ sed -E '/^SHIPTO/ s/(.{9}).{8}/1NuValue /' file
HEADER 123456
SHIPTO NuValue 123 LANE HOUSTON TX
ITEM ACME BRICK
How it works:
/^SHIPTO/
This selects lines that start with
SHIPTO. The substitute command which follows will only be applied to these lines.
s/(.{9}).{8}/1NuValue /
This matches the first 9+8 characters of the line and saves the first 9 characters of the line in group 1. These characters are replaced by group 1,
1, and your new value (which, to keep the format, should be 8 characters.
add a comment |
Using sed:
$ sed -E '/^SHIPTO/ s/(.{9}).{8}/1NuValue /' file
HEADER 123456
SHIPTO NuValue 123 LANE HOUSTON TX
ITEM ACME BRICK
How it works:
/^SHIPTO/
This selects lines that start with
SHIPTO. The substitute command which follows will only be applied to these lines.
s/(.{9}).{8}/1NuValue /
This matches the first 9+8 characters of the line and saves the first 9 characters of the line in group 1. These characters are replaced by group 1,
1, and your new value (which, to keep the format, should be 8 characters.
add a comment |
Using sed:
$ sed -E '/^SHIPTO/ s/(.{9}).{8}/1NuValue /' file
HEADER 123456
SHIPTO NuValue 123 LANE HOUSTON TX
ITEM ACME BRICK
How it works:
/^SHIPTO/
This selects lines that start with
SHIPTO. The substitute command which follows will only be applied to these lines.
s/(.{9}).{8}/1NuValue /
This matches the first 9+8 characters of the line and saves the first 9 characters of the line in group 1. These characters are replaced by group 1,
1, and your new value (which, to keep the format, should be 8 characters.
Using sed:
$ sed -E '/^SHIPTO/ s/(.{9}).{8}/1NuValue /' file
HEADER 123456
SHIPTO NuValue 123 LANE HOUSTON TX
ITEM ACME BRICK
How it works:
/^SHIPTO/
This selects lines that start with
SHIPTO. The substitute command which follows will only be applied to these lines.
s/(.{9}).{8}/1NuValue /
This matches the first 9+8 characters of the line and saves the first 9 characters of the line in group 1. These characters are replaced by group 1,
1, and your new value (which, to keep the format, should be 8 characters.
answered 3 hours ago
John1024John1024
49.1k5114129
49.1k5114129
add a comment |
add a comment |
anterys is a new contributor. Be nice, and check out our Code of Conduct.
anterys is a new contributor. Be nice, and check out our Code of Conduct.
anterys is a new contributor. Be nice, and check out our Code of Conduct.
anterys 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%2f517659%2freplace-string-in-specific-line-at-specific-position-of-fixed-length-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
(1) I’m not sure what you mean by a “fixed-length file”. (2) Positions 10-17 of the ship-to record are
CODE. Is that really what you mean?– Scott
3 hours ago