Print nth column of pattern

How to print nth column of a pattern/file without using awk,cut commands?

Questions by poi677   answers by poi677

Showing Answers 1 - 13 of 13 Answers


  • May 2nd, 2012

cat file_name | awk {print $n} # where N is the column no. and tab is field seperator

cat SO_02052012_2.csv | awk -F; {print $n} # where N is is column no. and semicolon is field seperator

  Was this answer useful?  Yes


  • Jul 5th, 2012

awk {print $n} < filename

where n is the field number

  Was this answer useful?  Yes


  • Aug 2nd, 2012

Question is without using awk commands, any idea ??????

  Was this answer useful?  Yes

Sijeesh KT

  • Aug 18th, 2012

perl -ane print "$F[0]"; < test.txt

You can use this I hope , replace the zero with the column number you want to print.Normally people use cut and awk command for extracting column.

  Was this answer useful?  Yes


  • Oct 30th, 2012

i think by modifying the IFS, we can achieve this as follows, i hope this helps.

#take for example there is an entry in /etc/password with this info

#let me extract my 6th column, ie, my home directory (/home/santarvedi) using the following mode.

santarvedi]$ bk_IFS=$IFS
santarvedi]$ IFS=:
santarvedi]$ set -- $(getent passwd santarvedi)
santarvedi]$ echo $6
santarvedi]$ IFS="$bk_IFS"

  Was this answer useful?  Yes

Saurabh Mishra

  • Jun 3rd, 2014

  1. echo "enter the line which is need to fetch"

  2. read n

  3. i=1

  4. while [$i -eq $n]

  5. do

  6. a=`cat file |cut -d " "-f$i`

  7. ((i = i+1))

  8. done

  9. echo $a


  Was this answer useful?  Yes

ajay goel

  • Jun 15th, 2014

grep "pattern" filename | xargs print {$n}

  Was this answer useful?  Yes

Tigran Petrosyan

  • Oct 24th, 2014

  1. filename="input.txt"

  2. echo "enter column number"

  3. read n


  5. len=`tail $filename |wc -l`

  6. echo $len

  7. for ((i=1; $i<$len; i++))

  8. {

  9.  clmn=set $(sed -n "$i"p $filename); echo $"$n"

  10.  eval $clmn

  11. }

  Was this answer useful?  Yes

Tigran Petrosyan

  • Oct 24th, 2014

i=1; while read line; do for word in $line; do if [[ $i -eq 2 ]]; then echo $word; i=1; break; else ((i++)); fi; done; done < input.txt

  Was this answer useful?  Yes


  • Jun 9th, 2017

Read the lines by character and count the number of spaces (delimiter). When the count of space (delimiter) is n-1 (keeping in mind you need nth column), print the characters before next space (delimiter).

  Was this answer useful?  Yes

Praveena Rao

  • Oct 24th, 2017

This one is for pattern and not file:
echo ${var:n:1}

  Was this answer useful?  Yes


  • Mar 1st, 2018

cat sample.txt | grep -o .$

  Was this answer useful?  Yes


  • May 27th, 2018

sed -n np file.txt

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions