3

This question already has an answer here:

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

marked as duplicate by Anthon, Stéphane Chazelas shell-script 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
13
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:

2

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
1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.