If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > how can pick a field stored in a file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-17-07, 03:25
jaganmohan.d jaganmohan.d is offline
Registered User
 
Join Date: May 2007
Location: HONKKONG
Posts: 2
how can pick a field stored in a file

i have a sequence of words seperated by delimiter i want to pick a field . How can i pick that?
Reply With Quote
  #2 (permalink)  
Old 05-17-07, 06:19
shiya shiya is offline
Registered User
 
Join Date: Mar 2007
Posts: 29
Hi Jagan,
Try using cut command. I hope this will be useful in your case.
Eg: cut -d: -f1,3 filename

After the -d option you can specify the delimiter(here i have mentioned as colon) and then in -f option you can mention the number of fields(here i have taken the first three fields).

Regards,
Shiya
Reply With Quote
  #3 (permalink)  
Old 05-17-07, 14:56
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Hi,

Another option is to use awk. With the -F flag you can define a different delimiter from the default space and tab. Like this:
Code:
awk -F ":" '{print $0}' file, or: 
echo sequenceofwords | awk -F ":" '{print $1, $2, $3}'
Like Shiya I specified the colon as delimiter and in the first case every complete line ($0) from the inputfile is written to output. In the second case the output from a command is piped into awk and only the first three fields are written to output.

Regards
Reply With Quote
  #4 (permalink)  
Old 05-17-07, 23:01
jaganmohan.d jaganmohan.d is offline
Registered User
 
Join Date: May 2007
Location: HONKKONG
Posts: 2
Thanx alot...

Thanx for your reply, but if the delimiter is only single Space!!!
Reply With Quote
  #5 (permalink)  
Old 05-18-07, 05:05
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
With awk it's easy to use a space/several spaces as delimiter cause that's the default delimiter. So with the previous example it would be:
Code:
echo sequenceofwords| awk '{print $1, $2, $3}'
(Note that if the sequenceofwords is a string of three fields separated by a space the output of this awk command will be exactly like the input. This is because of the comma's in the print statement, they separate the outputfields by spaces. Without the comma's the three outputfields will be concatenated.)

Regards
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On