finding the Last String in a Line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting finding the Last String in a Line
# 1  
Old 04-21-2011
finding the Last String in a Line

Hi,

i am looking for a command that can help me find the last Sting in a Line

ex.. [Thr 138] yyeyrtehhehrerry: change this from [Starting steps (10)] to [END (3)]

here i want to extract END.
# 2  
Old 04-21-2011
Code:
$ echo "[Thr 138] yyeyrtehhehrerry: change this from [Starting steps (10)] to [END (3)]" | nawk awk -F'[' '{$0=$NF ; sub(/ .*/, "") } 1'
END
$

# 3  
Old 04-22-2011
But that wouldn't work if the last string is not preceded by '['. E.g.:
Code:
$ echo 'ex.. [Thr 138] yyeyrtehhehrerry: change this from [Starting steps (10)] to END (3)]' | awk -F'[' '{$0=$NF ; sub(/ .*/, "") } 1'
Starting

How about this?
Code:
$ echo 'ex.. [Thr 138] yyeyrtehhehrerry: change this from [Starting steps (10)] to[END (3)]' | sed 's/[^[:alpha:]]/\n/g' | sed  '/^$/d'  | tail -n 1
END

# 4  
Old 04-22-2011
Code:
# tested with bash 4 (regex 3.2+)
[[ $string =~  ([[:alpha:]]+).[^[:alpha:]]*$ ]]
echo ${BASH_REMATCH[0]% *}

# 5  
Old 04-26-2011
finding the Last String in a Line

Thanks Experts ,

don't know but it does not work , what is wrong here(ksh)
Code:
$ echo 'ex.. [Thr 138] yyeyrtehhehrerry: change this from [Starting steps (10)] to[END (3)]' | sed 's/[^[:alpha:]]/\n/g' | sed  '/^$/d'  | tail -n 1
usage: tail [+/-[n][lbc][f]] [file]
       tail [+/-[n][l][r|f]] [file]

All i want is LAST string of a line or may be a file

Thanks

Last edited by Franklin52; 04-26-2011 at 04:15 AM.. Reason: Please use code tags
# 6  
Old 04-26-2011
Try this:
Code:
echo 'ex.. [Thr 138] yyeyrtehhehrerry: change this from [Starting steps (10)] to[END (3)]' | sed 's/[^[:alpha:]]/\n/g' | sed  '/^$/d'  | echo 'ex.. [Thr 138] yyeyrtehhehrerry: change this from [Starting steps (10)] to[END (3)]' | sed 's/[^[:alpha:]]/\n/g' | sed  '/^$/d'  | sed -n '$ p'

How do you define 'last string' in your universe? Because really, the whole input
Code:
'ex.. [Thr 138] yyeyrtehhehrerry: change this from [Starting steps (10)] to[END (3)]'

is a string. So what are you trying to extract? Last word consisting of letters only? Or last substring before space? Or last word (substring separated by spaces) consisting of at least one letter? Or something else last?
The above code works like this: the first sed command replaces all non-alphabet characters with a newline; the next sed command will delete the empty lines, and the last sed will print only the last line (which is the last blob of alphabet characters in the input).
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

How to add a new string at the end of line by searching a string on the same line?

Hi, I have a file which is an extract of jil codes of all autosys jobs in our server. Sample jil code: ************************** permission:gx,wx date_conditions:yes days_of_week:all start_times:"05:00" condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Discussion started by: raghavendra
1 Replies

2. Shell Programming and Scripting

Help with finding last line of file: if statement depending on that line.

Good morning, My first time actually posting in this forum, though I have used this forum to help with numerous projects. I am trying to figure out why my if statement does not work. I have a file where a line is inputted every 15 seconds. I want this if statement to check what the last line... (3 Replies)
Discussion started by: Shanrunt
3 Replies

3. Shell Programming and Scripting

Reformatting single column text file starting new line when finding particular string

Hi, I have a single colum file and I need to reformat the file so that it creates a new line every time it come to an IP address and the following lines are corresponding rows until it comes to the next IP address. I want to turn this 172.xx.xx.xx gwpusprdrp02_pv seinwnprd03... (7 Replies)
Discussion started by: kieranfoley
7 Replies

4. Shell Programming and Scripting

finding the line number of a particular line in a file

Hi Frnds, I need to find the line number of a particular line in a file and store that line number to a variable. if a file named myfile contains following look at the sun look at the moon look at the star look at the ocean i need to get the line number of the line 'look at the... (3 Replies)
Discussion started by: mvignesh
3 Replies

5. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

6. Shell Programming and Scripting

Finding a string with another string is found

finding a string with another string is found EX: abs c/- i want to find /-, then copy abs. i know it's easy use awk, but my problem is the substr syntax. substr($2,2,2) will give me /- but the conflict is /- is not always the second characted of the second string. (11 Replies)
Discussion started by: engr.jay
11 Replies

7. Shell Programming and Scripting

Finding a string in a text file and posting part of the line

What would be the most succinct way of doing this (preferably in 1 line, maybe 2): searching the first 10 characters of every line in a text file for a specific string, and if it was found, print out characters 11-20 of the line on which the string was found. In this case, it's known that there... (13 Replies)
Discussion started by: busdude
13 Replies

8. Shell Programming and Scripting

help with finding a string

Hello folks I have a text file abcd.txt and has a line starting with number '8'. I have a string in this line starting at position 'a' to position 'b' also this string is a number and have to be reduced by 1. there is also a problem that it has to be padded with zeros to make the string... (2 Replies)
Discussion started by: sandyg
2 Replies

9. Shell Programming and Scripting

Finding a certain string on each line in a file

Hi, I need a script to get every line from a file where there are less then 17 ; on a line. Thank's (5 Replies)
Discussion started by: VODAFUN
5 Replies
Login or Register to Ask a Question