Read value from specific row and column
I have csv file in this format:
1 value1
2 value2
3 value3
I can also define it like this (if it is more convenient)
column1,column2
1,value1
2,value2
3,value3
I should write a shell script in order to match value from the first column and take value from that row from the second column value.
So for example, I have variable var
If var is equal to 1
then take and use (or print) value1
.
If value is 2 take value2 and so on.
Number of rows is limited to 5.
Is this possible?
Thanks
linux bash shell centos sh
|
show 1 more comment
I have csv file in this format:
1 value1
2 value2
3 value3
I can also define it like this (if it is more convenient)
column1,column2
1,value1
2,value2
3,value3
I should write a shell script in order to match value from the first column and take value from that row from the second column value.
So for example, I have variable var
If var is equal to 1
then take and use (or print) value1
.
If value is 2 take value2 and so on.
Number of rows is limited to 5.
Is this possible?
Thanks
linux bash shell centos sh
2
use grep and cut like so:var=1 ; grep "^$var" yourfile.txt | cut -d',' -f2
– Red Cricket
Nov 12 '18 at 23:15
Hi Red. For what purposes is this used "^1"? how to match with my variable which will be read within the script? Thanks
– Dejan
Nov 12 '18 at 23:17
the^
mean match the start of a line. So^1
means match line that starts with 1
– Red Cricket
Nov 12 '18 at 23:18
Hi Red. but what if my value2 for example is something like this "1.1.1". Does it mean if I grep it with value "1" it will grep me the frst row (because of the first row first column value) but also and second row (because it is similar to second row second column value )? thanks
– Dejan
Nov 12 '18 at 23:20
try it and see.
– Red Cricket
Nov 12 '18 at 23:21
|
show 1 more comment
I have csv file in this format:
1 value1
2 value2
3 value3
I can also define it like this (if it is more convenient)
column1,column2
1,value1
2,value2
3,value3
I should write a shell script in order to match value from the first column and take value from that row from the second column value.
So for example, I have variable var
If var is equal to 1
then take and use (or print) value1
.
If value is 2 take value2 and so on.
Number of rows is limited to 5.
Is this possible?
Thanks
linux bash shell centos sh
I have csv file in this format:
1 value1
2 value2
3 value3
I can also define it like this (if it is more convenient)
column1,column2
1,value1
2,value2
3,value3
I should write a shell script in order to match value from the first column and take value from that row from the second column value.
So for example, I have variable var
If var is equal to 1
then take and use (or print) value1
.
If value is 2 take value2 and so on.
Number of rows is limited to 5.
Is this possible?
Thanks
linux bash shell centos sh
linux bash shell centos sh
edited Nov 12 '18 at 23:47
codeforester
17.4k83864
17.4k83864
asked Nov 12 '18 at 23:11
DejanDejan
57082148
57082148
2
use grep and cut like so:var=1 ; grep "^$var" yourfile.txt | cut -d',' -f2
– Red Cricket
Nov 12 '18 at 23:15
Hi Red. For what purposes is this used "^1"? how to match with my variable which will be read within the script? Thanks
– Dejan
Nov 12 '18 at 23:17
the^
mean match the start of a line. So^1
means match line that starts with 1
– Red Cricket
Nov 12 '18 at 23:18
Hi Red. but what if my value2 for example is something like this "1.1.1". Does it mean if I grep it with value "1" it will grep me the frst row (because of the first row first column value) but also and second row (because it is similar to second row second column value )? thanks
– Dejan
Nov 12 '18 at 23:20
try it and see.
– Red Cricket
Nov 12 '18 at 23:21
|
show 1 more comment
2
use grep and cut like so:var=1 ; grep "^$var" yourfile.txt | cut -d',' -f2
– Red Cricket
Nov 12 '18 at 23:15
Hi Red. For what purposes is this used "^1"? how to match with my variable which will be read within the script? Thanks
– Dejan
Nov 12 '18 at 23:17
the^
mean match the start of a line. So^1
means match line that starts with 1
– Red Cricket
Nov 12 '18 at 23:18
Hi Red. but what if my value2 for example is something like this "1.1.1". Does it mean if I grep it with value "1" it will grep me the frst row (because of the first row first column value) but also and second row (because it is similar to second row second column value )? thanks
– Dejan
Nov 12 '18 at 23:20
try it and see.
– Red Cricket
Nov 12 '18 at 23:21
2
2
use grep and cut like so:
var=1 ; grep "^$var" yourfile.txt | cut -d',' -f2
– Red Cricket
Nov 12 '18 at 23:15
use grep and cut like so:
var=1 ; grep "^$var" yourfile.txt | cut -d',' -f2
– Red Cricket
Nov 12 '18 at 23:15
Hi Red. For what purposes is this used "^1"? how to match with my variable which will be read within the script? Thanks
– Dejan
Nov 12 '18 at 23:17
Hi Red. For what purposes is this used "^1"? how to match with my variable which will be read within the script? Thanks
– Dejan
Nov 12 '18 at 23:17
the
^
mean match the start of a line. So ^1
means match line that starts with 1– Red Cricket
Nov 12 '18 at 23:18
the
^
mean match the start of a line. So ^1
means match line that starts with 1– Red Cricket
Nov 12 '18 at 23:18
Hi Red. but what if my value2 for example is something like this "1.1.1". Does it mean if I grep it with value "1" it will grep me the frst row (because of the first row first column value) but also and second row (because it is similar to second row second column value )? thanks
– Dejan
Nov 12 '18 at 23:20
Hi Red. but what if my value2 for example is something like this "1.1.1". Does it mean if I grep it with value "1" it will grep me the frst row (because of the first row first column value) but also and second row (because it is similar to second row second column value )? thanks
– Dejan
Nov 12 '18 at 23:20
try it and see.
– Red Cricket
Nov 12 '18 at 23:21
try it and see.
– Red Cricket
Nov 12 '18 at 23:21
|
show 1 more comment
1 Answer
1
active
oldest
votes
Awk is your friend here:
$ cat input.txt
1 value1
2 value2
3 value3
$ awk -v key=1 '$1 == key print $2 ' input.txt
value1
$ awk -v key=2 '$1 == key print $2 ' input.txt
value2
etc. Just replace the key=1
with key=$var
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53271435%2fread-value-from-specific-row-and-column%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
Awk is your friend here:
$ cat input.txt
1 value1
2 value2
3 value3
$ awk -v key=1 '$1 == key print $2 ' input.txt
value1
$ awk -v key=2 '$1 == key print $2 ' input.txt
value2
etc. Just replace the key=1
with key=$var
.
add a comment |
Awk is your friend here:
$ cat input.txt
1 value1
2 value2
3 value3
$ awk -v key=1 '$1 == key print $2 ' input.txt
value1
$ awk -v key=2 '$1 == key print $2 ' input.txt
value2
etc. Just replace the key=1
with key=$var
.
add a comment |
Awk is your friend here:
$ cat input.txt
1 value1
2 value2
3 value3
$ awk -v key=1 '$1 == key print $2 ' input.txt
value1
$ awk -v key=2 '$1 == key print $2 ' input.txt
value2
etc. Just replace the key=1
with key=$var
.
Awk is your friend here:
$ cat input.txt
1 value1
2 value2
3 value3
$ awk -v key=1 '$1 == key print $2 ' input.txt
value1
$ awk -v key=2 '$1 == key print $2 ' input.txt
value2
etc. Just replace the key=1
with key=$var
.
answered Nov 12 '18 at 23:36
ShawnShawn
3,5731613
3,5731613
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53271435%2fread-value-from-specific-row-and-column%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
2
use grep and cut like so:
var=1 ; grep "^$var" yourfile.txt | cut -d',' -f2
– Red Cricket
Nov 12 '18 at 23:15
Hi Red. For what purposes is this used "^1"? how to match with my variable which will be read within the script? Thanks
– Dejan
Nov 12 '18 at 23:17
the
^
mean match the start of a line. So^1
means match line that starts with 1– Red Cricket
Nov 12 '18 at 23:18
Hi Red. but what if my value2 for example is something like this "1.1.1". Does it mean if I grep it with value "1" it will grep me the frst row (because of the first row first column value) but also and second row (because it is similar to second row second column value )? thanks
– Dejan
Nov 12 '18 at 23:20
try it and see.
– Red Cricket
Nov 12 '18 at 23:21