Need to extract the 2nd last word of each line

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need to extract the 2nd last word of each line
# 1  
Old 07-13-2011
Need to extract the 2nd last word of each line

Hello experts,

I have question that i want to extract the second last word of each line.

I have a text file, in that text file there are near about 40 lines.
sample of line present in file


I want to extract
x10 from each line....

Can any one please help, as i am in learning phase.
# 2  
Old 07-13-2011
awk -F"/" '{print $(NF-1)}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 07-13-2011
Thanks a lot bartus11 for your help...
Can you tell me how could i store the whole path in a variable by excluding the last word

lets say


is the complete path then i want to get
/user/Oracle/x/x/S/part_bkp/temp_part_bkp/x1/ in a variable

actually i am trying to archive something which very tough for me as a beginner. here is a small description

Lets say i have a directory A which has some changes made on daily basis
i am storing information of file changed in file by using Find command.
Now i have another copy of directory A as copy_A
Now I want to write a script which read line by line text from file and copy the changes and place it in copy_A

if you have some logic then please share with me

find /usr/oracle/Data/web/catalog/default -mtime -1 -type f > user/oracle/Test_admin/myfolder/bakup/part_bkptemp_part_bkp_repeated/list.txt
while read eachline
do cp -p $eachline "copy of Dirctory Copy_A"
done < user/oracle/Test_admin/myfolder/bakup/part_bkptemp_part_bkp_repeated/list.txt

Last edited by pludi; 07-13-2011 at 11:02 AM..
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

extract whole thing in word, leaving behind last word. - perl

Hi, i've a string /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD/TESi T_11_HD_120/hd-12 i need to get string, like /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD the words from HD should get deleted, i need only a string till HD, i dont want to use any built in... (4 Replies)
Discussion started by: asak
4 Replies

4. Shell Programming and Scripting

Extract word from a line

Hi, I've searched the forum to get what I wanted but to no avail. Here's the problem I'm facing. The line is suppose as below: <INPUT DATABASE ="ORACLE" DBNAME ="UNIX" NAME ="FACT_TABLE" OWNERNAME ="DIPS"> Now I want to extract only FACT_TABLE. The trials are as follows: awk... (3 Replies)
Discussion started by: dips_ag
3 Replies

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

6. UNIX for Dummies Questions & Answers

Word extract from a line

In a file let a.txt, has 1 line code i.e. code QC:Tiger:10: /code I need to get my output like "Tiger 10". So how can i do this? (3 Replies)
Discussion started by: anupdas
3 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

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

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

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