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

Clash 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
shell-script echo cut
 marked as duplicate by Anthon, Stéphane Chazelas
 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.
add a comment |Â
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
shell-script echo cut
 marked as duplicate by Anthon, Stéphane Chazelas
 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.txtshould 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
add a comment |Â
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
shell-script echo cut
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
shell-script echo cut
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
 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
 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.txtshould 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
add a comment |Â
2
no need to over complicate things,cut -f1 input.txtshould 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
add a comment |Â
 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
add a comment |Â
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
add a comment |Â
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
1
TAB is the default separator forcut, 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
awkautomatically splits on spaces or tabs, so-Fis not needed in this particular case :-)
â Kusalananda
Sep 27 '17 at 7:08
@Kusalanandaawktakes any space by default, soabc defwill 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
 |Â
show 1 more comment
 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
add a comment |Â
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
add a comment |Â
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
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
edited Sep 26 '17 at 13:56
answered Sep 26 '17 at 13:38
Kusalananda
106k14209327
106k14209327
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Sep 26 '17 at 17:24
Barmar
6,7401122
6,7401122
add a comment |Â
add a comment |Â
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
1
TAB is the default separator forcut, 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
awkautomatically splits on spaces or tabs, so-Fis not needed in this particular case :-)
â Kusalananda
Sep 27 '17 at 7:08
@Kusalanandaawktakes any space by default, soabc defwill 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
 |Â
show 1 more comment
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
1
TAB is the default separator forcut, 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
awkautomatically splits on spaces or tabs, so-Fis not needed in this particular case :-)
â Kusalananda
Sep 27 '17 at 7:08
@Kusalanandaawktakes any space by default, soabc defwill 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
 |Â
show 1 more comment
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
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
edited Sep 27 '17 at 6:59
answered Sep 26 '17 at 15:31
rexkogitans
343110
343110
1
TAB is the default separator forcut, 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
awkautomatically splits on spaces or tabs, so-Fis not needed in this particular case :-)
â Kusalananda
Sep 27 '17 at 7:08
@Kusalanandaawktakes any space by default, soabc defwill 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
 |Â
show 1 more comment
1
TAB is the default separator forcut, 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
awkautomatically splits on spaces or tabs, so-Fis not needed in this particular case :-)
â Kusalananda
Sep 27 '17 at 7:08
@Kusalanandaawktakes any space by default, soabc defwill 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
 |Â
show 1 more comment
2
no need to over complicate things,
cut -f1 input.txtshould 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