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
extract field of characters after a specific pattern - using UNIX shell script jansat HP-UX 2 05-28-2008 12:08 AM
need to extract field of characters in a line jansat Shell Programming and Scripting 3 05-22-2008 09:44 AM
How to extract elements in a field using a number ahjiefreak Shell Programming and Scripting 17 12-13-2007 04:13 AM
Extract Part of string from 3rd field $3 using AWK stakuri Shell Programming and Scripting 4 10-10-2007 12:28 PM
Trying to extract a field from /etc/passwd file.. xBuRnTx UNIX for Dummies Questions & Answers 2 10-05-2005 02:45 AM

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-2008
pp56825 pp56825 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 32
How to extract field from variable more efective

Hi,
I know that simple way to do this is var=`echo $line | awk '{print $5}'`
But maybe there is shorter way to do this ?

Also maybe there is some way to assign all fileds from line separated by lets say ":" to some variables
instead of
var1=`echo $line | awk '{print $1}'`
var2=`echo $line | awk '{print $2}'`
.....

Peter

Last edited by pp56825; 01-19-2008 at 05:47 AM..
  #2 (permalink)  
Old 01-19-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,533
Code:
set -- $line
echo $1 $2 ...
  #3 (permalink)  
Old 01-19-2008
pp56825 pp56825 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 32
Quote:
Originally Posted by ghostdog74 View Post
Code:
set -- $line
echo $1 $2 ...
What if the fileds in line are separated by : or ~ ?

found that i can export IFS=":".

Ghostdog thanks for this set trick.

Last edited by pp56825; 01-19-2008 at 06:22 AM..
  #4 (permalink)  
Old 01-19-2008
JyotiC JyotiC is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 3
You can also use "cut".
  #5 (permalink)  
Old 01-19-2008
pp56825 pp56825 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 32
Maybe somebody know some page or forum with shell scripting optimalization ? or some tips and triks
  #6 (permalink)  
Old 01-19-2008
pp56825 pp56825 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 32
this is very usefull:

file:
sda:asd:asd
asd:awq:qwe
das:wer:tyu
asd:wer:123
asd:234:456

export IFS=":"
while read a b c;do echo $a $b $c;done < test
  #7 (permalink)  
Old 01-19-2008
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,866
Quote:
Originally Posted by pp56825 View Post
What if the fileds in line are separated by : or ~ ?

found that i can export IFS=":".
[...]
With ksh93 and bash you could use something like this:

Code:
$ str="one:two-three" oIFS=$IFS IFS=':-' a=($str) IFS=$oIFS
$ echo ${a[0]}
one
$ echo ${a[1]}
two
$ echo ${a[2]}
three
With zsh:

Code:
% str=one:two:three
% a=(${(s.:.)str})   
% echo $a[1]
one
% echo $a[2]
two
% str=one-two-three
% a=(${(s.-.)str})   
% echo $a[3]         
three
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:32 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