The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
matching a letter in a word Furqan_79 Shell Programming and Scripting 2 06-05-2008 02:45 PM
First letter of each Word from a line maxmave Shell Programming and Scripting 5 04-15-2008 08:52 PM
How to filter the words, if that word contains the expected letter venu_eie UNIX for Advanced & Expert Users 3 03-19-2008 01:00 PM
find a word in a file, and change a word beneath it ?? vikas027 Shell Programming and Scripting 2 02-13-2008 04:23 PM
Can a shell script pull the first word (or nth word) off each line of a text file? tricky Shell Programming and Scripting 5 08-17-2006 07:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-30-2009
JeiPrakash JeiPrakash is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 3
Red face Taking letter from a word

Hi All,

I am new to UNIX and i am trying to write a script.My requirement is that from the following logs i need to get the following outputs:
abc_lifecycle.log
bcde_enjoy.log
abc_twinkle.log

Output expecting:

lifecycle
enjoy
twinkle

Could you please help me in getting this?
  #2 (permalink)  
Old 03-30-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380

Code:
echo 'abc_lifecycle.log
  bcde_enjoy.log
  abc_twinkle.log' | awk -F [_.] '{ print $2 }'

  #3 (permalink)  
Old 03-30-2009
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,184
See "man basename".


Code:
basename abc_lifecycle.log .log|awk -F_ '{print $2}'
lifecycle


Last edited by methyl; 03-30-2009 at 09:16 PM.. Reason: wrt cfajohnson comment and OP requirement
  #4 (permalink)  
Old 03-30-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380
Quote:
Originally Posted by methyl View Post
See "man basename".


Code:
basename abc_lifecycle.log .log
abc_lifecycle

Apart from the fact that that is not what the OP asked for, the basename command is entirely unnecessary in a POSIX shell (which is available on every system made in the last 10 to 15 years at least) and on any system before that which had ksh.


Code:
string=abc_lifecycle.log
temp=${string##*_}
printf "%s\n" "${temp%.*}"

If there are many names to be processed, then the awk script I posted previously may be quicker.
  #5 (permalink)  
Old 03-30-2009
JeiPrakash JeiPrakash is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 3
Smile Thanks

Thanks Johnson.

But the problem is that i have just given only 3 log samples.In general i have 74 logs like this separated by such underscores.I need to get that 74 log names alone.
  #6 (permalink)  
Old 03-30-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,099

Code:
echo abc_lifecycle.log | sed 's/\..*$//'

or


Code:
var=abc_lifecycle.log
echo ${var/%.*/}

  #7 (permalink)  
Old 03-31-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380
Quote:
Originally Posted by JeiPrakash View Post
But the problem is that i have just given only 3 log samples.In general i have 74 logs like this separated by such underscores.I need to get that 74 log names alone.

Where do you "have" them?

If they're in a file, use the awk script I posted with a filename instead of a pipe.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0