LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-27-2013, 07:30 AM   #1
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Bash: back-referencing / shuffle string content using internals


Code:
#!/bin/bash
# -------------------------------------------------------------------------- #
# Examples before and after:
#   01.foo.bar.ext       ->  foo.bar.01.ext
#   10.foo.txt           ->  foo.10.txt
#   9.foo.bar.fubar.cfg  ->  foo.bar.fubar.9.cfg
# -------------------------------------------------------------------------- #
tstStr01="01.foo.bar.ext"  
echo -e "\nOriginal string: $tstStr01"

echo -e "\n - Using Sed - - - - - - -\n"
echo $tstStr01 | sed -r 's/(01)\.(.*)\.(ext)/\2.\1.\3/'

echo -e "\n - Using Bash - - - - - - -\n"
strPrefix=${tstStr01%%.*}                 # get prefix
strSuffix=${tstStr01##*.}                 # get suffix
tmp1=${tstStr01%.*} ; strRest=${tmp1#*.}  # get "rest"
echo "$strRest.$strPrefix.$strSuffix"     # rebuild
As shown in the above code snippet using sed and back-referencing is a simple task but using bash internals it needs multiple steps to accomplish the same result.

Questions (about the bold part):
- Is it at all possible to use back-referencing when using parameter expansion? Haven't been able to find anything on-line about this.
- Is there a shorter, possibly more elegant way to do this?

Just to make sure: I'm looking for a bash internal solution and _not_ for an awk, perl, sed, etc solution.
 
Old 11-27-2013, 10:56 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
How about:
Code:
#!/bin/bash

regex='([^.]+)\.(.*)\.(.{3})'

[[ "$1" =~ $regex ]] && echo "${BASH_REMATCH[2]}.${BASH_REMATCH[1]}.${BASH_REMATCH[3]}"
This gave me your required output for the 3 examples (Note: the sed will not )
 
1 members found this post helpful.
Old 11-27-2013, 11:48 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Original Poster
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by grail View Post
How about:
Code:
#!/bin/bash

regex='([^.]+)\.(.*)\.(.{3})'

[[ "$1" =~ $regex ]] && echo "${BASH_REMATCH[2]}.${BASH_REMATCH[1]}.${BASH_REMATCH[3]}"
This gave me your required output for the 3 examples
That's a really good alternative. Readable and compact.
Quote:
(Note: the sed will not )
Yeah..... Added the examples after I reread the finished post and forgot about the fixed ext part in the code.

Thanks!
 
  


Reply

Tags
bash, internals, parameter expansion



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
Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008) drwatson_droid Programming 5 12-01-2011 10:44 AM
bash scripting - arrays and indirect referencing soulcurry Programming 7 04-21-2011 06:41 AM
BASH variable referencing SwingingSimian Programming 3 10-16-2008 05:20 AM
Java Question - referencing variables by name as a string qscomputing Programming 3 01-31-2007 04:49 AM
sed back referencing pauljtester Linux - Software 2 11-11-2004 04:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:23 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration