cut -f (or awk alternative)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cut -f (or awk alternative)
# 1  
Old 03-01-2005
Question cut -f (or awk alternative)

I'm trying the following as i wish to get the last field in a report containing several fields, each line can have more fields than the previous, so I tried

rvwords=`echo $rvstatus |wc -w`
rvstat=`echo $rvstatus |cut -f $rvwords`

but it prints the whole variable contents of $rvstatus rather than just the seventh field, there are no delimeters (other than spaces / tabs) in the line, which would read along the lines of

0 1111 my file 102.00 1003.00 2.99
1 2222 my own file 111.00 112.98 200.10
...

Thanks in anticipation
# 2  
Old 03-01-2005
rvstat=`echo $rvstatus | nawk '{print $NF}'`

or with sed:

rvstat=`echo $rvstatus | sed -e 's/.* \([^ ][^ ]*\)[ ]*$/\1/'`

Last edited by vgersh99; 03-01-2005 at 02:39 PM..
# 3  
Old 03-01-2005
Try this ....

Code:
$ echo "0 1111 my file 102.00 1003.00 2.99" | cut -d" " -f 7
2.99

Try this if we have more than a one embedded space between the fields

Code:
$ echo "0 1111 my file 102.00 1003.00 2.99" | tr -s " " | cut -d" " -f 7
2.99

# 4  
Old 03-02-2005
Try as with awk as,

Code:
awk ' { print $NF }' <filename>

or 

sed 's/.*[ ]\(.*\)$/\1/' <filename>

IT will do too.
# 5  
Old 03-02-2005
Quote:
Originally Posted by vgersh99
rvstat=`echo $rvstatus | nawk '{print $NF}'`

or with sed:

rvstat=`echo $rvstatus | sed -e 's/.* \([^ ][^ ]*\)[ ]*$/\1/'`
Thanks the awk method works fine, however this now raises another issue, how about if I now wanted to print the field fourth and/or fifth from the right ?
# 6  
Old 03-02-2005
Quote:
Originally Posted by gefa
Thanks the awk method works fine, however this now raises another issue, how about if I now wanted to print the field fourth and/or fifth from the right ?
Code:
# 4th AND 5th from last
rvstat=`echo $rvstatus | nawk '{print $(NF-4), $(NF-5)}'`

# 4th from last
rvstat=`echo $rvstatus | nawk '{print $(NF-4)}'`

# 7  
Old 03-02-2005
Quote:
Originally Posted by vgersh99
Code:
# 4th AND 5th from last
rvstat=`echo $rvstatus | nawk '{print $(NF-4), $(NF-5)}'`

# 4th from last
rvstat=`echo $rvstatus | nawk '{print $(NF-4)}'`

Thanks again, that works as required, I had tried something similar but missed off the brackets !.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tip: alternative for NR==FNR in awk

Example: $ cat file1 2 3$ cat file2 1 2 3 4 5 6The following awk script works like a charm, NR==FNR is true for file1, the remainder runs for file2: awk ' NR==FNR {A; next} ($1 in A) ' file1 file2 2 3Now have an empty file1: >file1and run the awk script again. The result is empty... (8 Replies)
Discussion started by: MadeInGermany
8 Replies

2. Shell Programming and Scripting

Making a faster alternative to a slow awk command

Hi, I have a large number of input files with two columns of numbers. For example: 83 1453 99 3255 99 8482 99 7372 83 175 I only wish to retain lines where the numbers fullfil two requirements. E.g: =83 1000<=<=2000 To do this I use the following... (10 Replies)
Discussion started by: s052866
10 Replies

3. Shell Programming and Scripting

Want to use awk instead of cut

I want to use awk instead of cut command. Following is my code: line="slNumber=US78AJF11643, slName=USJFKAAUSYDAAUL80441032900095, dummy sl found? sqlca.sqlcode=0" sl_WORD=`echo $line| cut -f 1 -d','` sl=`echo $sl_WORD | cut -f 2 -d'='` echo "$sl" Please suggest me about the code. ... (5 Replies)
Discussion started by: rinku
5 Replies

4. Shell Programming and Scripting

HELP! using cut/awk

how would i write a shell script to show the number of lines in which int variable appears in a c++ program. how would i do this using cut or awk methods is it possbile and having a output similar to this x, number of apperances = y, number of apperances = (2 Replies)
Discussion started by: deadleg
2 Replies

5. UNIX for Dummies Questions & Answers

Help please awk or cut

Hi I'm new to unix programming so struggling with something thats probably simple to many of you I have data files of the format : ID, date, value1, value2, blank on each line either value1 or value2 will be zero. I need my output file to contain ID, date, non-zero value The input... (3 Replies)
Discussion started by: thewench
3 Replies

6. Shell Programming and Scripting

Is awk vs cut which one is better

i was trying to work on program to look for users never log on sever.. using awk with awk is working last| awk '{print $1}' |sort -u > /tmp/users1$$ cat /etc/passwd | awk -F: '{print $1}' |sort -u > /tmp/users2$$ comm -13 /tmp/users$$ rm -f /tmp/users$$ with cut it is not working ... (3 Replies)
Discussion started by: macrules
3 Replies

7. Shell Programming and Scripting

awk or cut

select some fields from data file (source.csv) The data in file(source.csv) is like "x1,2",,"y",,"z" How to get the 1st, 2nd and 3rd field from the file. Using awk or cut? Note: "x1,2" is one field. thanks, (16 Replies)
Discussion started by: anypager
16 Replies

8. Shell Programming and Scripting

[grep awk cut] > awk

Hi, I'm very new to scripting. grep $s $filename | awk '{print $2}' | cut -c 1-8 How can I optimize this using a single awk? I tried: awk '/$s/ {print $2}' $filename | cut -c 1-8 However didn't work, I think the awk is not recognizing $s and the verbal is something else. (6 Replies)
Discussion started by: firdousamir
6 Replies

9. Shell Programming and Scripting

Using awk (or an alternative)

Hi, I'm trying to echo a variable that has values separated by colons, putting each value on a new line. So as an example, a variable I might have would contain: My name is Earl:My name is Dorothy:My name is Bernard: And I want the output: My name is Earl My name is Dorothy My name... (5 Replies)
Discussion started by: michaeltravisuk
5 Replies
Login or Register to Ask a Question