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
Extracting records with unique fields from a fixed width txt file sitney Shell Programming and Scripting 8 02-10-2008 03:18 AM
Searching and extracting text from output sjday Shell Programming and Scripting 3 12-07-2007 04:12 AM
AWK Merge Fields for Print Output RacerX Shell Programming and Scripting 5 10-22-2007 08:31 AM
extracting fields prvnrk Shell Programming and Scripting 2 10-08-2007 03:39 AM
Extracting information from text fields. spindoctor UNIX for Dummies Questions & Answers 24 06-09-2007 01:17 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-19-2006
csaha's Avatar
csaha csaha is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 81
Lightbulb Extracting fields from an output 8-)

I am getting a variable as x=2006/01/18

now I have to extract each field from it.
Like x1=2006, x2=01 and x3=18.

Any idea how?

Thanks a lot for help.

Thanks
CSaha
  #2 (permalink)  
Old 01-19-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Code:
[/tmp]$ echo "2006/01/18" | { IFS="/" read a b c; echo $a $b $c ; }
2006 01 18
  #3 (permalink)  
Old 01-19-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
On a deeper note, I think I know where you are heading to...

If that solution does not work, pick up some clues from this thread Breaking input with "read" command
  #4 (permalink)  
Old 01-19-2006
csaha's Avatar
csaha csaha is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 81
Excellent Vino !!!

After posting I tried this and resolved

$ echo "2006/01/18" | awk -F"/" '{print $1}'
2006

$ echo "2006/01/18" | awk -F"/" '{print $2}'
01

$ echo "2006/01/18" | awk -F"/" '{print $3}'
18

See this also ...
  #5 (permalink)  
Old 01-19-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
here is one more in sed,

Code:
echo 2006/01/18 | sed 's/\(.*\)\/\(.*\)\/\(.*\)/year: \1 month: \2 day: \3/'
year: 2006 month: 01 day: 18
  #6 (permalink)  
Old 01-20-2006
aigles's Avatar
aigles aigles is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,417
You can also use 'cut'

year=`echo 2006/01/18 | cut -d/ -f1`
month=`echo 2006/01/18 | cut -d/ -f2`
day=`echo 2006/01/18 | cut -d/ -f3`


But, i think tah the better solution is Vino post

echo 2006/01/18 | IFS=/ read year month day


Jean-Pierre.
  #7 (permalink)  
Old 01-19-2006
mona's Avatar
mona mona is offline
Registered User
  
 

Join Date: Nov 2005
Location: Singapore
Posts: 96
Code:
echo "2006/01/18" | awk -F"/" '{print "x1="$1",x2="$2 ",x3=" $3}'
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 09:05 AM.


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