How to replace a number by its corresponding string (low/high) in bash?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a file:
property(Address1, 4.5)
property(Address2, 2.2)
property(Address3, 9.0)
property(Address4, 3.4)
---
I want to replace the floating points based on some conditions (For example: if the number is between 2 and 4, I will replace it by Low, otherwise High) to produce this:
property(Address1, High)
property(Address2, Low)
property(Address3, High)
property(Address4, Low)
---
My try: (1) First extract the number using $. (2) Check whether the condition satisfies (3) then print it. But in this process I am failing to output the Address* to the output file.
shell-script shell awk replace
add a comment |
up vote
0
down vote
favorite
I have a file:
property(Address1, 4.5)
property(Address2, 2.2)
property(Address3, 9.0)
property(Address4, 3.4)
---
I want to replace the floating points based on some conditions (For example: if the number is between 2 and 4, I will replace it by Low, otherwise High) to produce this:
property(Address1, High)
property(Address2, Low)
property(Address3, High)
property(Address4, Low)
---
My try: (1) First extract the number using $. (2) Check whether the condition satisfies (3) then print it. But in this process I am failing to output the Address* to the output file.
shell-script shell awk replace
The process you describe sounds correct. After you calculate whether the floating point is high or low, you can use something likesed
to replace the number in the string with high or low.
– Peschke
Dec 1 at 5:22
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a file:
property(Address1, 4.5)
property(Address2, 2.2)
property(Address3, 9.0)
property(Address4, 3.4)
---
I want to replace the floating points based on some conditions (For example: if the number is between 2 and 4, I will replace it by Low, otherwise High) to produce this:
property(Address1, High)
property(Address2, Low)
property(Address3, High)
property(Address4, Low)
---
My try: (1) First extract the number using $. (2) Check whether the condition satisfies (3) then print it. But in this process I am failing to output the Address* to the output file.
shell-script shell awk replace
I have a file:
property(Address1, 4.5)
property(Address2, 2.2)
property(Address3, 9.0)
property(Address4, 3.4)
---
I want to replace the floating points based on some conditions (For example: if the number is between 2 and 4, I will replace it by Low, otherwise High) to produce this:
property(Address1, High)
property(Address2, Low)
property(Address3, High)
property(Address4, Low)
---
My try: (1) First extract the number using $. (2) Check whether the condition satisfies (3) then print it. But in this process I am failing to output the Address* to the output file.
shell-script shell awk replace
shell-script shell awk replace
edited Dec 1 at 6:07
asked Dec 1 at 5:17
Coder
1126
1126
The process you describe sounds correct. After you calculate whether the floating point is high or low, you can use something likesed
to replace the number in the string with high or low.
– Peschke
Dec 1 at 5:22
add a comment |
The process you describe sounds correct. After you calculate whether the floating point is high or low, you can use something likesed
to replace the number in the string with high or low.
– Peschke
Dec 1 at 5:22
The process you describe sounds correct. After you calculate whether the floating point is high or low, you can use something like
sed
to replace the number in the string with high or low.– Peschke
Dec 1 at 5:22
The process you describe sounds correct. After you calculate whether the floating point is high or low, you can use something like
sed
to replace the number in the string with high or low.– Peschke
Dec 1 at 5:22
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
This does (strictly) what you ask for:
awk ' print $1, (($2>2)&&($2<4))?"Low)":"High)" '
But it is taking advantage of the fact that an string like 9.0)
(with the trailing parenthesis) is converted to the number 9
by awk. That may fail if an space gets added for example.
A more strict solution is to also use the closing parenthesis as a field delimiter to remove it from the contents of field number 2
.
awk -F'[ )]' 'print $1,(($2>2)&&($2<4))?"Low)":"High)"'
add a comment |
up vote
1
down vote
I have tried with if and else condition
sed "s/)$//g" filename| awk 'if($2 > 2 && $2 < 4) $2="low)";print $0else$2="high)";print $0'
property(Address1, high)
property(Address2, low)
property(Address3, high)
property(Address4, low)
add a comment |
up vote
1
down vote
With perl
, assuming numbers are in the format [-]<digits>[.<digits>]
, and matching on them wherever they are in the input provided they're neither preceded nor followed by word characters (so Address1
is not changed to AddressHigh
for instance):
<input perl -pe 's(?<!w)-?d[d.]*(?!w)$& >=2 && $& <=4 ? "Low" : "High"ge'
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
This does (strictly) what you ask for:
awk ' print $1, (($2>2)&&($2<4))?"Low)":"High)" '
But it is taking advantage of the fact that an string like 9.0)
(with the trailing parenthesis) is converted to the number 9
by awk. That may fail if an space gets added for example.
A more strict solution is to also use the closing parenthesis as a field delimiter to remove it from the contents of field number 2
.
awk -F'[ )]' 'print $1,(($2>2)&&($2<4))?"Low)":"High)"'
add a comment |
up vote
2
down vote
accepted
This does (strictly) what you ask for:
awk ' print $1, (($2>2)&&($2<4))?"Low)":"High)" '
But it is taking advantage of the fact that an string like 9.0)
(with the trailing parenthesis) is converted to the number 9
by awk. That may fail if an space gets added for example.
A more strict solution is to also use the closing parenthesis as a field delimiter to remove it from the contents of field number 2
.
awk -F'[ )]' 'print $1,(($2>2)&&($2<4))?"Low)":"High)"'
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
This does (strictly) what you ask for:
awk ' print $1, (($2>2)&&($2<4))?"Low)":"High)" '
But it is taking advantage of the fact that an string like 9.0)
(with the trailing parenthesis) is converted to the number 9
by awk. That may fail if an space gets added for example.
A more strict solution is to also use the closing parenthesis as a field delimiter to remove it from the contents of field number 2
.
awk -F'[ )]' 'print $1,(($2>2)&&($2<4))?"Low)":"High)"'
This does (strictly) what you ask for:
awk ' print $1, (($2>2)&&($2<4))?"Low)":"High)" '
But it is taking advantage of the fact that an string like 9.0)
(with the trailing parenthesis) is converted to the number 9
by awk. That may fail if an space gets added for example.
A more strict solution is to also use the closing parenthesis as a field delimiter to remove it from the contents of field number 2
.
awk -F'[ )]' 'print $1,(($2>2)&&($2<4))?"Low)":"High)"'
edited Dec 1 at 7:03
answered Dec 1 at 5:58
Isaac
10.8k11447
10.8k11447
add a comment |
add a comment |
up vote
1
down vote
I have tried with if and else condition
sed "s/)$//g" filename| awk 'if($2 > 2 && $2 < 4) $2="low)";print $0else$2="high)";print $0'
property(Address1, high)
property(Address2, low)
property(Address3, high)
property(Address4, low)
add a comment |
up vote
1
down vote
I have tried with if and else condition
sed "s/)$//g" filename| awk 'if($2 > 2 && $2 < 4) $2="low)";print $0else$2="high)";print $0'
property(Address1, high)
property(Address2, low)
property(Address3, high)
property(Address4, low)
add a comment |
up vote
1
down vote
up vote
1
down vote
I have tried with if and else condition
sed "s/)$//g" filename| awk 'if($2 > 2 && $2 < 4) $2="low)";print $0else$2="high)";print $0'
property(Address1, high)
property(Address2, low)
property(Address3, high)
property(Address4, low)
I have tried with if and else condition
sed "s/)$//g" filename| awk 'if($2 > 2 && $2 < 4) $2="low)";print $0else$2="high)";print $0'
property(Address1, high)
property(Address2, low)
property(Address3, high)
property(Address4, low)
answered Dec 1 at 6:21
Praveen Kumar BS
1,162138
1,162138
add a comment |
add a comment |
up vote
1
down vote
With perl
, assuming numbers are in the format [-]<digits>[.<digits>]
, and matching on them wherever they are in the input provided they're neither preceded nor followed by word characters (so Address1
is not changed to AddressHigh
for instance):
<input perl -pe 's(?<!w)-?d[d.]*(?!w)$& >=2 && $& <=4 ? "Low" : "High"ge'
add a comment |
up vote
1
down vote
With perl
, assuming numbers are in the format [-]<digits>[.<digits>]
, and matching on them wherever they are in the input provided they're neither preceded nor followed by word characters (so Address1
is not changed to AddressHigh
for instance):
<input perl -pe 's(?<!w)-?d[d.]*(?!w)$& >=2 && $& <=4 ? "Low" : "High"ge'
add a comment |
up vote
1
down vote
up vote
1
down vote
With perl
, assuming numbers are in the format [-]<digits>[.<digits>]
, and matching on them wherever they are in the input provided they're neither preceded nor followed by word characters (so Address1
is not changed to AddressHigh
for instance):
<input perl -pe 's(?<!w)-?d[d.]*(?!w)$& >=2 && $& <=4 ? "Low" : "High"ge'
With perl
, assuming numbers are in the format [-]<digits>[.<digits>]
, and matching on them wherever they are in the input provided they're neither preceded nor followed by word characters (so Address1
is not changed to AddressHigh
for instance):
<input perl -pe 's(?<!w)-?d[d.]*(?!w)$& >=2 && $& <=4 ? "Low" : "High"ge'
edited Dec 1 at 10:28
answered Dec 1 at 7:25
Stéphane Chazelas
296k54560905
296k54560905
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%2f485299%2fhow-to-replace-a-number-by-its-corresponding-string-low-high-in-bash%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 process you describe sounds correct. After you calculate whether the floating point is high or low, you can use something like
sed
to replace the number in the string with high or low.– Peschke
Dec 1 at 5:22