LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-01-2012, 03:25 PM   #1
rm_-rf_windows
Member
 
Registered: Jun 2007
Location: Europe
Distribution: Ubuntu
Posts: 292

Rep: Reputation: 27
BASH: string is "charset=iso-8859-1", I want to extract "iso-8859-1" only. How??


Hello all,

I think this is an easy question to answer, but I can't seem to figure it out. 'sed' is for files, not variables...

I get:
Code:
<myfile>: text/x-c++; charset=iso-8859-1
as output, the result of
Code:
file -i <myfile>
I've stored this little result ("<myfile>: text/x-c++; charset=iso-8859-1") into a variable and would like to get yet another variable which contains the string "iso-8859-1" only (length may vary depending on the encoding), in other words, to extract the part of the string that follows "charset=" up until the end ot the string. It seems simple, but I just can't figure it out!

Thanks in advance,

rm

Last edited by rm_-rf_windows; 10-02-2012 at 06:13 AM.
 
Old 10-01-2012, 03:53 PM   #2
kaiserkarl13
LQ Newbie
 
Registered: Jan 2010
Distribution: Fedora
Posts: 28

Rep: Reputation: 5
This should work:
Code:
var1="<myfile>: text/x-c++; charset=iso-8859-1"
var2=$(echo $var1 | cut -f2 -c=)
Alternatively, you can use sed:
Code:
var1="<myfile>: text/x-c++; charset=iso-8859-1"
var2=$(echo $var1 | sed 's/^.*=//')
 
Old 10-01-2012, 04:20 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Alternatively
Code:
var1='<myfile>: text/x-c++; charset=iso-8859-1'
var2=${var1##*=}
That is shell parameter expansion. The ## means "strip the longest string matching what comes next from the left side". Shell parameter expansion is a powerful technique and runs faster than using awk, cut or sed (provided the strings are less than a few thousand characters). Unfortunately it's not very intuitive.
 
1 members found this post helpful.
Old 10-02-2012, 06:11 AM   #4
rm_-rf_windows
Member
 
Registered: Jun 2007
Location: Europe
Distribution: Ubuntu
Posts: 292

Original Poster
Rep: Reputation: 27
Many thanks to both or you. It worked like a charm!

I'm going to post my second question in a separate post (text format conversion, no longer above for those of you who didn't answer this post. It can be found here: http://www.linuxquestions.org/questi...95#post4794795).

rm

Last edited by rm_-rf_windows; 10-02-2012 at 06:21 AM.
 
  


Reply



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
Is "Raw CD image" exactly same as an "ISO" file ? Aquarius_Girl General 7 10-27-2010 05:19 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Info:Switching console charset mapping to ISO-8859 Rixcz Linux - Software 0 10-06-2006 09:14 AM
Suse and ISO-8859-15 charset badbunny Linux - Newbie 2 10-21-2005 03:44 AM
gnome-terminal wrong charset (ANSI_X3.4-1986 not ISO-8859-1). omes Linux - General 1 06-10-2005 09:55 PM

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

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

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