get last word from each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get last word from each line
# 1  
Old 06-30-2011
get last word from each line

Hello Experts,

I have requirement where a file consist of multiple lines...some sample of lines are ...
Code:
/usr/local/Oracle/shared/admin/adhoc
/usr/local/Oracle/shared/admin/new
/usr/local/Oracle/shared/admin/test

I want to get all my last word "adhoc new test"
Code:
sed 's/.*/\(.*\)/\1/' list.txt

but giving error
Code:
sed: 0602-404 Function s/.*/\(.*\)/\1/ cannot be parsed.

its failing


Please help me
Smilie
Thanks for all your support and help

Last edited by Franklin52; 06-30-2011 at 01:02 PM.. Reason: Please use code tags, thank you
# 2  
Old 06-30-2011
Code:
 
$nawk -F"\/" '{print $NF}' test
adhoc
new
test

---------- Post updated at 02:43 PM ---------- Previous update was at 02:42 PM ----------

Code:
 
$while read line; do basename $line; done < test
adhoc
new
test

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 06-30-2011
if the format of the lines does not change this may help you..
Code:
name=`cat <filename> | awk -F '/'  '{ print $7}'`
echo $name

or else to print the last word try this
Code:
name=`cat <file name> | awk -F '/'  '{ print $(NF)}'`


Last edited by Franklin52; 06-30-2011 at 01:03 PM.. Reason: Please use code tags, thank you
This User Gave Thanks to gowtham.varma For This Post:
# 4  
Old 06-30-2011
Hi
Just a backslash missing in your original sed:

Code:
sed 's/.*\/\(.*\)/\1/'  list.txt

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 06-30-2011
Thanks Guru...thanks for all your help and everyone cheers
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

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

5. UNIX for Dummies Questions & Answers

regular expression for replacing the fist word with a last word in line

I have a File with the below contents File1 I have no prior experience in unix. I have just started to work in unix. My experience in unix is 0. My Total It exp is 3 yrs. I need to replace the first word in each line with the last word for example unix have no prior experience in... (2 Replies)
Discussion started by: kri_swami
2 Replies

6. Shell Programming and Scripting

Adding a word in front of a word of each line.

Adding a word in front of a word of each line.In that line only one word will be there. pl help:( (4 Replies)
Discussion started by: Ramesh Vellanki
4 Replies

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

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

9. UNIX for Dummies Questions & Answers

how to move word by word on command line

Hey All, On commad promt of a shell.. How can we move our cursor word by word. Like Ctrl+A takes to the starting of the command... Any shortcut like that..? Thanks pbsrinivas (1 Reply)
Discussion started by: pbsrinivas
1 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