LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-25-2008, 08:31 AM   #1
amuthiga
LQ Newbie
 
Registered: Nov 2008
Posts: 5

Rep: Reputation: 0
Can not capture no at end of file store it and print it using sed and awk


Hello, Would someone guide me on how to write a shell script the would search for a phone no using at the end text file using sed or awk and store it in a varaible or print it.

The text file is in this form

text or numbers in first line
text or numbers in second line
.
.
.
Firsname Lastname:KEN:+254456789

The first line may take any form but the last line will always be
Firstname Lastname:KEN:+254456789
I would like to seach for the Phone and store it in a variable and print it.
The phone no will alway be preceeded by ":+"


Thank you in advance
 
Old 11-25-2008, 10:12 AM   #2
AuroraCA
Member
 
Registered: Jul 2008
Location: Northern CA USA
Distribution: Ubuntu, Slackware, Gentoo, Fedora, Red Hat, Puppy Linux
Posts: 370

Rep: Reputation: 35
Your title says that you cannot capture # at end of file store it and print it using sed and awk.

This implies that you have tried and failed.

What have you tried? Please post your code and we can help you correct it.
 
Old 11-25-2008, 10:15 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
An awk solution:
Code:
awk 'END{print "+" gensub(/.*\+/,"","g")}' testfile
This will process the last line (END), substituting all the charcaters up to the last "+" with... nothing. The print statement adds the "+" before the number again. To store the number in a shell variable, use command substitution:
Code:
number=$(awk 'END{print "+" gensub(/.*\+/,"","g")}' testfile)
 
Old 11-25-2008, 06:47 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
# awk 'BEGIN{FS="[:][+]"}END{print $NF}' file
254456789
 
Old 11-25-2008, 10:45 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,301

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Code:
tail -1 t.t|cut -d'+' -f2
where t.t is your datafile name
 
Old 11-26-2008, 03:57 AM   #6
amuthiga
LQ Newbie
 
Registered: Nov 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by AuroraCA View Post
Your title says that you cannot capture # at end of file store it and print it using sed and awk.

This implies that you have tried and failed.

What have you tried? Please post your code and we can help you correct it.
Thank you for your reply. I am one day old on shell scripting,awk and sed.
What I ment was I tried to come up with script that does what I described and failed.
 
Old 11-26-2008, 04:18 AM   #7
amuthiga
LQ Newbie
 
Registered: Nov 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
An awk solution:
Code:
awk 'END{print "+" gensub(/.*\+/,"","g")}' testfile
This will process the last line (END), substituting all the charcaters up to the last "+" with... nothing. The print statement adds the "+" before the number again. To store the number in a shell variable, use command substitution:
Code:
number=$(awk 'END{print "+" gensub(/.*\+/,"","g")}' testfile)
Thank you the help colucix. The code worked.
I can across TEXT=`sed -e '1,/^$/d' < $2` would you please tell what it means.
I am only one day in using shellscripts, awk and sed.

Thank you for your response.
 
Old 11-26-2008, 04:22 AM   #8
amuthiga
LQ Newbie
 
Registered: Nov 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
Code:
# awk 'BEGIN{FS="[:][+]"}END{print $NF}' file
254456789
Thank you for the help. The code worked but would explain how it gets the number.

Thanks
 
Old 11-26-2008, 04:28 AM   #9
amuthiga
LQ Newbie
 
Registered: Nov 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
Code:
tail -1 t.t|cut -d'+' -f2
where t.t is your datafile name
Thank you the code did the job. Would you explain what tail -1 t.t|cut -d'+' -f2 means or how it accomplishes the task.

Thanks.
 
Old 11-26-2008, 07:11 AM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,301

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
man tail
man cut

basically 'tail' reads lines from the end (tail) of the file. '-1' says just one/last line.
cut cuts (sic) the output, using a delimiter (-d) of '+' and outputs field num 2

see http://rute.2038bug.com/index.html.gz
http://www.tldp.org/LDP/abs/html/

bookmark and read those links. You can get the syntax for any Unix cmd using

man <cmd-name> as shown above.
 
Old 11-26-2008, 07:37 AM   #11
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
To learn awk, check the official user guide of GNU awk "Gawk: Effective AWK Programming". Chapter 1 should give you a good overview on how to use awk.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Manipulating Text File with awk or sed kushalkoolwal Programming 2 09-10-2008 07:35 PM
sed: print section of file from string to end of file samyboy Linux - Newbie 4 02-26-2008 07:23 AM
Sed/Awk: print lines between n'th and (n+1)'th match of "foo" xaverius Programming 17 08-20-2007 11:39 AM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 05:07 PM


All times are GMT -5. The time now is 09:07 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration