Finding and replacing text inside awk block
I want to find and replace a value inside awk block.
Here is a sample input file to my script
P1
10,9:11/18013013582
,10:1
,167:0
,487:5/E.164
11,9:15/310410532169026
,10:60
,167:0
,487:4/IMSI
12,18:15/013329002130500
,19:0
15,9:10/9609610015
,10:1
,14:68
,167:0
,436:5/ABCDE
,487:5/E.164
16,87:1
17,9:10/8013765024
,10:1
,11:1
,12:0
,167:0
,487:5/E.164
23,9:11/13123149810
,10:1
,11:1
,167:0
,487:5/E.164
P3
42,3:1.
,4:3
,300:1.
43,3:1.
,4:3
,300:1.
44,3:0.
,4:7
,300:0.
45,5:0.3
And here is the piece of code that I am currently using,
nawk -v fname="${filename}" -F '/|:' '
function isnum(x){return(x==x+0)}
/P1/,/P3/{
# Found start increment i reset variables go to next line
if(/P1/){
++i
fid =""
count++
next
}
# Found end validate variable and print go to next line
if(/P3/){
printf "%s|",count
printf "%s|",isnum(fid)?fid:"NULL"
next
}
if(!fid && /36,59:*/)
{
fid = $NF
}
' ${filename} >>output.txt
Basically, I my input file will contain several P1/P3 blocks, I am taking one block at a time and finding out value from it. Now, what I want to basically do is change the value of first ,11:1 (i.e the one after 17,9 part) into 11:2 based on the value of 17,9:10/8013765024.
Please note there can be several ,11 before and after but those have to remain unchanged.
Please suggest how to proceed further. I am very new to both awk and sed.
Also, I tried writing a regular expression but could not figure out it properly. Here it is,
17[,0-9:]*[n]*,11
text-processing awk regular-expression
migrated from stackoverflow.com Jul 24 '14 at 1:57
This question came from our site for professional and enthusiast programmers.
add a comment |
I want to find and replace a value inside awk block.
Here is a sample input file to my script
P1
10,9:11/18013013582
,10:1
,167:0
,487:5/E.164
11,9:15/310410532169026
,10:60
,167:0
,487:4/IMSI
12,18:15/013329002130500
,19:0
15,9:10/9609610015
,10:1
,14:68
,167:0
,436:5/ABCDE
,487:5/E.164
16,87:1
17,9:10/8013765024
,10:1
,11:1
,12:0
,167:0
,487:5/E.164
23,9:11/13123149810
,10:1
,11:1
,167:0
,487:5/E.164
P3
42,3:1.
,4:3
,300:1.
43,3:1.
,4:3
,300:1.
44,3:0.
,4:7
,300:0.
45,5:0.3
And here is the piece of code that I am currently using,
nawk -v fname="${filename}" -F '/|:' '
function isnum(x){return(x==x+0)}
/P1/,/P3/{
# Found start increment i reset variables go to next line
if(/P1/){
++i
fid =""
count++
next
}
# Found end validate variable and print go to next line
if(/P3/){
printf "%s|",count
printf "%s|",isnum(fid)?fid:"NULL"
next
}
if(!fid && /36,59:*/)
{
fid = $NF
}
' ${filename} >>output.txt
Basically, I my input file will contain several P1/P3 blocks, I am taking one block at a time and finding out value from it. Now, what I want to basically do is change the value of first ,11:1 (i.e the one after 17,9 part) into 11:2 based on the value of 17,9:10/8013765024.
Please note there can be several ,11 before and after but those have to remain unchanged.
Please suggest how to proceed further. I am very new to both awk and sed.
Also, I tried writing a regular expression but could not figure out it properly. Here it is,
17[,0-9:]*[n]*,11
text-processing awk regular-expression
migrated from stackoverflow.com Jul 24 '14 at 1:57
This question came from our site for professional and enthusiast programmers.
you want to change the value which was present just after to17,9:10/8013765024
– Avinash Raj
Jun 18 '14 at 4:42
You want to prune this down to a really minimal example and explain how 2 is "based on" 8013765024. Is that a magic number we should be looking for, or a threshold value, or what are the conditions under which the first ,11:1 should be replaced?
– tripleee
Jun 18 '14 at 5:01
You cannot apply a regular expression across lines. You will need something like/^17[,0-9:]*$/ { found=1 } found&/^,11/ { do things; found=0 }
– tripleee
Jun 18 '14 at 5:04
Your explanation is gibberish. Try rewording it.
– ooga
Jun 18 '14 at 5:07
Sorry for being unclear. 17,9:10/8013765024 ,10:1 ,11:1 see, if value after 17,9:10/ i.e. 8013765024 starts with 80 then I want to change this to +118013765024 and also the next ,11:1 value to ,11:2. I hope I am clear this time. Please let me know if I am not.
– Garvit Khandelwal
Jun 18 '14 at 5:40
add a comment |
I want to find and replace a value inside awk block.
Here is a sample input file to my script
P1
10,9:11/18013013582
,10:1
,167:0
,487:5/E.164
11,9:15/310410532169026
,10:60
,167:0
,487:4/IMSI
12,18:15/013329002130500
,19:0
15,9:10/9609610015
,10:1
,14:68
,167:0
,436:5/ABCDE
,487:5/E.164
16,87:1
17,9:10/8013765024
,10:1
,11:1
,12:0
,167:0
,487:5/E.164
23,9:11/13123149810
,10:1
,11:1
,167:0
,487:5/E.164
P3
42,3:1.
,4:3
,300:1.
43,3:1.
,4:3
,300:1.
44,3:0.
,4:7
,300:0.
45,5:0.3
And here is the piece of code that I am currently using,
nawk -v fname="${filename}" -F '/|:' '
function isnum(x){return(x==x+0)}
/P1/,/P3/{
# Found start increment i reset variables go to next line
if(/P1/){
++i
fid =""
count++
next
}
# Found end validate variable and print go to next line
if(/P3/){
printf "%s|",count
printf "%s|",isnum(fid)?fid:"NULL"
next
}
if(!fid && /36,59:*/)
{
fid = $NF
}
' ${filename} >>output.txt
Basically, I my input file will contain several P1/P3 blocks, I am taking one block at a time and finding out value from it. Now, what I want to basically do is change the value of first ,11:1 (i.e the one after 17,9 part) into 11:2 based on the value of 17,9:10/8013765024.
Please note there can be several ,11 before and after but those have to remain unchanged.
Please suggest how to proceed further. I am very new to both awk and sed.
Also, I tried writing a regular expression but could not figure out it properly. Here it is,
17[,0-9:]*[n]*,11
text-processing awk regular-expression
I want to find and replace a value inside awk block.
Here is a sample input file to my script
P1
10,9:11/18013013582
,10:1
,167:0
,487:5/E.164
11,9:15/310410532169026
,10:60
,167:0
,487:4/IMSI
12,18:15/013329002130500
,19:0
15,9:10/9609610015
,10:1
,14:68
,167:0
,436:5/ABCDE
,487:5/E.164
16,87:1
17,9:10/8013765024
,10:1
,11:1
,12:0
,167:0
,487:5/E.164
23,9:11/13123149810
,10:1
,11:1
,167:0
,487:5/E.164
P3
42,3:1.
,4:3
,300:1.
43,3:1.
,4:3
,300:1.
44,3:0.
,4:7
,300:0.
45,5:0.3
And here is the piece of code that I am currently using,
nawk -v fname="${filename}" -F '/|:' '
function isnum(x){return(x==x+0)}
/P1/,/P3/{
# Found start increment i reset variables go to next line
if(/P1/){
++i
fid =""
count++
next
}
# Found end validate variable and print go to next line
if(/P3/){
printf "%s|",count
printf "%s|",isnum(fid)?fid:"NULL"
next
}
if(!fid && /36,59:*/)
{
fid = $NF
}
' ${filename} >>output.txt
Basically, I my input file will contain several P1/P3 blocks, I am taking one block at a time and finding out value from it. Now, what I want to basically do is change the value of first ,11:1 (i.e the one after 17,9 part) into 11:2 based on the value of 17,9:10/8013765024.
Please note there can be several ,11 before and after but those have to remain unchanged.
Please suggest how to proceed further. I am very new to both awk and sed.
Also, I tried writing a regular expression but could not figure out it properly. Here it is,
17[,0-9:]*[n]*,11
text-processing awk regular-expression
text-processing awk regular-expression
edited 1 hour ago
Jeff Schaller
40.1k1054126
40.1k1054126
asked Jun 18 '14 at 4:37
Garvit KhandelwalGarvit Khandelwal
162
162
migrated from stackoverflow.com Jul 24 '14 at 1:57
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com Jul 24 '14 at 1:57
This question came from our site for professional and enthusiast programmers.
you want to change the value which was present just after to17,9:10/8013765024
– Avinash Raj
Jun 18 '14 at 4:42
You want to prune this down to a really minimal example and explain how 2 is "based on" 8013765024. Is that a magic number we should be looking for, or a threshold value, or what are the conditions under which the first ,11:1 should be replaced?
– tripleee
Jun 18 '14 at 5:01
You cannot apply a regular expression across lines. You will need something like/^17[,0-9:]*$/ { found=1 } found&/^,11/ { do things; found=0 }
– tripleee
Jun 18 '14 at 5:04
Your explanation is gibberish. Try rewording it.
– ooga
Jun 18 '14 at 5:07
Sorry for being unclear. 17,9:10/8013765024 ,10:1 ,11:1 see, if value after 17,9:10/ i.e. 8013765024 starts with 80 then I want to change this to +118013765024 and also the next ,11:1 value to ,11:2. I hope I am clear this time. Please let me know if I am not.
– Garvit Khandelwal
Jun 18 '14 at 5:40
add a comment |
you want to change the value which was present just after to17,9:10/8013765024
– Avinash Raj
Jun 18 '14 at 4:42
You want to prune this down to a really minimal example and explain how 2 is "based on" 8013765024. Is that a magic number we should be looking for, or a threshold value, or what are the conditions under which the first ,11:1 should be replaced?
– tripleee
Jun 18 '14 at 5:01
You cannot apply a regular expression across lines. You will need something like/^17[,0-9:]*$/ { found=1 } found&/^,11/ { do things; found=0 }
– tripleee
Jun 18 '14 at 5:04
Your explanation is gibberish. Try rewording it.
– ooga
Jun 18 '14 at 5:07
Sorry for being unclear. 17,9:10/8013765024 ,10:1 ,11:1 see, if value after 17,9:10/ i.e. 8013765024 starts with 80 then I want to change this to +118013765024 and also the next ,11:1 value to ,11:2. I hope I am clear this time. Please let me know if I am not.
– Garvit Khandelwal
Jun 18 '14 at 5:40
you want to change the value which was present just after to
17,9:10/8013765024
– Avinash Raj
Jun 18 '14 at 4:42
you want to change the value which was present just after to
17,9:10/8013765024
– Avinash Raj
Jun 18 '14 at 4:42
You want to prune this down to a really minimal example and explain how 2 is "based on" 8013765024. Is that a magic number we should be looking for, or a threshold value, or what are the conditions under which the first ,11:1 should be replaced?
– tripleee
Jun 18 '14 at 5:01
You want to prune this down to a really minimal example and explain how 2 is "based on" 8013765024. Is that a magic number we should be looking for, or a threshold value, or what are the conditions under which the first ,11:1 should be replaced?
– tripleee
Jun 18 '14 at 5:01
You cannot apply a regular expression across lines. You will need something like
/^17[,0-9:]*$/ { found=1 } found&/^,11/ { do things; found=0 }
– tripleee
Jun 18 '14 at 5:04
You cannot apply a regular expression across lines. You will need something like
/^17[,0-9:]*$/ { found=1 } found&/^,11/ { do things; found=0 }
– tripleee
Jun 18 '14 at 5:04
Your explanation is gibberish. Try rewording it.
– ooga
Jun 18 '14 at 5:07
Your explanation is gibberish. Try rewording it.
– ooga
Jun 18 '14 at 5:07
Sorry for being unclear. 17,9:10/8013765024 ,10:1 ,11:1 see, if value after 17,9:10/ i.e. 8013765024 starts with 80 then I want to change this to +118013765024 and also the next ,11:1 value to ,11:2. I hope I am clear this time. Please let me know if I am not.
– Garvit Khandelwal
Jun 18 '14 at 5:40
Sorry for being unclear. 17,9:10/8013765024 ,10:1 ,11:1 see, if value after 17,9:10/ i.e. 8013765024 starts with 80 then I want to change this to +118013765024 and also the next ,11:1 value to ,11:2. I hope I am clear this time. Please let me know if I am not.
– Garvit Khandelwal
Jun 18 '14 at 5:40
add a comment |
3 Answers
3
active
oldest
votes
Give a try to this awk command. Whenevre the awk find a number starting with 80
on column2, it gets the next line and then the next line and change it's value to ,11:2
print the other lines as it is.
$ awk -F/ '$2~/^80/{print; getline; print; getline; $1=",11.2"}1' file1.txt
P1
10,9:11/18013013582
,10:1
,167:0
,487:5/E.164
11,9:15/310410532169026
,10:60
,167:0
,487:4/IMSI
12,18:15/013329002130500
,19:0
15,9:10/9609610015
,10:1
,14:68
,167:0
,436:5/ABCDE
,487:5/E.164
16,87:1
17,9:10/8013765024
,10:1
,11.2
,12:0
,167:0
,487:5/E.164
23,9:11/13123149810
,10:1
,11:1
,167:0
,487:5/E.164
P3
42,3:1.
,4:3
,300:1.
43,3:1.
,4:3
,300:1.
44,3:0.
,4:7
,300:0.
45,5:0.3
Avinash I dont want to hard code the value 8013765024. I should be able to read the first two letters of 8013765024 and based on it I should be able to decide whether to change from ,11:1 to ,11:2. If first 2 digits are 80 then only i should change ,11:1 to ,11:2. To add to this I want 8013765024 also to be appended with +11 (+118013765024)
– Garvit Khandelwal
Jun 18 '14 at 6:11
You should avoid usinggetline
read more here: awk.freeshell.org/AllAboutGetline
– Jotne
Jun 18 '14 at 6:12
@Jotne Please stop offering arbitrary "advice" about what not to do. It's (possibly) reasonable to use getline here. Of course, it's still very unclear what the OP actually wants, but getline is not the problem.
– ooga
Jun 18 '14 at 14:43
@AvinashRaj I don't see what the purpose of yourf
variable is. You set it, but never actually use it.
– ooga
Jun 18 '14 at 14:44
yes i set it to change the value of the second line after17,9:10/8013765024
– Avinash Raj
Jun 18 '14 at 14:49
|
show 7 more comments
Here is an awk
solution:
$ awk -F '/|:' '
$3 == "8013765024" {flag = 1}
$0 == ",11:1" && flag {$2 = 2;flag = 0}
1
' OFS=':' file
add a comment |
Here's a possible sed
solution:
sed '/17,9:10/80/,/11:1/ {
s/8013765024/+118013765024/
s/11:1/11:2/ }' file.txt
This starts the substitution after it finds 17,9:10/80
and first appends +11
to 8013765024
making it +118013765024
and it also replaces 11:1
with 11:2
for only the first occurrence of 11:1
(the range is inclusive)
Running diff
on the output of the sed
and the initial file shows:
19c19
< 17,9:10/+118013765024
---
> 17,9:10/8013765024
21c21
< ,11:2
---
> ,11:1
meaning that the only changes to the file are the addition of [+11
] and the substitution of [11:1
] to [11:2
]. Let me know if this correct.
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
});
}
});
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%2f146248%2ffinding-and-replacing-text-inside-awk-block%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Give a try to this awk command. Whenevre the awk find a number starting with 80
on column2, it gets the next line and then the next line and change it's value to ,11:2
print the other lines as it is.
$ awk -F/ '$2~/^80/{print; getline; print; getline; $1=",11.2"}1' file1.txt
P1
10,9:11/18013013582
,10:1
,167:0
,487:5/E.164
11,9:15/310410532169026
,10:60
,167:0
,487:4/IMSI
12,18:15/013329002130500
,19:0
15,9:10/9609610015
,10:1
,14:68
,167:0
,436:5/ABCDE
,487:5/E.164
16,87:1
17,9:10/8013765024
,10:1
,11.2
,12:0
,167:0
,487:5/E.164
23,9:11/13123149810
,10:1
,11:1
,167:0
,487:5/E.164
P3
42,3:1.
,4:3
,300:1.
43,3:1.
,4:3
,300:1.
44,3:0.
,4:7
,300:0.
45,5:0.3
Avinash I dont want to hard code the value 8013765024. I should be able to read the first two letters of 8013765024 and based on it I should be able to decide whether to change from ,11:1 to ,11:2. If first 2 digits are 80 then only i should change ,11:1 to ,11:2. To add to this I want 8013765024 also to be appended with +11 (+118013765024)
– Garvit Khandelwal
Jun 18 '14 at 6:11
You should avoid usinggetline
read more here: awk.freeshell.org/AllAboutGetline
– Jotne
Jun 18 '14 at 6:12
@Jotne Please stop offering arbitrary "advice" about what not to do. It's (possibly) reasonable to use getline here. Of course, it's still very unclear what the OP actually wants, but getline is not the problem.
– ooga
Jun 18 '14 at 14:43
@AvinashRaj I don't see what the purpose of yourf
variable is. You set it, but never actually use it.
– ooga
Jun 18 '14 at 14:44
yes i set it to change the value of the second line after17,9:10/8013765024
– Avinash Raj
Jun 18 '14 at 14:49
|
show 7 more comments
Give a try to this awk command. Whenevre the awk find a number starting with 80
on column2, it gets the next line and then the next line and change it's value to ,11:2
print the other lines as it is.
$ awk -F/ '$2~/^80/{print; getline; print; getline; $1=",11.2"}1' file1.txt
P1
10,9:11/18013013582
,10:1
,167:0
,487:5/E.164
11,9:15/310410532169026
,10:60
,167:0
,487:4/IMSI
12,18:15/013329002130500
,19:0
15,9:10/9609610015
,10:1
,14:68
,167:0
,436:5/ABCDE
,487:5/E.164
16,87:1
17,9:10/8013765024
,10:1
,11.2
,12:0
,167:0
,487:5/E.164
23,9:11/13123149810
,10:1
,11:1
,167:0
,487:5/E.164
P3
42,3:1.
,4:3
,300:1.
43,3:1.
,4:3
,300:1.
44,3:0.
,4:7
,300:0.
45,5:0.3
Avinash I dont want to hard code the value 8013765024. I should be able to read the first two letters of 8013765024 and based on it I should be able to decide whether to change from ,11:1 to ,11:2. If first 2 digits are 80 then only i should change ,11:1 to ,11:2. To add to this I want 8013765024 also to be appended with +11 (+118013765024)
– Garvit Khandelwal
Jun 18 '14 at 6:11
You should avoid usinggetline
read more here: awk.freeshell.org/AllAboutGetline
– Jotne
Jun 18 '14 at 6:12
@Jotne Please stop offering arbitrary "advice" about what not to do. It's (possibly) reasonable to use getline here. Of course, it's still very unclear what the OP actually wants, but getline is not the problem.
– ooga
Jun 18 '14 at 14:43
@AvinashRaj I don't see what the purpose of yourf
variable is. You set it, but never actually use it.
– ooga
Jun 18 '14 at 14:44
yes i set it to change the value of the second line after17,9:10/8013765024
– Avinash Raj
Jun 18 '14 at 14:49
|
show 7 more comments
Give a try to this awk command. Whenevre the awk find a number starting with 80
on column2, it gets the next line and then the next line and change it's value to ,11:2
print the other lines as it is.
$ awk -F/ '$2~/^80/{print; getline; print; getline; $1=",11.2"}1' file1.txt
P1
10,9:11/18013013582
,10:1
,167:0
,487:5/E.164
11,9:15/310410532169026
,10:60
,167:0
,487:4/IMSI
12,18:15/013329002130500
,19:0
15,9:10/9609610015
,10:1
,14:68
,167:0
,436:5/ABCDE
,487:5/E.164
16,87:1
17,9:10/8013765024
,10:1
,11.2
,12:0
,167:0
,487:5/E.164
23,9:11/13123149810
,10:1
,11:1
,167:0
,487:5/E.164
P3
42,3:1.
,4:3
,300:1.
43,3:1.
,4:3
,300:1.
44,3:0.
,4:7
,300:0.
45,5:0.3
Give a try to this awk command. Whenevre the awk find a number starting with 80
on column2, it gets the next line and then the next line and change it's value to ,11:2
print the other lines as it is.
$ awk -F/ '$2~/^80/{print; getline; print; getline; $1=",11.2"}1' file1.txt
P1
10,9:11/18013013582
,10:1
,167:0
,487:5/E.164
11,9:15/310410532169026
,10:60
,167:0
,487:4/IMSI
12,18:15/013329002130500
,19:0
15,9:10/9609610015
,10:1
,14:68
,167:0
,436:5/ABCDE
,487:5/E.164
16,87:1
17,9:10/8013765024
,10:1
,11.2
,12:0
,167:0
,487:5/E.164
23,9:11/13123149810
,10:1
,11:1
,167:0
,487:5/E.164
P3
42,3:1.
,4:3
,300:1.
43,3:1.
,4:3
,300:1.
44,3:0.
,4:7
,300:0.
45,5:0.3
answered Jun 18 '14 at 5:49
Avinash RajAvinash Raj
2,60731227
2,60731227
Avinash I dont want to hard code the value 8013765024. I should be able to read the first two letters of 8013765024 and based on it I should be able to decide whether to change from ,11:1 to ,11:2. If first 2 digits are 80 then only i should change ,11:1 to ,11:2. To add to this I want 8013765024 also to be appended with +11 (+118013765024)
– Garvit Khandelwal
Jun 18 '14 at 6:11
You should avoid usinggetline
read more here: awk.freeshell.org/AllAboutGetline
– Jotne
Jun 18 '14 at 6:12
@Jotne Please stop offering arbitrary "advice" about what not to do. It's (possibly) reasonable to use getline here. Of course, it's still very unclear what the OP actually wants, but getline is not the problem.
– ooga
Jun 18 '14 at 14:43
@AvinashRaj I don't see what the purpose of yourf
variable is. You set it, but never actually use it.
– ooga
Jun 18 '14 at 14:44
yes i set it to change the value of the second line after17,9:10/8013765024
– Avinash Raj
Jun 18 '14 at 14:49
|
show 7 more comments
Avinash I dont want to hard code the value 8013765024. I should be able to read the first two letters of 8013765024 and based on it I should be able to decide whether to change from ,11:1 to ,11:2. If first 2 digits are 80 then only i should change ,11:1 to ,11:2. To add to this I want 8013765024 also to be appended with +11 (+118013765024)
– Garvit Khandelwal
Jun 18 '14 at 6:11
You should avoid usinggetline
read more here: awk.freeshell.org/AllAboutGetline
– Jotne
Jun 18 '14 at 6:12
@Jotne Please stop offering arbitrary "advice" about what not to do. It's (possibly) reasonable to use getline here. Of course, it's still very unclear what the OP actually wants, but getline is not the problem.
– ooga
Jun 18 '14 at 14:43
@AvinashRaj I don't see what the purpose of yourf
variable is. You set it, but never actually use it.
– ooga
Jun 18 '14 at 14:44
yes i set it to change the value of the second line after17,9:10/8013765024
– Avinash Raj
Jun 18 '14 at 14:49
Avinash I dont want to hard code the value 8013765024. I should be able to read the first two letters of 8013765024 and based on it I should be able to decide whether to change from ,11:1 to ,11:2. If first 2 digits are 80 then only i should change ,11:1 to ,11:2. To add to this I want 8013765024 also to be appended with +11 (+118013765024)
– Garvit Khandelwal
Jun 18 '14 at 6:11
Avinash I dont want to hard code the value 8013765024. I should be able to read the first two letters of 8013765024 and based on it I should be able to decide whether to change from ,11:1 to ,11:2. If first 2 digits are 80 then only i should change ,11:1 to ,11:2. To add to this I want 8013765024 also to be appended with +11 (+118013765024)
– Garvit Khandelwal
Jun 18 '14 at 6:11
You should avoid using
getline
read more here: awk.freeshell.org/AllAboutGetline– Jotne
Jun 18 '14 at 6:12
You should avoid using
getline
read more here: awk.freeshell.org/AllAboutGetline– Jotne
Jun 18 '14 at 6:12
@Jotne Please stop offering arbitrary "advice" about what not to do. It's (possibly) reasonable to use getline here. Of course, it's still very unclear what the OP actually wants, but getline is not the problem.
– ooga
Jun 18 '14 at 14:43
@Jotne Please stop offering arbitrary "advice" about what not to do. It's (possibly) reasonable to use getline here. Of course, it's still very unclear what the OP actually wants, but getline is not the problem.
– ooga
Jun 18 '14 at 14:43
@AvinashRaj I don't see what the purpose of your
f
variable is. You set it, but never actually use it.– ooga
Jun 18 '14 at 14:44
@AvinashRaj I don't see what the purpose of your
f
variable is. You set it, but never actually use it.– ooga
Jun 18 '14 at 14:44
yes i set it to change the value of the second line after
17,9:10/8013765024
– Avinash Raj
Jun 18 '14 at 14:49
yes i set it to change the value of the second line after
17,9:10/8013765024
– Avinash Raj
Jun 18 '14 at 14:49
|
show 7 more comments
Here is an awk
solution:
$ awk -F '/|:' '
$3 == "8013765024" {flag = 1}
$0 == ",11:1" && flag {$2 = 2;flag = 0}
1
' OFS=':' file
add a comment |
Here is an awk
solution:
$ awk -F '/|:' '
$3 == "8013765024" {flag = 1}
$0 == ",11:1" && flag {$2 = 2;flag = 0}
1
' OFS=':' file
add a comment |
Here is an awk
solution:
$ awk -F '/|:' '
$3 == "8013765024" {flag = 1}
$0 == ",11:1" && flag {$2 = 2;flag = 0}
1
' OFS=':' file
Here is an awk
solution:
$ awk -F '/|:' '
$3 == "8013765024" {flag = 1}
$0 == ",11:1" && flag {$2 = 2;flag = 0}
1
' OFS=':' file
answered Jul 24 '14 at 2:30
cuonglmcuonglm
103k24203302
103k24203302
add a comment |
add a comment |
Here's a possible sed
solution:
sed '/17,9:10/80/,/11:1/ {
s/8013765024/+118013765024/
s/11:1/11:2/ }' file.txt
This starts the substitution after it finds 17,9:10/80
and first appends +11
to 8013765024
making it +118013765024
and it also replaces 11:1
with 11:2
for only the first occurrence of 11:1
(the range is inclusive)
Running diff
on the output of the sed
and the initial file shows:
19c19
< 17,9:10/+118013765024
---
> 17,9:10/8013765024
21c21
< ,11:2
---
> ,11:1
meaning that the only changes to the file are the addition of [+11
] and the substitution of [11:1
] to [11:2
]. Let me know if this correct.
add a comment |
Here's a possible sed
solution:
sed '/17,9:10/80/,/11:1/ {
s/8013765024/+118013765024/
s/11:1/11:2/ }' file.txt
This starts the substitution after it finds 17,9:10/80
and first appends +11
to 8013765024
making it +118013765024
and it also replaces 11:1
with 11:2
for only the first occurrence of 11:1
(the range is inclusive)
Running diff
on the output of the sed
and the initial file shows:
19c19
< 17,9:10/+118013765024
---
> 17,9:10/8013765024
21c21
< ,11:2
---
> ,11:1
meaning that the only changes to the file are the addition of [+11
] and the substitution of [11:1
] to [11:2
]. Let me know if this correct.
add a comment |
Here's a possible sed
solution:
sed '/17,9:10/80/,/11:1/ {
s/8013765024/+118013765024/
s/11:1/11:2/ }' file.txt
This starts the substitution after it finds 17,9:10/80
and first appends +11
to 8013765024
making it +118013765024
and it also replaces 11:1
with 11:2
for only the first occurrence of 11:1
(the range is inclusive)
Running diff
on the output of the sed
and the initial file shows:
19c19
< 17,9:10/+118013765024
---
> 17,9:10/8013765024
21c21
< ,11:2
---
> ,11:1
meaning that the only changes to the file are the addition of [+11
] and the substitution of [11:1
] to [11:2
]. Let me know if this correct.
Here's a possible sed
solution:
sed '/17,9:10/80/,/11:1/ {
s/8013765024/+118013765024/
s/11:1/11:2/ }' file.txt
This starts the substitution after it finds 17,9:10/80
and first appends +11
to 8013765024
making it +118013765024
and it also replaces 11:1
with 11:2
for only the first occurrence of 11:1
(the range is inclusive)
Running diff
on the output of the sed
and the initial file shows:
19c19
< 17,9:10/+118013765024
---
> 17,9:10/8013765024
21c21
< ,11:2
---
> ,11:1
meaning that the only changes to the file are the addition of [+11
] and the substitution of [11:1
] to [11:2
]. Let me know if this correct.
answered Jul 29 '14 at 14:32
skamazinskamazin
184129
184129
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.
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%2f146248%2ffinding-and-replacing-text-inside-awk-block%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
you want to change the value which was present just after to
17,9:10/8013765024
– Avinash Raj
Jun 18 '14 at 4:42
You want to prune this down to a really minimal example and explain how 2 is "based on" 8013765024. Is that a magic number we should be looking for, or a threshold value, or what are the conditions under which the first ,11:1 should be replaced?
– tripleee
Jun 18 '14 at 5:01
You cannot apply a regular expression across lines. You will need something like
/^17[,0-9:]*$/ { found=1 } found&/^,11/ { do things; found=0 }
– tripleee
Jun 18 '14 at 5:04
Your explanation is gibberish. Try rewording it.
– ooga
Jun 18 '14 at 5:07
Sorry for being unclear. 17,9:10/8013765024 ,10:1 ,11:1 see, if value after 17,9:10/ i.e. 8013765024 starts with 80 then I want to change this to +118013765024 and also the next ,11:1 value to ,11:2. I hope I am clear this time. Please let me know if I am not.
– Garvit Khandelwal
Jun 18 '14 at 5:40