Using tab delimiter in Cut in Unix Shell Scripting [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite













This question already has an answer here:



  • How to define 'tab' delimiter with 'cut' in BASH?

    4 answers



I have file input.txt with below data in TAB delimited format-



23776112 Inactive Active
23415312 Inactive Active


As per requirement, Inside the while loop, I want to use cut 1st column's data and print it -



Below is the part of code-



......
......
while read line
do
SN=`echo $line|cut -d ' ' -f1`
echo $SN
done < input.txt
....
....


To use the tab as delimiter above, I am using Ctrl V Tab



But the output is not as expected.I am getting O/P-



23776112 Inactive Active
23415312 Inactive Active


Whereas I want O/P like -




23776112 
23415312










share|improve this question















marked as duplicate by Anthon, Stéphane Chazelas shell-script
Users with the  shell-script badge can single-handedly close shell-script 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();

);
);
);
Sep 26 '17 at 21:18


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.










  • 2




    no need to over complicate things, cut -f1 input.txt should be enough
    – RomanPerekhrest
    Sep 26 '17 at 13:40











  • @Q23 Note that the accepted answer there is far too complicated.
    – Kusalananda
    Sep 26 '17 at 13:46










  • Yes, It is working, but I need to use it inside while loop as per complete program requirement.
    – Praveen Verma
    Sep 26 '17 at 14:00






  • 1




    @PraveenVerma This is seldom needed. How do you need to use it? What do you want to do with it. It is quite possible that it's doable without a fragile shell loop.
    – Kusalananda
    Sep 26 '17 at 14:04















up vote
2
down vote

favorite













This question already has an answer here:



  • How to define 'tab' delimiter with 'cut' in BASH?

    4 answers



I have file input.txt with below data in TAB delimited format-



23776112 Inactive Active
23415312 Inactive Active


As per requirement, Inside the while loop, I want to use cut 1st column's data and print it -



Below is the part of code-



......
......
while read line
do
SN=`echo $line|cut -d ' ' -f1`
echo $SN
done < input.txt
....
....


To use the tab as delimiter above, I am using Ctrl V Tab



But the output is not as expected.I am getting O/P-



23776112 Inactive Active
23415312 Inactive Active


Whereas I want O/P like -




23776112 
23415312










share|improve this question















marked as duplicate by Anthon, Stéphane Chazelas shell-script
Users with the  shell-script badge can single-handedly close shell-script 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();

);
);
);
Sep 26 '17 at 21:18


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.










  • 2




    no need to over complicate things, cut -f1 input.txt should be enough
    – RomanPerekhrest
    Sep 26 '17 at 13:40











  • @Q23 Note that the accepted answer there is far too complicated.
    – Kusalananda
    Sep 26 '17 at 13:46










  • Yes, It is working, but I need to use it inside while loop as per complete program requirement.
    – Praveen Verma
    Sep 26 '17 at 14:00






  • 1




    @PraveenVerma This is seldom needed. How do you need to use it? What do you want to do with it. It is quite possible that it's doable without a fragile shell loop.
    – Kusalananda
    Sep 26 '17 at 14:04













up vote
2
down vote

favorite









up vote
2
down vote

favorite












This question already has an answer here:



  • How to define 'tab' delimiter with 'cut' in BASH?

    4 answers



I have file input.txt with below data in TAB delimited format-



23776112 Inactive Active
23415312 Inactive Active


As per requirement, Inside the while loop, I want to use cut 1st column's data and print it -



Below is the part of code-



......
......
while read line
do
SN=`echo $line|cut -d ' ' -f1`
echo $SN
done < input.txt
....
....


To use the tab as delimiter above, I am using Ctrl V Tab



But the output is not as expected.I am getting O/P-



23776112 Inactive Active
23415312 Inactive Active


Whereas I want O/P like -




23776112 
23415312










share|improve this question
















This question already has an answer here:



  • How to define 'tab' delimiter with 'cut' in BASH?

    4 answers



I have file input.txt with below data in TAB delimited format-



23776112 Inactive Active
23415312 Inactive Active


As per requirement, Inside the while loop, I want to use cut 1st column's data and print it -



Below is the part of code-



......
......
while read line
do
SN=`echo $line|cut -d ' ' -f1`
echo $SN
done < input.txt
....
....


To use the tab as delimiter above, I am using Ctrl V Tab



But the output is not as expected.I am getting O/P-



23776112 Inactive Active
23415312 Inactive Active


Whereas I want O/P like -




23776112 
23415312





This question already has an answer here:



  • How to define 'tab' delimiter with 'cut' in BASH?

    4 answers







shell-script echo cut






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 26 '17 at 14:08









roaima

40.2k547110




40.2k547110










asked Sep 26 '17 at 13:32









Praveen Verma

2316




2316




marked as duplicate by Anthon, Stéphane Chazelas shell-script
Users with the  shell-script badge can single-handedly close shell-script 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();

);
);
);
Sep 26 '17 at 21:18


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 Anthon, Stéphane Chazelas shell-script
Users with the  shell-script badge can single-handedly close shell-script 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();

);
);
);
Sep 26 '17 at 21:18


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.









  • 2




    no need to over complicate things, cut -f1 input.txt should be enough
    – RomanPerekhrest
    Sep 26 '17 at 13:40











  • @Q23 Note that the accepted answer there is far too complicated.
    – Kusalananda
    Sep 26 '17 at 13:46










  • Yes, It is working, but I need to use it inside while loop as per complete program requirement.
    – Praveen Verma
    Sep 26 '17 at 14:00






  • 1




    @PraveenVerma This is seldom needed. How do you need to use it? What do you want to do with it. It is quite possible that it's doable without a fragile shell loop.
    – Kusalananda
    Sep 26 '17 at 14:04













  • 2




    no need to over complicate things, cut -f1 input.txt should be enough
    – RomanPerekhrest
    Sep 26 '17 at 13:40











  • @Q23 Note that the accepted answer there is far too complicated.
    – Kusalananda
    Sep 26 '17 at 13:46










  • Yes, It is working, but I need to use it inside while loop as per complete program requirement.
    – Praveen Verma
    Sep 26 '17 at 14:00






  • 1




    @PraveenVerma This is seldom needed. How do you need to use it? What do you want to do with it. It is quite possible that it's doable without a fragile shell loop.
    – Kusalananda
    Sep 26 '17 at 14:04








2




2




no need to over complicate things, cut -f1 input.txt should be enough
– RomanPerekhrest
Sep 26 '17 at 13:40





no need to over complicate things, cut -f1 input.txt should be enough
– RomanPerekhrest
Sep 26 '17 at 13:40













@Q23 Note that the accepted answer there is far too complicated.
– Kusalananda
Sep 26 '17 at 13:46




@Q23 Note that the accepted answer there is far too complicated.
– Kusalananda
Sep 26 '17 at 13:46












Yes, It is working, but I need to use it inside while loop as per complete program requirement.
– Praveen Verma
Sep 26 '17 at 14:00




Yes, It is working, but I need to use it inside while loop as per complete program requirement.
– Praveen Verma
Sep 26 '17 at 14:00




1




1




@PraveenVerma This is seldom needed. How do you need to use it? What do you want to do with it. It is quite possible that it's doable without a fragile shell loop.
– Kusalananda
Sep 26 '17 at 14:04





@PraveenVerma This is seldom needed. How do you need to use it? What do you want to do with it. It is quite possible that it's doable without a fragile shell loop.
– Kusalananda
Sep 26 '17 at 14:04











3 Answers
3






active

oldest

votes

















up vote
11
down vote



accepted










cut -f 1 input.txt


This gives you the first column from the tab-delimited file input.txt. The default field delimiter for cut is the tab character, so there's no need to further specify this.



If the delimiter is actually a space, use



cut -d ' ' -f 1 input.txt


If it turns out that there are multiple tabs and/or spaces, use awk:



awk ' print $1 ' input.txt


The shell loop is not necessary for this operation, regardless of whether you use cut or awk.



See also "Why is using a shell loop to process text considered bad practice?".




