Visit Our UNIX and Linux User Community


How to print last word of line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print last word of line
# 1  
Old 03-10-2011
How to print last word of line

Hi,

How to print last word of line?

Code:
#!/bin/bash

x1="This is Kiran"
echo "$x1"

how to print "Kiran" in new variable.i.e x2=kiran
# 2  
Old 03-10-2011
Code:
x1="This is Kiran"
x2=`echo ${x1##* }`
echo $x2
Kiran

This User Gave Thanks to yinyuemi For This Post:
# 3  
Old 03-10-2011
Another way..
Code:
x1="This is Kiran"
x2=`echo $x1|cut -d ' ' -f3`

This User Gave Thanks to michaelrozar17 For This Post:
# 4  
Old 03-10-2011
There is always another way in Linux :Smilie

x1="This is Kiran"
x2= `echo $x1 | awk '{ print $NF }'
This User Gave Thanks to r05h777 For This Post:
# 5  
Old 03-10-2011
Quote:
Originally Posted by yinyuemi
Code:
x1="This is Kiran"
x2=`echo ${x1##* }`
echo $x2
Kiran

I guess no need of "echo" here.

Code:
x2=${x1##* }

Another way,

Code:
expr "$x1" : '.* \(.*\)'

These 2 Users Gave Thanks to clx For This Post:
# 6  
Old 03-10-2011
Hi,

I have written one script :

Code:
#!/bin/bash

echo -n -e "\nEnter file name : "
read File
Temp=`find /apps12i -name $File`
echo "$Temp"

Output:

Enter file name : abc.log

/path1/abc.log
/path2/abc.log

I need script that should print "abc.log file present in multiple path"
so how do i compare "abc.log " ????
# 7  
Old 03-10-2011
You should start new thread for different topic/issue.

Try something like...

Code:
Temp=`find /apps12i -name $File | wc -l`

if [ "$Temp" -gt 1 ]; then
 echo "abc.log file present in multiple path"
fi

This User Gave Thanks to clx For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #490
Difficulty: Medium
Java source code is compiled into Java bytecode which is actual machine code.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to match the first word and print only that line in UNIX?

Below is the file DISK-A 109063.2 49 31 40.79 DISK-B 110058.5 49 44 57.07 DISK-c 4402.4 2 1 2.14 from the file, i want to search for 'DISK-A' and print only that line with the first word matching to DISK-A and the output should skip DISK-A. Desired Output: (If i'm... (2 Replies)
Discussion started by: web2moha
2 Replies

2. Shell Programming and Scripting

Command to grep a word and print the whole line splitted into many

Hi, I need to search a word in the java file. Assume the line in the java file is, (the line was splitted into 3 lines) 1.operationContext.sendFeedback(2.FeedbackType.ERROR, null, "Input is empty.", "Input Details: pr 3.ovide Valid pair(s): "+pairType); When i grep for the word... (6 Replies)
Discussion started by: tulasiram
6 Replies

3. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

4. Shell Programming and Scripting

break the string and print it in a new line after a specific word

Hi Gurus I am new to this forum.. I am using HP Unix OS. I have one single string in input file as shown below Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight | Abc098 | ........ I want to achive the result in a output file as shown... (3 Replies)
Discussion started by: kannansr621
3 Replies

5. Shell Programming and Scripting

Using awk to print line starting with particular word

Hi Geeks, Consider this line: driver=c:\folder1\folder2 The above line is contained in a variable say 'var' . I want to copy everything after 'driver=' in to another variable say var2. Please tell me how can this be done. (8 Replies)
Discussion started by: ajincoep
8 Replies

6. Shell Programming and Scripting

How to find and print the last word of each line from a text file

Can any one help us in finding the the last word of each line from a text file and print it. eg: 1st --> aaa bbbb cccc dddd eeee ffff ee 2nd --> aab ered er fdf ere ww ww f the o/p should be a below. ee f (1 Reply)
Discussion started by: naveen_sangam
1 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. Shell Programming and Scripting

Search word in a line and print earlier pattern match

Hi All, I have almost 1000+ files and I want to search specific pattern. Looking forwarded your input. Search for: word1.word2 (Which procedure contain this word, I need procedure name in output. Expected output: procedure test1 procedure test2 procedure test3 procedure test4 ... (7 Replies)
Discussion started by: susau_79
7 Replies

9. Shell Programming and Scripting

print next line if matches a particular word..need help

Hi i need a help for making a script whch can print next line if it matches a particular word like file1 have ename Mohan eid 2008 ename Shyam eid 345 if scipt got Mohan it will print next line (eid 2008) pls help me .......:) (2 Replies)
Discussion started by: anish19
2 Replies

10. Shell Programming and Scripting

print a line containing word in a column using grep

hi, how to print a row which contains a perticular word in its third column using grep, cut, or any thing else. thanks (2 Replies)
Discussion started by: useless79
2 Replies

Featured Tech Videos