Jump to content

Regex With Sed


dimaj
 Share

Recommended Posts

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

Link to comment
Share on other sites

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 by Psychosis
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

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 by dimaj
Link to comment
Share on other sites

  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...