How do I get the last word of last line in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I get the last word of last line in a file
# 1  
Old 07-12-2012
How do I get the last word of last line in a file

Say the file is something like:

word1/word2/word3
word4/word5/word6
word7/word8/word9


How would I extract word9?
# 2  
Old 07-12-2012
awk: Set the field separator appropriately, then, use the END pattern to print the last field of the last line using $NF.

sed: Suppress printing with -n, use $ pattern to detect when you've arrived at the final line, delete everything up to and including the last delimiter in the line, and print.

sh/tail: tail and command substitution can store that last line in a shell variable. Then parameter expansion operators can delete the unwanted content in a manner analogous (but not identical) to what you'd do with sed.

tail/cut: If you know how many fields there are in the line, you can use tail to print the final line and then cut to extract the final field.

No doubt there are other variations. What have you tried?

Regards,
Alister

Last edited by alister; 07-12-2012 at 01:31 PM..
# 3  
Old 07-12-2012
I am using the sh/tail method, this is my code, but there is an error:

Code:
#!/usr/bin/ksh
ftp -n hostx <<EOF>> Report.txt
user user password
ls /tmp/ReportFile.*
 
EOF
$(tail -1 Report.txt) | grep ReportFile.* > lastword
 
cat lastword

it says "-rw-r--r--: not found" By the way, the last line of the txt file is:

Code:
-rw-r--r--   1 root       sys          85970 Jun 26 19:35 /tmp/ReportFile.0626


Last edited by mrskittles99; 07-12-2012 at 02:34 PM..
# 4  
Old 07-12-2012
from the suggestions of alister there are multiple ways to do this, based on your sample data here is the code:

Code:
$ cat test1
word1/word2/word3
word4/word5/word6
word7/word8/word9

Code:
cat test1 | tr -d '\n' | awk -F/ '{print $NF}'

or

Code:
awk -F/  '{print $NR}' test1 | tail -1

# 5  
Old 07-12-2012
Note: Not all awks retain the values of the field variables in the END section:

Code:
$ echo 1 2 | gawk 'END{print $2}'
2
$ echo 1 2 | nawk 'END{print $2}'

$

Code:
$ echo 1 2 | nawk '{s=$2}END{print s}'
2
$

# 6  
Old 07-12-2012
Quote:
Originally Posted by Scrutinizer
Note: Not all awks retain the values of the field variables in the END section
Is that a current/recent version of nawk?

Not that it changes the fact of the matter, but that behavior has always been a part of POSIX. If I am mistaken and it wasn't specified by the very first version of the standard, it was definitely codified by the second (which is at least 15 years old at this time).

I wonder why any modern, maintained implementation would not be compliant (I do realize that the K in AWK is the person behind nawk and that his work on awk and nawk predates the standard). Since awk generally has no way of indicating when the final record has been reached, aside from the END pattern, it's very useful to keep those variables intact. Also, historical scripts written for such an implementation would either not have been using those variables in END or would have been setting their values, so I don't see any backwards-compatibility issue with becoming compliant.

If that is a current nawk, given the timelines involved, this is on par with having to caution someone about using the $(...) form of command substitution with a recently released version of a POSIX-like shell.

Regards,
Alister
# 7  
Old 07-12-2012
It is the behavior nawk and /usr/xpg4/bin/awk on Solaris (also old awk) and of awk/nawk on AIX.
In my opinion this is in compliance with the POSIX standard (which was modeled after nawk), since POSIX only says something about retaining NF in the END section but it is mum about $0 and $1, $2, ..
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

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

4. Shell Programming and Scripting

[Bash]Attempting to Merge text from one file into another file at the line directly under a word

Hello, This is my first post on the forums. So I want to start by thanking anyone who is kind enough to read this post and offer advise. I hope to be an active contributor now that I've found these forums. I have an issue that I figure would be a good first post.. I have 2 text files... (5 Replies)
Discussion started by: efciem
5 Replies

5. 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

6. Shell Programming and Scripting

Help need to cut the first word of a line in text file

Hi All, I would like help with a script which can get rid of the first work of all lines in text file. File 1 The name is Scott. Output : name is Scott ---------- Post updated at 02:38 PM ---------- Previous update was at 02:37 PM ---------- Hi ALL There is typo error in... (3 Replies)
Discussion started by: bubbly
3 Replies

7. Shell Programming and Scripting

How to take first word of each line in the file

In Unix how to take the word of the line as out put pl help me (2 Replies)
Discussion started by: Ramesh Vellanki
2 Replies

8. 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

9. Shell Programming and Scripting

insert word in each line of a file

can someone tell me how can I insert a word in front of each line in a file. i tried with sed but didn't managed yet. Is there another command or this one(sed) works? 10x anyway. (7 Replies)
Discussion started by: atticus
7 Replies

10. UNIX for Dummies Questions & Answers

extract last word on line to new file

Can someone please help me with how to extract the last word on a line to a new file? I have a list of names like: Ms. Nell D. Bullock Mrs. Sherrie M Avent LINDA ANNETTE RUSSELL Mr. Jerome R. Harris Pandora Tyndall I want the new file to look like this: Bullock Avent RUSSELL Harris... (10 Replies)
Discussion started by: michieka
10 Replies
Login or Register to Ask a Question