The reason your script does not work is because the tab disappears when you echo the unquoted variable.



Related:



  • Why is printf better than echo?


  • Security implications of forgetting to quote a variable in bash/POSIX shells






share|improve this answer





























    up vote
    2
    down vote













    Tab is the default separator for cut, you don't need an explicit argument for it.



    However, you need to quote your variable to prevent the tabs from being turned into space.



    SN=`echo "$line"|cut -f1`


    But you can also avoid using cut in the first place. Just set IFS to t.



    IFS=$'t'
    while read -r SN rest
    do
    echo "$SN"
    done < input.txt





    share|improve this answer



























      up vote
      0
      down vote













      bash interprets



      $'t'


      as tabulator, so



      cut -d $'t' -f 1


      is the way to tell cut to use the tabulator as seperator (which, however, is redunant, as it is default). However, I would prefer awk with the same syntax:



      awk -v FS=$'t' ' print $1 ' < input.txt





      share|improve this answer


















      • 1




        TAB is the default separator for cut, you don't need to use -d $'t'
        – Barmar
        Sep 26 '17 at 17:21










      • @Barmar Of course, you are right. I added this as remark.
        – rexkogitans
        Sep 27 '17 at 6:59











      • awk automatically splits on spaces or tabs, so -F is not needed in this particular case :-)
        – Kusalananda
        Sep 27 '17 at 7:08










      • @Kusalananda awk takes any space by default, so abc def will also be split apart, which may not be wanted.
        – rexkogitans
        Sep 27 '17 at 7:27










      • I know. In this particular case, the fields seems to be space free though.
        – Kusalananda
        Sep 27 '17 at 7:30

















      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      11
      down vote



      accepted










      cut -f 1 input.txt


      This gives you the first column from the tab-delimited file input.txt. The default field delimiter for cut is the tab character, so there's no need to further specify this.



      If the delimiter is actually a space, use



      cut -d ' ' -f 1 input.txt


      If it turns out that there are multiple tabs and/or spaces, use awk:



      awk ' print $1 ' input.txt


      The shell loop is not necessary for this operation, regardless of whether you use cut or awk.



      See also "Why is using a shell loop to process text considered bad practice?".




      The reason your script does not work is because the tab disappears when you echo the unquoted variable.



      Related:



      • Why is printf better than echo?


      • Security implications of forgetting to quote a variable in bash/POSIX shells






      share|improve this answer


























        up vote
        11
        down vote



        accepted










        cut -f 1 input.txt


        This gives you the first column from the tab-delimited file input.txt. The default field delimiter for cut is the tab character, so there's no need to further specify this.



        If the delimiter is actually a space, use



        cut -d ' ' -f 1 input.txt


        If it turns out that there are multiple tabs and/or spaces, use awk:



        awk ' print $1 ' input.txt


        The shell loop is not necessary for this operation, regardless of whether you use cut or awk.



        See also "Why is using a shell loop to process text considered bad practice?".




        The reason your script does not work is because the tab disappears when you echo the unquoted variable.



        Related:



        • Why is printf better than echo?


        • Security implications of forgetting to quote a variable in bash/POSIX shells






        share|improve this answer
























          up vote
          11
          down vote



          accepted







          up vote
          11
          down vote



          accepted






          cut -f 1 input.txt


          This gives you the first column from the tab-delimited file input.txt. The default field delimiter for cut is the tab character, so there's no need to further specify this.



          If the delimiter is actually a space, use



          cut -d ' ' -f 1 input.txt


          If it turns out that there are multiple tabs and/or spaces, use awk:



          awk ' print $1 ' input.txt


          The shell loop is not necessary for this operation, regardless of whether you use cut or awk.



          See also "Why is using a shell loop to process text considered bad practice?".




          The reason your script does not work is because the tab disappears when you echo the unquoted variable.



          Related:



          • Why is printf better than echo?


          • Security implications of forgetting to quote a variable in bash/POSIX shells






          share|improve this answer














          cut -f 1 input.txt


          This gives you the first column from the tab-delimited file input.txt. The default field delimiter for cut is the tab character, so there's no need to further specify this.



          If the delimiter is actually a space, use



          cut -d ' ' -f 1 input.txt


          If it turns out that there are multiple tabs and/or spaces, use awk:



          awk ' print $1 ' input.txt


          The shell loop is not necessary for this operation, regardless of whether you use cut or awk.



          See also "Why is using a shell loop to process text considered bad practice?".




          The reason your script does not work is because the tab disappears when you echo the unquoted variable.



          Related:



          • Why is printf better than echo?


          • Security implications of forgetting to quote a variable in bash/POSIX shells







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 26 '17 at 13:56

























          answered Sep 26 '17 at 13:38









          Kusalananda

          106k14209327




          106k14209327






















              up vote
              2
              down vote













              Tab is the default separator for cut, you don't need an explicit argument for it.



              However, you need to quote your variable to prevent the tabs from being turned into space.



              SN=`echo "$line"|cut -f1`


              But you can also avoid using cut in the first place. Just set IFS to t.



              IFS=$'t'
              while read -r SN rest
              do
              echo "$SN"
              done < input.txt





              share|improve this answer
























                up vote
                2
                down vote













                Tab is the default separator for cut, you don't need an explicit argument for it.



                However, you need to quote your variable to prevent the tabs from being turned into space.



                SN=`echo "$line"|cut -f1`


                But you can also avoid using cut in the first place. Just set IFS to t.



                IFS=$'t'
                while read -r SN rest
                do
                echo "$SN"
                done < input.txt





                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Tab is the default separator for cut, you don't need an explicit argument for it.



                  However, you need to quote your variable to prevent the tabs from being turned into space.



                  SN=`echo "$line"|cut -f1`


                  But you can also avoid using cut in the first place. Just set IFS to t.



                  IFS=$'t'
                  while read -r SN rest
                  do
                  echo "$SN"
                  done < input.txt





                  share|improve this answer












                  Tab is the default separator for cut, you don't need an explicit argument for it.



                  However, you need to quote your variable to prevent the tabs from being turned into space.



                  SN=`echo "$line"|cut -f1`


                  But you can also avoid using cut in the first place. Just set IFS to t.



                  IFS=$'t'
                  while read -r SN rest
                  do
                  echo "$SN"
                  done < input.txt






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 26 '17 at 17:24









                  Barmar

                  6,7401122




                  6,7401122




















                      up vote
                      0
                      down vote













                      bash interprets



                      $'t'


                      as tabulator, so



                      cut -d $'t' -f 1


                      is the way to tell cut to use the tabulator as seperator (which, however, is redunant, as it is default). However, I would prefer awk with the same syntax:



                      awk -v FS=$'t' ' print $1 ' < input.txt





                      share|improve this answer


















                      • 1




                        TAB is the default separator for cut, you don't need to use -d $'t'
                        – Barmar
                        Sep 26 '17 at 17:21










                      • @Barmar Of course, you are right. I added this as remark.
                        – rexkogitans
                        Sep 27 '17 at 6:59











                      • awk automatically splits on spaces or tabs, so -F is not needed in this particular case :-)
                        – Kusalananda
                        Sep 27 '17 at 7:08










                      • @Kusalananda awk takes any space by default, so abc def will also be split apart, which may not be wanted.
                        – rexkogitans
                        Sep 27 '17 at 7:27










                      • I know. In this particular case, the fields seems to be space free though.
                        – Kusalananda
                        Sep 27 '17 at 7:30














                      up vote
                      0
                      down vote













                      bash interprets



                      $'t'


                      as tabulator, so



                      cut -d $'t' -f 1


                      is the way to tell cut to use the tabulator as seperator (which, however, is redunant, as it is default). However, I would prefer awk with the same syntax:



                      awk -v FS=$'t' ' print $1 ' < input.txt





                      share|improve this answer


















                      • 1




                        TAB is the default separator for cut, you don't need to use -d $'t'
                        – Barmar
                        Sep 26 '17 at 17:21










                      • @Barmar Of course, you are right. I added this as remark.
                        – rexkogitans
                        Sep 27 '17 at 6:59











                      • awk automatically splits on spaces or tabs, so -F is not needed in this particular case :-)
                        – Kusalananda
                        Sep 27 '17 at 7:08










                      • @Kusalananda awk takes any space by default, so abc def will also be split apart, which may not be wanted.
                        – rexkogitans
                        Sep 27 '17 at 7:27










                      • I know. In this particular case, the fields seems to be space free though.
                        – Kusalananda
                        Sep 27 '17 at 7:30












                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      bash interprets



                      $'t'


                      as tabulator, so



                      cut -d $'t' -f 1


                      is the way to tell cut to use the tabulator as seperator (which, however, is redunant, as it is default). However, I would prefer awk with the same syntax:



                      awk -v FS=$'t' ' print $1 ' < input.txt





                      share|improve this answer














                      bash interprets



                      $'t'


                      as tabulator, so



                      cut -d $'t' -f 1


                      is the way to tell cut to use the tabulator as seperator (which, however, is redunant, as it is default). However, I would prefer awk with the same syntax:



                      awk -v FS=$'t' ' print $1 ' < input.txt






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Sep 27 '17 at 6:59

























                      answered Sep 26 '17 at 15:31









                      rexkogitans

                      343110




                      343110







                      • 1




                        TAB is the default separator for cut, you don't need to use -d $'t'
                        – Barmar
                        Sep 26 '17 at 17:21










                      • @Barmar Of course, you are right. I added this as remark.
                        – rexkogitans
                        Sep 27 '17 at 6:59











                      • awk automatically splits on spaces or tabs, so -F is not needed in this particular case :-)
                        – Kusalananda
                        Sep 27 '17 at 7:08










                      • @Kusalananda awk takes any space by default, so abc def will also be split apart, which may not be wanted.
                        – rexkogitans
                        Sep 27 '17 at 7:27










                      • I know. In this particular case, the fields seems to be space free though.
                        – Kusalananda
                        Sep 27 '17 at 7:30












                      • 1




                        TAB is the default separator for cut, you don't need to use -d $'t'
                        – Barmar
                        Sep 26 '17 at 17:21










                      • @Barmar Of course, you are right. I added this as remark.
                        – rexkogitans
                        Sep 27 '17 at 6:59











                      • awk automatically splits on spaces or tabs, so -F is not needed in this particular case :-)
                        – Kusalananda
                        Sep 27 '17 at 7:08










                      • @Kusalananda awk takes any space by default, so abc def will also be split apart, which may not be wanted.
                        – rexkogitans
                        Sep 27 '17 at 7:27










                      • I know. In this particular case, the fields seems to be space free though.
                        – Kusalananda
                        Sep 27 '17 at 7:30







                      1




                      1




                      TAB is the default separator for cut, you don't need to use -d $'t'
                      – Barmar
                      Sep 26 '17 at 17:21




                      TAB is the default separator for cut, you don't need to use -d $'t'
                      – Barmar
                      Sep 26 '17 at 17:21












                      @Barmar Of course, you are right. I added this as remark.
                      – rexkogitans
                      Sep 27 '17 at 6:59





                      @Barmar Of course, you are right. I added this as remark.
                      – rexkogitans
                      Sep 27 '17 at 6:59













                      awk automatically splits on spaces or tabs, so -F is not needed in this particular case :-)
                      – Kusalananda
                      Sep 27 '17 at 7:08




                      awk automatically splits on spaces or tabs, so -F is not needed in this particular case :-)
                      – Kusalananda
                      Sep 27 '17 at 7:08












                      @Kusalananda awk takes any space by default, so abc def will also be split apart, which may not be wanted.
                      – rexkogitans
                      Sep 27 '17 at 7:27




                      @Kusalananda awk takes any space by default, so abc def will also be split apart, which may not be wanted.
                      – rexkogitans
                      Sep 27 '17 at 7:27












                      I know. In this particular case, the fields seems to be space free though.
                      – Kusalananda
                      Sep 27 '17 at 7:30




                      I know. In this particular case, the fields seems to be space free though.
                      – Kusalananda
                      Sep 27 '17 at 7:30


                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay