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 - 42 of 42 Answers

naresh

  • 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

ardiansyah

  • Jul 5th, 2012
 

awk {print $n} < filename

where n is the field number

  Was this answer useful?  Yes

Rudy

  • 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

santarvedi

  • 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
santarvedi:x:500:500:santarvedi:/home/santarvedi:/bin/bash

#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
 

Code
  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

  10.  

  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
 

Code
  1. filename="input.txt"

  2. echo "enter column number"

  3. read n

  4.  

  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

Nawaz

  • 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:
var=hello
echo ${var:n:1}

  Was this answer useful?  Yes

Sonu

  • Mar 1st, 2018
 

cat sample.txt | grep -o .$

  Was this answer useful?  Yes

Ashok

  • May 27th, 2018
 

sed -n np file.txt

  Was this answer useful?  Yes

Sathish Krishnan Venkatachalam

  • Aug 7th, 2020
 

I hope the below answer, to retrieve 3rd column without using awk (or) cut, use the following command:
sed s/^(.*) (.*) (.*) (.*)$/3/g file.txt
insert or delete "(.*)" based on which column to be retrieved and change "3" to nth column no.

  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