The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Trim inside awk subin_bala Shell Programming and Scripting 3 05-06-2008 09:51 AM
Trim issue scorpio Shell Programming and Scripting 3 04-28-2008 12:19 PM
trim lines melanie_pfefer Shell Programming and Scripting 6 03-26-2008 07:25 AM
trim file tungaw2004 UNIX for Dummies Questions & Answers 1 09-15-2007 07:05 AM
trim letters fed.linuxgossip Shell Programming and Scripting 10 05-15-2007 11:11 PM

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 04-23-2007
JWilliams JWilliams is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 9
Trim

Hello,

I am passing a filename to a script to draw parameters from it. However, I want to use part of the filename as a parameter. The filename is transfer_ccf_3731_10.sh but I only need the 3731_10 part of it.

Is this possible? Any help or suggestions would be appreciated!

Regards,

J.
  #2 (permalink)  
Old 04-23-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,538
you can use bash's internal parameter expansion, check the bash man page, or tools like awk/sed.
eg awk
Code:
# echo "transfer_ccf_3731_10.sh" | awk -F "_" '{print $3"_"substr($4,-1,2)}'
3731_10
combination eg:
Code:
# var="transfer_ccf_3731_10.sh"
# echo ${var%.sh}
transfer_ccf_3731_10
# echo ${var%.sh} | awk -F "_" '{print $3"_"$4}'
3731_10
  #3 (permalink)  
Old 04-23-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by JWilliams
I am passing a filename to a script to draw parameters from it. However, I want to use part of the filename as a parameter. The filename is transfer_ccf_3731_10.sh but I only need the 3731_10 part of it.

Use the shell's parameter expansion:

Code:
filename=transfer_ccf_3731_10.sh
temp=${filename%%[0-9]*}
temp=${filename#"$temp"}
extract=${temp%.*}

Last edited by cfajohnson; 04-23-2007 at 06:44 PM..
  #4 (permalink)  
Old 04-23-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,223
Quote:
Originally Posted by JWilliams
Hello,
....The filename is transfer_ccf_3731_10.sh but I only need the 3731_10 part of it.
Code:
# var=transfer_ccf_3731_10.sh
# OIFS="$IFS"
# IFS="[_.]"
# set -- $var
# part="$3_$4"
# IFS="$OIFS"
# echo $part
3731_10
  #5 (permalink)  
Old 04-24-2007
napster_san napster_san is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 6
echo transfer_ccf_3731_10| cut -d_ -f3-
Closed Thread

Bookmarks

Tags
awk, awk trim, trim, trim awk

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 02:32 PM.


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