Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Substring using sed or awk

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-01-2009
jamjam10k jamjam10k is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 21 February 2011, 11:49 AM EST
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Substring using sed or awk

I am trying to get a substring from a string stored in a variable. I tried sed with a bit help from this forum, but not successful. Here is my problem.

My string is: "REPLYFILE=myfile.txt"
And I need: myfile.txt (everything after the = symbol).

My string is: "myfile.txt.gz.20091120.enc
And I need: myfile.txt.gz (everything before string .gz, including .gz).

After my sed failed, I tried to get the position of the delimiting string thinking of using cut command. But failed at that as well.

As I am trying to learn sed and awk, any links to good tutorial is appreciated.
Thanks
Sponsored Links
    #2  
Old Unix and Linux 12-01-2009
daptal daptal is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 18 July 2012, 9:53 PM EDT
Posts: 175
Thanks: 0
Thanked 2 Times in 2 Posts

Code:
cat abc.txt
REPLYFILE=myfile.txt
myfile.txt.gz.20091120.enc
REPLYFILE=myfile.txt.gz.20091120.enc


Code:
sed 's/.*=//g;s/\.gz.*//g' abc.txt

chk if this helps.
Sponsored Links
    #3  
Old Unix and Linux 12-01-2009
rdcwayx rdcwayx is offline Forum Advisor  
Use nawk in Solaris
 
Join Date: Jun 2006
Last Activity: 22 March 2014, 12:27 PM EDT
Posts: 2,759
Thanks: 44
Thanked 419 Times in 407 Posts

Code:
echo "myfile.txt.gz.20091120.enc" |sed 's/\(\.gz\).*/\1/'

    #4  
Old Unix and Linux 12-01-2009
jaduks's Unix or Linux Image
jaduks jaduks is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 5 January 2012, 7:47 AM EST
Location: India
Posts: 185
Thanks: 0
Thanked 5 Times in 3 Posts
Using bash parameter substitution techniques:

1)
________________
${string##substring}
It deletes the "longest" match of $substring from 'front' of $string.
_________________

Code:
$ var="REPLYFILE=myfile.txt"

$ echo ${var##*=}
myfile.txt

or

$ echo $var | awk '{print $NF}' FS=\=
myfile.txt

2)
_________________________
${string%substring}
It deletes shortest match of $substring from 'back' of $string.
_________________________

Code:
$ var="myfile.txt.gz.20091120.enc"

$ echo ${var%gz*}
myfile.txt.

$ echo ${var%gz*}gz
myfile.txt.gz

Regards,
Jadu
Sponsored Links
    #5  
Old Unix and Linux 12-02-2009
jamjam10k jamjam10k is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 21 February 2011, 11:49 AM EST
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for everyone's time. All solutions are working.
Sponsored Links
    #6  
Old Unix and Linux 12-02-2009
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 4 August 2015, 5:59 PM EDT
Location: Amsterdam
Posts: 10,353
Thanks: 351
Thanked 2,748 Times in 2,455 Posts
Or:

Code:
var=myfile.txt.gz.20091120.enc
echo ${var%.*.*}
-or-
echo ${var%%.[1-9]*}

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
substring ?? varungupta Shell Programming and Scripting 7 10-01-2007 09:18 AM
substring panknil Shell Programming and Scripting 4 10-01-2007 08:12 AM
substring alla.kishore UNIX for Dummies Questions & Answers 8 01-09-2007 01:57 AM
substring using AWK mahabunta UNIX for Dummies Questions & Answers 3 09-19-2006 04:47 PM
substring Anika UNIX for Dummies Questions & Answers 4 09-19-2001 03:10 PM



All times are GMT -4. The time now is 07:52 PM.