How to get the first column from the txt file using unix command? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to get the first column from the txt file using unix command?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-08-2009
psiva_arul's Avatar
psiva_arul psiva_arul is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 19 November 2009, 6:58 AM EST
Location: Bangalore, India
Posts: 97
Thanks: 0
Thanked 1 Time in 1 Post
How to get the first column from the txt file using unix command?

Hi All,

I have the file like this (file name is : tem_text)

no Id name ccy
------- ---- ------------------- --------
7777 17 India Overseas Partners 500INR

I want to retreive the third colimn of from the above text file
if i use the basic awk command
cat tem_text | awk '{ print $3 }'
it will return only the
name
------
India
insetead of return the all details from the third column

name
---------------------
India Overseas Partners

Please advice me on this

Thanks
MPS
Sponsored Links
    #2  
Old 01-08-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 27 August 2014, 3:33 PM EDT
Location: St. Gallen, Switzerland
Posts: 6,226
Thanks: 121
Thanked 450 Times in 410 Posts
$3 is field 3 not row 3. Also no cat is needed.


Code:
awk 'NR == 3 {print}' tem_text

or shorter in sed

Code:
sed '3!d' tem_text

Sponsored Links
    #3  
Old 01-08-2009
Patagonicus Patagonicus is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 18 December 2009, 6:00 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
I think he wants the third column, not the third row, so $3 is right, but the third column has spaces in it. You can use this:

Code:
awk '{ sub("^[^ ]* [^ ]* ", "") ; sub(" [^ ]*$", "") ; print }'

The first sub() deletes the first two columns ([^ ]* => everything except a space; as long as possible, then a space and the whole thing again). The second sub() deletes the last column and everything in between (=> third column) is printed.
    #4  
Old 01-08-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 27 August 2014, 3:33 PM EDT
Location: St. Gallen, Switzerland
Posts: 6,226
Thanks: 121
Thanked 450 Times in 410 Posts
Oh, you are right of course.

sed:

Code:
sed 's/^[^ ]* [^ ]* \(.*\) [^ ]\{1,\}/\1/' tem_text


Last edited by zaxxon; 01-08-2009 at 09:39 AM..
Sponsored Links
    #5  
Old 01-08-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 27 August 2014, 5:29 AM EDT
Location: /dev/ph
Posts: 4,786
Thanks: 63
Thanked 424 Times in 393 Posts
Or you can use sed

Code:
$ sed -e 's/\([^ ]*\) \([^ ]*\) \(.*\) \(.*$\)/\3/' tem_text
India Overseas Partners

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 05:27 AM
4 column tsv file, output 1 specific column casphar Shell Programming and Scripting 6 11-12-2008 03:04 PM
To cut entire column from a file and apend it to another file as another column sakthifire Shell Programming and Scripting 4 06-25-2008 04:27 AM
Replacing column with column of another file manneni prakash UNIX for Advanced & Expert Users 1 06-24-2008 11:27 PM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM



All times are GMT -4. The time now is 07:01 PM.