Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Search Forums:



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-28-2011
Registered User
 

Join Date: Feb 2010
Posts: 304
Thanks: 21
Thanked 8 Times in 8 Posts
cut words based on the word count of a line

I would like to cut words based on the word count of a line. This over here inspired me with some ideas but I wasn't able to get what I needed.

http://www.unix.com/shell-programmin...ing-xargs.html

If the line has 6 words I would like to use this.

Code:
awk -F" " '{print $6}'

If the line has 4 words I would like to use this.

Code:
awk -F" " '{print $4}'

I don't want to use a shellsript. I would like this to be just one line that I can put into my shell and it work.
Sponsored Links
    #2  
Old 02-28-2011
vgersh99's Avatar
ɹoʇɐɹǝpoɯ
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 6,959
Thanks: 69
Thanked 351 Times in 338 Posts

Code:
awk '{print $NF}' myFile

Sponsored Links
    #3  
Old 02-28-2011
Registered User
 

Join Date: Feb 2010
Posts: 304
Thanks: 21
Thanked 8 Times in 8 Posts
Quote:
Originally Posted by vgersh99 View Post
Code:
awk '{print $NF}' myFile

I saw that from the link I gave. How do I cut the 4th word and 6th word based on word count.
    #4  
Old 02-28-2011
vgersh99's Avatar
ɹoʇɐɹǝpoɯ
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 6,959
Thanks: 69
Thanked 351 Times in 338 Posts
Quote:
Originally Posted by COKEDUDE View Post
I saw that from the link I gave. How do I cut the 4th word and 6th word based on word count.
This will return the last 'word' from the record/line.
Isn't that what your examples illustrate?

When you say 'cut words' what do you example mean?
Please provide a sample input a desired output.
Sponsored Links
    #5  
Old 02-28-2011
Registered User
 

Join Date: Feb 2010
Posts: 304
Thanks: 21
Thanked 8 Times in 8 Posts
Quote:
Originally Posted by vgersh99 View Post
This will return the last 'word' from the record/line.
Isn't that what your examples illustrate?

When you say 'cut words' what do you example mean?
Please provide a sample input a desired output.
I have several lines similar to this. The first one if you count it you can see has 6 words and the second one has 4 words. On the lines that have 6 words I would like to cut the 6th word and output it to my screen and on the lines that have 4 words I would like to cut the 4th word and output it to my screen.

Quote:
mon tues wed thur fri sat
mon tues wed thur
Output would be
sat
thur
Sponsored Links
    #6  
Old 02-28-2011
vgersh99's Avatar
ɹoʇɐɹǝpoɯ
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 6,959
Thanks: 69
Thanked 351 Times in 338 Posts
Quote:
Originally Posted by COKEDUDE View Post
I have several lines similar to this. The first one if you count it you can see has 6 words and the second one has 4 words. On the lines that have 6 words I would like to cut the 6th word and output it to my screen and on the lines that have 4 words I would like to cut the 4th word and output it to my screen.



Output would be
sat
thur
Then what I've posted should give you a desired result.
Does it not?
Sponsored Links
    #7  
Old 02-28-2011
Registered User
 

Join Date: Feb 2010
Posts: 304
Thanks: 21
Thanked 8 Times in 8 Posts
Quote:
Originally Posted by vgersh99 View Post
Then what I've posted should give you a desired result.
Does it not?
No. I was trying to keep my example simple. And what you gave just cuts the last word. This is the full line of text.

Quote:
mon tues wed thur fri sat This is a 6 day week
mon tues wed thur This is a 4 day week
Output would be
sat
thur
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Count and print all repeating words in a line AshwaniSharma09 Shell Programming and Scripting 5 10-13-2010 07:24 AM
How to fetch rows based on line numbers or based on the beginning of a word? Muthuraj K Shell Programming and Scripting 18 02-02-2010 04:27 AM
Count words on each line in file using xargs Qwetek Shell Programming and Scripting 3 03-28-2009 06:21 PM
count no of words in a line Satyak Shell Programming and Scripting 3 09-26-2008 11:52 AM
count no of words in a line satish@123 Shell Programming and Scripting 7 05-21-2008 02:59 AM



All times are GMT -4. The time now is 03:03 AM.