Read last word of the line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read last word of the line.
# 1  
Old 04-12-2012
Read last word of the line.

Hi, I need a script to read last word of the line and out put in some temp file.
i
it can contain any word like:

My name is Harry.
or
My zip code is 24490
or it can be
My secret code is 024H

I just need last word of the line (Harry, or 2440 or 024H)

Thanks for the posts below. commands are working properly. but there is slight modification in requirement.

1. command to get only first word of line.
2. command to get any middle word. like it can be 4th word or 5th. (something that i can change on run time).

Thanks again.

Last edited by HarryReid; 04-12-2012 at 05:00 PM.. Reason: requirment updatd.
# 2  
Old 04-12-2012
Code:
echo "My zip code is 24490" | awk '{print $NF}' >>tempfile

This User Gave Thanks to 47shailesh For This Post:
# 3  
Old 04-12-2012
Suppose the file is abc.txt from which u have to extract out last line..

the command is
Code:
cat abc.txt | awk '{print $NF}'


Last edited by Franklin52; 04-13-2012 at 03:44 AM.. Reason: Please use code tags for code and data samples, thank you
This User Gave Thanks to sudeep.id For This Post:
# 4  
Old 04-12-2012
That is a useless use of cat. awk is fully capable of reading files without cat's help.

Code:
awk '{ print $NF }' filename1 filename2 filename3 ...
# or
awk '{ print $NF }' < filename

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 04-12-2012
Homework question?

Please answer.

What did you really try?

OK
# 6  
Old 04-12-2012
I think the "." after "Harry" should not be included, right?
# 7  
Old 04-13-2012
@ Scrutinizer: yeah you are right. that was typo. "." is not included. but I'm still looking for answer for the question. like i want to get nth word of this line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

2. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

3. Shell Programming and Scripting

Read line by line and store a word in array

Hi, I would like to read a file line by line and then store the whole line word by word in array so that I can do specific manipulation on each word. $ cat test.txt hello how are you i am good I would like to have an array created dynamically so that first time , the... (4 Replies)
Discussion started by: ashwin3086
4 Replies

4. Shell Programming and Scripting

Read last word of nth line

Hi people; i want to read the last word of the 14th line of my file1.txt. Here is the EXACT 14th line of the file. 250 SectorPortnum=3,AuxPortInUngo=2,PortDeviceGroup=1,PortDeviceSet=1,PorDevice=1 20 >>> Set. i have to get the word Set. how can i call it and also how... (3 Replies)
Discussion started by: gc_sw
3 Replies

5. Shell Programming and Scripting

To read data word by word from given file & storing in variables

File having data in following format : file name : file.txt -------------------- 111111;name1 222222;name2 333333;name3 I want to read this file so that I can split these into two paramaters i.e. 111111 & name1 into two different variables(say value1 & value2). i.e val1=11111 &... (2 Replies)
Discussion started by: sjoshi98
2 Replies

6. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

7. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

8. UNIX for Dummies Questions & Answers

query on how to search for a line and read 4th word from that line

Assume I have a text file as below: me con pi ind ken pras ur me con rome ind kent pras urs pintu con mys ind pan pras ki con kit ind sys My requirement, I need to search for "con rome" and if exists, then print 4th word from rome, i.e in above example, since "con rome"... (4 Replies)
Discussion started by: jaggesh
4 Replies

9. Shell Programming and Scripting

Read line by line not word by word

i have this line my,name,is,john stored in a file d.sh and i wish to print the line as string..but im getting word by word...have tried this......... for in $(cat $d.sh); do echo $i done but this is giving me my name is john (6 Replies)
Discussion started by: vadharah
6 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question