Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to count occurances 1

Status
Not open for further replies.

Tison

Programmer
May 12, 1999
216
CH
I have the following file that I want to extract the last field from ;
Nov 11 07:20 Test_1.1.12345
Nov 11 08:20 Test_1.1.1.7544
Nov 12 07:00 Fred_455.11.123.42479
Nov 12 11:13 STUFF_1.55

ie. All I need is the 12345 or 7544 or 42479 or 55

How do I cut it when the field length varies and the record can have any amount of data before it ?
There will always be a . before the number but there may be more than one dot in the entire record.
 
Sorry, something disappeared:
sed -e 's/.*\.\(.*\)/\1/' your_file
 
Thanks a ton,,,works like a charm.

Can you explain what it is doing ?
 
For some variety, another method would be to use awk -F"." '{print $NF}' filename

This separates the fields using the "." as a separator and prints out the last field.
 
We tell sed (stream editor to perform editing on-the-fly, script introduced by -e, / used as delimiter in command syntax) to :

s/ :substitue in a string...

.*\.\(.*\) :look for a pattern "any occurrence of any character (.*) followed by a dot (\.) (dot is a special character, needs backslash) followed by any occurrence of any character we want to remember (same as first .* surrounded by "\(" and "\)" )

\1 :so, substitue the whole string with the sub-pattern we previously extracted, and finally print it (by default, sed prints every input line).
 
Oh shoot forget the cryptic sed syntax! All you need is korn shell:

${variable##.*}

This will print everything after the last "."
 
Works fine but now I need to cut the name from the string.
In this example ;
Nov 12 07:00 Fred_455.11.123.42479
Nov 12 11:13 STUFF_1.55

I need to cut the Fred_455.11.123 and STUFF_1

How do I do that ?
 
To do the whole thing ( i.e. extract the name plus the number at the end )
awk -F"." '{print $1 " " $NF}' filename|awk '{print $4 " " $5}'

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top