How to get the nth line from a file and save the result to a variable [duplicate]How do I set a variable to the output of a command in Bash?Bash tool to get nth line from a fileGet the source directory of a Bash script from within the script itselfHow can I remove the first line of a text file using bash/sed script?How to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?How to count all the lines of code in a directory recursively?Looping through the content of a file in BashHow to check if a variable is set in Bash?How to concatenate string variables in BashHow do I set a variable to the output of a command in Bash?Read a file line by line assigning the value to a variable
I feel like most of my characters are the same, what can I do?
Is there any actual security benefit to restricting foreign IPs?
Can Bless or Bardic Inspiration help a creature from rolling a 1 on a death save?
Are actors contractually obligated to certain things like going nude/ Sensual Scenes/ Gory Scenes?
Why does NASA publish all the results/data it gets?
Safely hang a mirror that does not have hooks
Wired to Wireless Doorbell
GitHub repo with Apache License version 2 in package.json, but no full license copy nor comment headers
Should the pagination be reset when changing the order?
As an employer, can I compel my employees to vote?
What do these pins mean? Where should I plug them in?
US entry with tourist visa but past alcohol arrest
How is the problem, ⟨G⟩ in Logspace?
Manager encourages me to take day of sick leave instead of PTO, what's in it for him?
How to reference parameters outside of Apex Class that can be configured by Administrator
What did the controller say during my approach to land (audio clip)?
Resolving moral conflict
What do you do if you have developments on your paper during the long peer review process?
Understanding an example in Golan's "Linear Algebra"
How do I improve in sight reading?
What was an "insurance cover"?
I reverse the source code, you negate the output!
How to ask a man to not take up more than one seat on public transport while avoiding conflict?
How do rulers get rich from war?
How to get the nth line from a file and save the result to a variable [duplicate]
How do I set a variable to the output of a command in Bash?Bash tool to get nth line from a fileGet the source directory of a Bash script from within the script itselfHow can I remove the first line of a text file using bash/sed script?How to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?How to count all the lines of code in a directory recursively?Looping through the content of a file in BashHow to check if a variable is set in Bash?How to concatenate string variables in BashHow do I set a variable to the output of a command in Bash?Read a file line by line assigning the value to a variable
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This question already has an answer here:
Bash tool to get nth line from a file
18 answers
How do I set a variable to the output of a command in Bash?
14 answers
So, I am creating a script in bash and I am trying to read a text file which is saved in the same directory. I need to read the nth
line of that file and then save it to a variable to be used later but I'm not sure how I can do this
What I have currently tried is listed below but it essentially reads the line from the file, saves it a variable and then deletes that line from the file and repeats. This is a hack and although it works, isn't what I want, I can't get the nth value
and it's deleting from the file which I definitely don't want.
read -r first<"test.txt" // reads first line and stores in first
sed -i -e "1d" "test.txt" . // removes first line
read -r second<"test.txt" // reads first line and stores in second
sed -i -e "1d" "test.txt" . // removes first line
If I wanted to get the 2nd line for example, I have seen sed '2q;d' file
but not sure how/where the result is saved. It gets printed in terminal? Any help appreciated, thanks!w
bash shell terminal
marked as duplicate by Socowi, tripleee
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 28 at 16:19
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment
|
This question already has an answer here:
Bash tool to get nth line from a file
18 answers
How do I set a variable to the output of a command in Bash?
14 answers
So, I am creating a script in bash and I am trying to read a text file which is saved in the same directory. I need to read the nth
line of that file and then save it to a variable to be used later but I'm not sure how I can do this
What I have currently tried is listed below but it essentially reads the line from the file, saves it a variable and then deletes that line from the file and repeats. This is a hack and although it works, isn't what I want, I can't get the nth value
and it's deleting from the file which I definitely don't want.
read -r first<"test.txt" // reads first line and stores in first
sed -i -e "1d" "test.txt" . // removes first line
read -r second<"test.txt" // reads first line and stores in second
sed -i -e "1d" "test.txt" . // removes first line
If I wanted to get the 2nd line for example, I have seen sed '2q;d' file
but not sure how/where the result is saved. It gets printed in terminal? Any help appreciated, thanks!w
bash shell terminal
marked as duplicate by Socowi, tripleee
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 28 at 16:19
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You can also write that sed assed -n '2p;q'
which I find (a little) more obvious.
– glenn jackman
Mar 28 at 15:58
add a comment
|
This question already has an answer here:
Bash tool to get nth line from a file
18 answers
How do I set a variable to the output of a command in Bash?
14 answers
So, I am creating a script in bash and I am trying to read a text file which is saved in the same directory. I need to read the nth
line of that file and then save it to a variable to be used later but I'm not sure how I can do this
What I have currently tried is listed below but it essentially reads the line from the file, saves it a variable and then deletes that line from the file and repeats. This is a hack and although it works, isn't what I want, I can't get the nth value
and it's deleting from the file which I definitely don't want.
read -r first<"test.txt" // reads first line and stores in first
sed -i -e "1d" "test.txt" . // removes first line
read -r second<"test.txt" // reads first line and stores in second
sed -i -e "1d" "test.txt" . // removes first line
If I wanted to get the 2nd line for example, I have seen sed '2q;d' file
but not sure how/where the result is saved. It gets printed in terminal? Any help appreciated, thanks!w
bash shell terminal
This question already has an answer here:
Bash tool to get nth line from a file
18 answers
How do I set a variable to the output of a command in Bash?
14 answers
So, I am creating a script in bash and I am trying to read a text file which is saved in the same directory. I need to read the nth
line of that file and then save it to a variable to be used later but I'm not sure how I can do this
What I have currently tried is listed below but it essentially reads the line from the file, saves it a variable and then deletes that line from the file and repeats. This is a hack and although it works, isn't what I want, I can't get the nth value
and it's deleting from the file which I definitely don't want.
read -r first<"test.txt" // reads first line and stores in first
sed -i -e "1d" "test.txt" . // removes first line
read -r second<"test.txt" // reads first line and stores in second
sed -i -e "1d" "test.txt" . // removes first line
If I wanted to get the 2nd line for example, I have seen sed '2q;d' file
but not sure how/where the result is saved. It gets printed in terminal? Any help appreciated, thanks!w
This question already has an answer here:
Bash tool to get nth line from a file
18 answers
How do I set a variable to the output of a command in Bash?
14 answers
bash shell terminal
bash shell terminal
asked Mar 28 at 15:02
NoumanNouman
1093 silver badges13 bronze badges
1093 silver badges13 bronze badges
marked as duplicate by Socowi, tripleee
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 28 at 16:19
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Socowi, tripleee
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 28 at 16:19
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Socowi, tripleee
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 28 at 16:19
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You can also write that sed assed -n '2p;q'
which I find (a little) more obvious.
– glenn jackman
Mar 28 at 15:58
add a comment
|
You can also write that sed assed -n '2p;q'
which I find (a little) more obvious.
– glenn jackman
Mar 28 at 15:58
You can also write that sed as
sed -n '2p;q'
which I find (a little) more obvious.– glenn jackman
Mar 28 at 15:58
You can also write that sed as
sed -n '2p;q'
which I find (a little) more obvious.– glenn jackman
Mar 28 at 15:58
add a comment
|
2 Answers
2
active
oldest
votes
sed '2q;d' file
prints the second line in file
to the terminal.
To populate a variable with it, use bash's command expansion feature:
$ var=$(sed '2q;d' file)
$ echo "$var"
this is second line
add a comment
|
Simple solution using head
and tail
:
a=$(head -2 test.txt | tail -1 )
Saves the second line of test.txt to the variable $a
.
add a comment
|
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
sed '2q;d' file
prints the second line in file
to the terminal.
To populate a variable with it, use bash's command expansion feature:
$ var=$(sed '2q;d' file)
$ echo "$var"
this is second line
add a comment
|
sed '2q;d' file
prints the second line in file
to the terminal.
To populate a variable with it, use bash's command expansion feature:
$ var=$(sed '2q;d' file)
$ echo "$var"
this is second line
add a comment
|
sed '2q;d' file
prints the second line in file
to the terminal.
To populate a variable with it, use bash's command expansion feature:
$ var=$(sed '2q;d' file)
$ echo "$var"
this is second line
sed '2q;d' file
prints the second line in file
to the terminal.
To populate a variable with it, use bash's command expansion feature:
$ var=$(sed '2q;d' file)
$ echo "$var"
this is second line
edited Mar 28 at 15:16
answered Mar 28 at 15:09
oguz ismailoguz ismail
12.8k7 gold badges19 silver badges37 bronze badges
12.8k7 gold badges19 silver badges37 bronze badges
add a comment
|
add a comment
|
Simple solution using head
and tail
:
a=$(head -2 test.txt | tail -1 )
Saves the second line of test.txt to the variable $a
.
add a comment
|
Simple solution using head
and tail
:
a=$(head -2 test.txt | tail -1 )
Saves the second line of test.txt to the variable $a
.
add a comment
|
Simple solution using head
and tail
:
a=$(head -2 test.txt | tail -1 )
Saves the second line of test.txt to the variable $a
.
Simple solution using head
and tail
:
a=$(head -2 test.txt | tail -1 )
Saves the second line of test.txt to the variable $a
.
answered Mar 28 at 15:58
mttpgnmttpgn
2282 silver badges11 bronze badges
2282 silver badges11 bronze badges
add a comment
|
add a comment
|
You can also write that sed as
sed -n '2p;q'
which I find (a little) more obvious.– glenn jackman
Mar 28 at 15:58