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;








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










share|improve this question














marked as duplicate by Socowi, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

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 as sed -n '2p;q' which I find (a little) more obvious.

    – glenn jackman
    Mar 28 at 15:58

















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










share|improve this question














marked as duplicate by Socowi, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

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 as sed -n '2p;q' which I find (a little) more obvious.

    – glenn jackman
    Mar 28 at 15:58













0












0








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










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 15:02









NoumanNouman

1093 silver badges13 bronze badges




1093 silver badges13 bronze badges





marked as duplicate by Socowi, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

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 bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

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 bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

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 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
















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












2 Answers
2






active

oldest

votes


















1
















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





share|improve this answer


































    0
















    Simple solution using head and tail:



    a=$(head -2 test.txt | tail -1 )


    Saves the second line of test.txt to the variable $a.






    share|improve this answer

































      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1
















      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





      share|improve this answer































        1
















        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





        share|improve this answer





























          1














          1










          1









          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





          share|improve this answer















          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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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


























              0
















              Simple solution using head and tail:



              a=$(head -2 test.txt | tail -1 )


              Saves the second line of test.txt to the variable $a.






              share|improve this answer





























                0
















                Simple solution using head and tail:



                a=$(head -2 test.txt | tail -1 )


                Saves the second line of test.txt to the variable $a.






                share|improve this answer



























                  0














                  0










                  0









                  Simple solution using head and tail:



                  a=$(head -2 test.txt | tail -1 )


                  Saves the second line of test.txt to the variable $a.






                  share|improve this answer













                  Simple solution using head and tail:



                  a=$(head -2 test.txt | tail -1 )


                  Saves the second line of test.txt to the variable $a.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 28 at 15:58









                  mttpgnmttpgn

                  2282 silver badges11 bronze badges




                  2282 silver badges11 bronze badges
















                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript