The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
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
Pulling a file off a backup tape rocker40 UNIX for Dummies Questions & Answers 14 10-12-2007 12:37 PM
Pulling data and following lines from file MizzGail Shell Programming and Scripting 2 01-31-2006 03:13 PM
pulling the following line from a file peter.herlihy UNIX for Dummies Questions & Answers 4 08-29-2002 10:09 PM
Pulling out fields from a file Saz UNIX for Advanced & Expert Users 2 09-30-2001 04:31 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-08-2003
dangral dangral is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2002
Posts: 699
pulling a column from a file in ksh

I would like to pull a column from a file and place it in a variable:

The file would look like this:

N.Korea gibberish garbage
S.Korea gibberish garbage
USA gibberish garbage
Iraq gibberish garbage
Canada gibberish garbage



and items in the first column would be one word only. I would like to place the whole column in a variable. I think sed would be good for this, but I'm not sure of the syntax.
Thanks
  #2 (permalink)  
Old 01-08-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
How about:
Code:
x=$(cut -d ' ' -f 1 < file)
  #3 (permalink)  
Old 01-08-2003
dangral dangral is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2002
Posts: 699
The file was actually tab-delimited so I ended up using this:

x=$(cut -f1 < file)

which achieved the results I was looking for. Thanks!
  #4 (permalink)  
Old 01-08-2003
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
Exclamation Re: pulling a column from a file in ksh

x=$(cut -d ' ' -f 1 < file)

What shell is syntax from? Is this a form of command grouping used in csh?

or

If this is Korn Shell, why dont you have to use back quotes ( ` ) to execute the commands inside the parens? Are there limitations to this sort of syntax?

Last edited by google; 01-09-2003 at 02:39 PM..
  #5 (permalink)  
Old 01-09-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
This is not csh syntax, it is ksh syntax. Note that the title of this thread explicitly mentions ksh. In ksh, the $(command) is preferred, while the `command` syntax is currently supported for backward compatibility.

The advantage is nesting:
gcos=$(grep $(whoami) /etc/passwd | cut -d: -f5)
is easy to code and easy to read. Depending on the contents of the gcos field:
eval gcos=\`grep `whoami` /etc/passwd \| cut -d: -f5 \'
may or may not work.
  #6 (permalink)  
Old 01-09-2003
dangral dangral is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2002
Posts: 699
Well, here's where it gets tricky:

It turns out that I do need to use the second column. The file would now look like this:

N.Korea NUCLEAR garbage
S.Korea NON-NUCLEAR garbage
USA NUCLEAR garbage
Iraq UNKNOWN garbage
Canada HORSEBACK garbage


(By the way, the second column is a joke, for those who don't get my sense of humor)

But what I need to do is pull out the second column and place it in a separate variable, but in a way that I can still associate it with the first column.
  #7 (permalink)  
Old 01-09-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
How about:
Code:
y=$(cut -f 2 < file)
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0