cut words based on the word count of a line


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users cut words based on the word count of a line
# 1  
Old 02-28-2011
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.

https://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.
# 2  
Old 02-28-2011
Code:
awk '{print $NF}' myFile

# 3  
Old 02-28-2011
Quote:
Originally Posted by vgersh99
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
Quote:
Originally Posted by COKEDUDE
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.
# 5  
Old 02-28-2011
Quote:
Originally Posted by vgersh99
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
# 6  
Old 02-28-2011
Quote:
Originally Posted by COKEDUDE
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?
# 7  
Old 02-28-2011
Quote:
Originally Posted by vgersh99
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. UNIX for Advanced & Expert Users

Sort words based on word count on each line

Hi Folks :) I have a .txt file with thousands of words. I'm trying to sort the lines in order based on number of words per line. Example from: word word word word word word word word word word word word word word word word to desired output: word (2 Replies)
Discussion started by: martinsmith
2 Replies

3. Shell Programming and Scripting

Count and print all repeating words in a line

Gurus, I have a file containing lines like this : Now, number of words in each line varies. My need is, if a word repeats in a line get it printed. Also total number of repeats. So, the output would be : Any help would be highly appreciated. Thanks & Regards (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

4. Shell Programming and Scripting

How to remove all words from a matching word in a line?

Hi Guys, :p I have a file like this: 2010-04-25 00:00:30,095 INFO - ]- start process U100M4 2010-04-25 00:00:30,096 DEBUG - ] -- call EJB 2010-04-25 00:00:30,709 INFO - - end processU100M4 2010-04-25 00:00:30,710 DEBUG - got message=Sorry I want to out put format. 2010-04-25... (5 Replies)
Discussion started by: ooilinlove
5 Replies

5. Shell Programming and Scripting

How to remove all words starting from a matching word in a line

Hi Guys, I have a file like this: wwwe 1 ioie ewew yyy uuu 88 erehrlk 4 ihoiwhe lkjhassad lkhsad yyy mmm 45 jhash lhasdhs lkhsdkjsn ouiyrshroi oihoihswodnw oiyhewe yyy ggg 77 I want to remove everything after "yyy" and including "yyy" from each line in the file. So I want:... (2 Replies)
Discussion started by: npatwardhan
2 Replies

6. Shell Programming and Scripting

How to fetch rows based on line numbers or based on the beginning of a word?

I have a file which will have rows like shown below, ST*820*316054716 RMR*IV*11333331009*PO*40.31 REF*IV*22234441009*xsss471-2762 DTM*003*091016 ENT*000006 RMR*IV*2222234444*PO*239.91 REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN> DTM*003* 091016 RMR*IV*2223344441009*PO*40.31... (18 Replies)
Discussion started by: Muthuraj K
18 Replies

7. Shell Programming and Scripting

Count words on each line in file using xargs

Hi, im having a problem with xargs, i want to cout word of each line in file, and i HAVE to use xargs, i tried: cat file | xargs wc -w .....that uses all words in file like name of files and passed then to wc so it worte wc :somewordformfile is not i afile or directory cat file | xargs -I{} wc... (3 Replies)
Discussion started by: Qwetek
3 Replies

8. Shell Programming and Scripting

count no of words in a line

hi i have a line "abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc" I want to print the no of words, words separated by comma please help (3 Replies)
Discussion started by: Satyak
3 Replies

9. Shell Programming and Scripting

count no of words in a line

hi i have a string like str=abc def ghi jkl now i want to count the no of words in the string please help (7 Replies)
Discussion started by: satish@123
7 Replies

10. Shell Programming and Scripting

cut last line of a word

Hi all, want to delete a crahecter from each line..? how can i do it... (2 Replies)
Discussion started by: bullz26
2 Replies
Login or Register to Ask a Question