Replacing same keyword with different strings per line
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I'm using the following command to return a list of available disks and whether they're ssd or rotational (basically replacing 0 and 1 in lsblk's output with their actual meaning):
lsblk -d -o name,ro | sed "s/RO/ TYPE/; s/NAME/DISK/; s/0/SSD/g; s/1/ROT/g"
sample output:
DISK TYPE
sda SSD
sdb ROT
Now I'd like to add another column, "rm", to lsblk
(lsblk -d -o name,ro,rm
) but it also uses 0 and 1 to mean a different thing (0=fixed disk, 1=removable).
Is there a way to use sed (or awk or anything else) to replace 0 and 1's in the third column with YES and NO?
text-processing awk sed lsblk
add a comment |Â
up vote
1
down vote
favorite
I'm using the following command to return a list of available disks and whether they're ssd or rotational (basically replacing 0 and 1 in lsblk's output with their actual meaning):
lsblk -d -o name,ro | sed "s/RO/ TYPE/; s/NAME/DISK/; s/0/SSD/g; s/1/ROT/g"
sample output:
DISK TYPE
sda SSD
sdb ROT
Now I'd like to add another column, "rm", to lsblk
(lsblk -d -o name,ro,rm
) but it also uses 0 and 1 to mean a different thing (0=fixed disk, 1=removable).
Is there a way to use sed (or awk or anything else) to replace 0 and 1's in the third column with YES and NO?
text-processing awk sed lsblk
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm using the following command to return a list of available disks and whether they're ssd or rotational (basically replacing 0 and 1 in lsblk's output with their actual meaning):
lsblk -d -o name,ro | sed "s/RO/ TYPE/; s/NAME/DISK/; s/0/SSD/g; s/1/ROT/g"
sample output:
DISK TYPE
sda SSD
sdb ROT
Now I'd like to add another column, "rm", to lsblk
(lsblk -d -o name,ro,rm
) but it also uses 0 and 1 to mean a different thing (0=fixed disk, 1=removable).
Is there a way to use sed (or awk or anything else) to replace 0 and 1's in the third column with YES and NO?
text-processing awk sed lsblk
I'm using the following command to return a list of available disks and whether they're ssd or rotational (basically replacing 0 and 1 in lsblk's output with their actual meaning):
lsblk -d -o name,ro | sed "s/RO/ TYPE/; s/NAME/DISK/; s/0/SSD/g; s/1/ROT/g"
sample output:
DISK TYPE
sda SSD
sdb ROT
Now I'd like to add another column, "rm", to lsblk
(lsblk -d -o name,ro,rm
) but it also uses 0 and 1 to mean a different thing (0=fixed disk, 1=removable).
Is there a way to use sed (or awk or anything else) to replace 0 and 1's in the third column with YES and NO?
text-processing awk sed lsblk
edited Jul 3 at 13:56
Jeff Schaller
30.8k846104
30.8k846104
asked Jul 3 at 13:47
Mark Roi
448
448
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Awk
approach:
lsblk -d -o name,ro,rm
| awk 'NR == 1 print "DISK TYPE REMOVABLE"; split("SSD ROT", ro); split("NO YES", rm); next
print $1, ro[$2+1], rm[$3+1] '
Sample output:
DISK TYPE REMOVABLE
sda SSD NO
sr0 SSD YES
Both answers valid, but choosing this one for the smarter approach (didn't know about split). Thanks!
â Mark Roi
Jul 4 at 13:20
@MarkRoi, you're welcome
â RomanPerekhrest
Jul 4 at 13:36
add a comment |Â
up vote
3
down vote
try
lsblk -d -o name,ro,rm |
awk '$2 == 0 $2 = "SSD" ; $2 == 1 $2 = "ROT" ;
$3 == 0 $3 = "NO" ; $3 == 1 $3 = "YES" ;
print ; '
- this can be one-lined.
- sometime people use
1
instead ofprint ;
. - changing
RO
toTYPE
is left as an exercice.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Awk
approach:
lsblk -d -o name,ro,rm
| awk 'NR == 1 print "DISK TYPE REMOVABLE"; split("SSD ROT", ro); split("NO YES", rm); next
print $1, ro[$2+1], rm[$3+1] '
Sample output:
DISK TYPE REMOVABLE
sda SSD NO
sr0 SSD YES
Both answers valid, but choosing this one for the smarter approach (didn't know about split). Thanks!
â Mark Roi
Jul 4 at 13:20
@MarkRoi, you're welcome
â RomanPerekhrest
Jul 4 at 13:36
add a comment |Â
up vote
2
down vote
accepted
Awk
approach:
lsblk -d -o name,ro,rm
| awk 'NR == 1 print "DISK TYPE REMOVABLE"; split("SSD ROT", ro); split("NO YES", rm); next
print $1, ro[$2+1], rm[$3+1] '
Sample output:
DISK TYPE REMOVABLE
sda SSD NO
sr0 SSD YES
Both answers valid, but choosing this one for the smarter approach (didn't know about split). Thanks!
â Mark Roi
Jul 4 at 13:20
@MarkRoi, you're welcome
â RomanPerekhrest
Jul 4 at 13:36
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Awk
approach:
lsblk -d -o name,ro,rm
| awk 'NR == 1 print "DISK TYPE REMOVABLE"; split("SSD ROT", ro); split("NO YES", rm); next
print $1, ro[$2+1], rm[$3+1] '
Sample output:
DISK TYPE REMOVABLE
sda SSD NO
sr0 SSD YES
Awk
approach:
lsblk -d -o name,ro,rm
| awk 'NR == 1 print "DISK TYPE REMOVABLE"; split("SSD ROT", ro); split("NO YES", rm); next
print $1, ro[$2+1], rm[$3+1] '
Sample output:
DISK TYPE REMOVABLE
sda SSD NO
sr0 SSD YES
answered Jul 3 at 14:32
RomanPerekhrest
22.4k12144
22.4k12144
Both answers valid, but choosing this one for the smarter approach (didn't know about split). Thanks!
â Mark Roi
Jul 4 at 13:20
@MarkRoi, you're welcome
â RomanPerekhrest
Jul 4 at 13:36
add a comment |Â
Both answers valid, but choosing this one for the smarter approach (didn't know about split). Thanks!
â Mark Roi
Jul 4 at 13:20
@MarkRoi, you're welcome
â RomanPerekhrest
Jul 4 at 13:36
Both answers valid, but choosing this one for the smarter approach (didn't know about split). Thanks!
â Mark Roi
Jul 4 at 13:20
Both answers valid, but choosing this one for the smarter approach (didn't know about split). Thanks!
â Mark Roi
Jul 4 at 13:20
@MarkRoi, you're welcome
â RomanPerekhrest
Jul 4 at 13:36
@MarkRoi, you're welcome
â RomanPerekhrest
Jul 4 at 13:36
add a comment |Â
up vote
3
down vote
try
lsblk -d -o name,ro,rm |
awk '$2 == 0 $2 = "SSD" ; $2 == 1 $2 = "ROT" ;
$3 == 0 $3 = "NO" ; $3 == 1 $3 = "YES" ;
print ; '
- this can be one-lined.
- sometime people use
1
instead ofprint ;
. - changing
RO
toTYPE
is left as an exercice.
add a comment |Â
up vote
3
down vote
try
lsblk -d -o name,ro,rm |
awk '$2 == 0 $2 = "SSD" ; $2 == 1 $2 = "ROT" ;
$3 == 0 $3 = "NO" ; $3 == 1 $3 = "YES" ;
print ; '
- this can be one-lined.
- sometime people use
1
instead ofprint ;
. - changing
RO
toTYPE
is left as an exercice.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
try
lsblk -d -o name,ro,rm |
awk '$2 == 0 $2 = "SSD" ; $2 == 1 $2 = "ROT" ;
$3 == 0 $3 = "NO" ; $3 == 1 $3 = "YES" ;
print ; '
- this can be one-lined.
- sometime people use
1
instead ofprint ;
. - changing
RO
toTYPE
is left as an exercice.
try
lsblk -d -o name,ro,rm |
awk '$2 == 0 $2 = "SSD" ; $2 == 1 $2 = "ROT" ;
$3 == 0 $3 = "NO" ; $3 == 1 $3 = "YES" ;
print ; '
- this can be one-lined.
- sometime people use
1
instead ofprint ;
. - changing
RO
toTYPE
is left as an exercice.
answered Jul 3 at 14:03
Archemar
18.9k93365
18.9k93365
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453227%2freplacing-same-keyword-with-different-strings-per-line%23new-answer', 'question_page');
);
Post as a guest
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
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
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