dimaj Posted August 4, 2010 Share Posted August 4, 2010 Hey guys! I was hoping you could help me with a regular expression. I have a string that I'm trying to separate using sed under FreeBSD (FreeNAS). Here's an example: Given: something 3D (something else) v1 2 3 I want to split it in the following manner: Name: something 3D (something else) Version: 1.2.3 I'm trying to write a bash script that would separate a string and write Name and Version to a csv file. I've been struggling with this for couple of days now and didn't get anywhere. dimaj Quote Link to comment Share on other sites More sharing options...
Netshroud Posted August 5, 2010 Share Posted August 5, 2010 (edited) TEXT="something 3D (something else) v1 2 3" echo Name: `echo $TEXT | cut -d v -f 1` echo Version: `echo $TEXT | cut -d v -f 2 | sed 's/ /./g'` Probably a better way to do it using regexp, but that works for me (assuming your "something" doesn't contain the letter V) Edited August 5, 2010 by Psychosis Quote Link to comment Share on other sites More sharing options...
dimaj Posted August 5, 2010 Author Share Posted August 5, 2010 TEXT="something 3D (something else) v1 2 3" echo Name: `echo $TEXT | cut -d v -f 1` echo Version: `echo $TEXT | cut -d v -f 2 | sed 's/ /./g'` Probably a better way to do it using regexp, but that works for me (assuming your "something" doesn't contain the letter V) Thanks for your reply, Psychosis! Unfortunately, that snippet won't work for me since 'v' could be present in the Name portion. I was able to use sed to get the version aspect by doing something like this: echo "something 3D (something else) v1 2 3" | sed -e 's/[[:digit:][:space:]]*$/(&)/g' This basically puts '()' around found string, but it doesn't return it. I was thinking that if I could extract the version info, I could remove it from the original string. thanks again. dimaj Quote Link to comment Share on other sites More sharing options...
Netshroud Posted August 6, 2010 Share Posted August 6, 2010 You could set your own delimiter instead of 'v', using that expression for sed. For example, I use the caret (^): echo "something 3D (something else) v1 2 3" | sed -e 's/[[:digit:][:space:]]*$/^&/g' | cut -d \^ -f 2 | sed 's/ /./g' Quote Link to comment Share on other sites More sharing options...
dimaj Posted August 6, 2010 Author Share Posted August 6, 2010 (edited) You could set your own delimiter instead of 'v', using that expression for sed. For example, I use the caret (^): echo "something 3D (something else) v1 2 3" | sed -e 's/[[:digit:][:space:]]*$/^&/g' | cut -d \^ -f 2 | sed 's/ /./g' THANK YOU!!! That was just what I needed :) In case somebody else is interested, here's my final code of the split: for i in *; do i=`echo $i | sed 's/\ AnDrOiD//g'` version=`echo $i | sed -e 's/[[:digit:][:space:]]*$/|&/g' | cut -d \| -f 2 | sed -e 's/^ //g' | sed 's/ /./g'` title=`echo $i | sed -e 's/[[:digit:][:space:]]*$/|&/g' | cut -d \| -f 1 | sed -e 's/ v$//g'` echo "Original: $i Version: $version title: $title" done dimaj Edited August 6, 2010 by dimaj Quote Link to comment Share on other sites More sharing options...
operat0r_001 Posted August 26, 2010 Share Posted August 26, 2010 regexbuddy ahs libs for regex and sort of try to convert it to other langs ... ( power grep too ) all fu ! http://www.rmccurdy.com/scripts/fu.txt Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.