How to extract field from variable more efective


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract field from variable more efective
# 1  
Old 01-19-2008
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  
Old 01-19-2008
Code:
set -- $line
echo $1 $2 ...

# 3  
Old 01-19-2008
Quote:
Originally Posted by ghostdog74
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  
Old 01-19-2008
You can also use "cut".
# 5  
Old 01-19-2008
Maybe somebody know some page or forum with shell scripting optimalization ? or some tips and triks
# 6  
Old 01-19-2008
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  
Old 01-19-2008
Quote:
Originally Posted by pp56825
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Ps output field extract

This is the output from ps -ef cmd . I have to extract the fourth (C) and the seventh (TIME) field root 3932344 3801216 Apr 08 - 0:00 /usr/sbin/rsct/bin/ERrmd root 3997836 1 0 Apr 08 - 0:00 /usr/sbin/uprintfd root 4128894 3801216 0 Apr 08 - 0:09... (3 Replies)
Discussion started by: Anu_1
3 Replies

2. Shell Programming and Scripting

Extract lines whose third field is 0

Hi, I have a file with colon separated values like below. How can i get those lines whose third field is 0 (zero). In the below example, lines starting with stapler and tempo has its third field as 0 $ cat list.txt galaxy:b:5:world stapler:a:0:hello abc:a:4:stomper kepler:uic:5:jam... (8 Replies)
Discussion started by: John K
8 Replies

3. Shell Programming and Scripting

Field extract w/o - and adding formats

I have a txt file like below: Accession Orderable Unique ID Subject ID Timepoint Colletion Date/Time 12-170-0185 Glucose 1756487 1 D-07_H00.03 18-JUN-2012 06:03:00 12-170-0185 Insulin 1756487 1 D-07_H00.03 18-JUN-2012 06:03:00 12-170-0200 Glucose 1756487 1 D-07_H00.05... (9 Replies)
Discussion started by: Daniel Gate
9 Replies

4. Shell Programming and Scripting

Extract Field from a file

I have an input file with content like : 18:51:18 | 217863|Acct 0110855565|RC 17608| 16 Subs| 1596 UsgRecs| 2 Secs| 430 CPUms| prmis2:26213 <MoveUsage d aemon needs to run on this account before it can be billed.> 23:03:30 | 896529|Acct 2063947620|RC 17608| 8 Subs| 148 UsgRecs| ... (9 Replies)
Discussion started by: Rajesh Putnala
9 Replies

5. Shell Programming and Scripting

How to extract a field from ls-l command and display?

So I want to put a line at the end of my script which greps for keywords from syslog.log that outputs the following after it is done: "This file was last modified on (thisdate)" I know I can use the following to get the date: rtidsvb(izivanov):/home/izivanov> ll /var/adm/syslog/syslog.log ... (4 Replies)
Discussion started by: zixzix01
4 Replies

6. UNIX for Dummies Questions & Answers

Extract a certain field from a CSV?

EDIT: This problem has been solved thanks to the help of scottn. Okay, so I have a CSV. Let's say it has the following entries in it: Jackie Chan,1954,M Chuck Norris,1930,M Bruce Lee,1940,M How would I, for example, extract the gender out of a certain person, maybe based on the year of... (12 Replies)
Discussion started by: chickeneaterguy
12 Replies

7. UNIX Desktop Questions & Answers

Extract third field of third line

Hi, how do I extract the third field of the first line? I know how to get the first line or the third field of a file, but I can't get the single entry. awk 'NR==1' file.txt awk '{print $3}' file.txt Please tell me how to combine these. And how do I set this value into a variable? ... (1 Reply)
Discussion started by: etownbetty
1 Replies

8. Shell Programming and Scripting

how to extract last occurence of the field

path = /first/second/third/fourth. i want to extract /first/second/third from path.my program something like this .... path=/first/second/third/fourth noc=`echo $path|tr '/' '\n'|wc -w` var='echo $noc|cut -d'/' -f 1-$noc --> is giving error. why $noc is not working here.any other... (3 Replies)
Discussion started by: kcp_pavan
3 Replies

9. UNIX for Dummies Questions & Answers

extract a field by logic

below are the contents of file 'tmp.out': 2|34|534| 1|355|54| 1|443|34| 3|43|768| 3|7|887| 1|9|990| How do I extract the 2nd and 3rd columns of file 'tmp.out' only when the first column equals '1'. Note that this file will be huge; atleast 5million+ rows, so I want to avoid looping... (4 Replies)
Discussion started by: ChicagoBlues
4 Replies

10. Shell Programming and Scripting

need to extract field of characters in a line

Hello, Below is my input file's content ( in HP-UX platform ): ABCD120672-B21 1 ABCD142257-002 1 ABCD142257-003 1 ABCD142257-006 1 From the above, I just want to get the field of 13 characters that comes after 'ABCD' i.e '120672-B21'... . Could you please let me know the shell script... (3 Replies)
Discussion started by: jansat
3 Replies
Login or Register to Ask a Question