LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-06-2018, 07:59 AM   #1
santhoshmr
LQ Newbie
 
Registered: Mar 2018
Posts: 1

Rep: Reputation: Disabled
Exact required content from name


Hi,

I am learning sed command recently,

i need to store the particular content in seperate variables.

echo "INDEX_VALUE_id65_p225_TOTAL_TIME_100h30m52S_POSITION_1234p5p8"

my output required like
variable_1=65_p225
variable_2=100.30.52
variable_3=1234.5.8

i can get this output using cut command but my content will change randomly.
 
Old 03-06-2018, 08:09 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,650

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by santhoshmr View Post
Hi,
I am learning sed command recently, i need to store the particular content in seperate variables.

echo "INDEX_VALUE_id65_p225_TOTAL_TIME_100h30m52S_POSITION_1234p5p8"

my output required like
variable_1=65_p225
variable_2=100.30.52
variable_3=1234.5.8

i can get this output using cut command but my content will change randomly.
Ok..we're happy to help, but the first step is for you to post what you have done/tried on your own so far. Read the "Question Guidelines" link in my posting signature. We don't know what you mean by "content will change randomly", but if it's a consistent pattern as above, it shouldn't be too hard. I would suggest looking at the cut and awk commands too.
 
1 members found this post helpful.
Old 03-06-2018, 08:16 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
To add to TB0ne's reply. What will be helpful is to see your cut command, and also understand what you mean when you say that your content will change randomly. Do you mean the input changes or the output changes? Perhaps some additional samples of strings for input as well as some descriptions about what you mean when you encounter your problems.

Please post example data or your commands and script into a [code] block by using [code][/code] around that information.
 
Old 03-06-2018, 08:31 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
show your "home work" code to we can see it then / and try to figure out your logic and method you're using to try and get whatever you're doing, done.

PATTERNS, very important. Always look for a patten you can use when dealing with strings, and data extraction, and replacements. You have a string that looks to be a set pattern with parts of it that may change, The parts you put in bold are your changing parts these are not the pattern, because it changes, the pattern is the part of it that stays constant through out the entire run of what you're doing.

So you're going to need to use the pattern and something that will complement that. Seeings that you're needing the the data that is going to be an unknown at all times. Use the Pattern to find your changing data, then use that pattern for your set point, then look ahead or behind it depending on which end of the string you're attacking to get your data.

BASH Scripting example.
Code:
#!/bin/bash
data_string="INDEX_VALUE_id65_p225_TOTAL_TIME_100h30m52S_POSITION_1234p5p8"
#cut off left side
var1=${data_string/INDEX_VALUE_/}
echo $var1
#take remainder then cut off right side
var1=${var1%_TOTAL*}
#final results
echo $var1
Results.
Code:
$ ./get_data
id65_p225_TOTAL_TIME_100h30m52S_POSITION_1234p5p8
id65_p225
No matter what the INDEX VALUE is you're always going to get it using the patterns and string manipulation.

You have basically two types to deal with. The one where you have to find a pattern then look ahead or behind it, then get that information.or replace it, or look for the information itself and get that information, or replace it. All depending on its pattern.

Does not matter if you're using BASH, sed, awk, perl, ect.. PATTERNS are very important. Familiarize yourself with finding patterns. Then figure out what is the best tool to use.

you also can use character count which is also a type of pattern in itself. Using string manipulation, ending with this.

Code:
var1=id65_p225
var2=100h30m52S
var3=1234p5p8
http://tldp.org/LDP/abs/html/string-manipulation.html

Last edited by BW-userx; 03-06-2018 at 09:04 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
[SOLVED] wanted to make file content in required format. azheruddin Linux - Newbie 15 02-01-2013 01:39 PM
Dividing content of one file by content of another larspend Linux - Newbie 5 04-12-2011 08:00 PM
How to view the content of "slack-required" and extract from .exe files lchxr Slackware 6 06-07-2007 06:50 AM
Exact HD-to-HD copy samtheviking Fedora 7 02-25-2005 05:32 PM
Apache serving content after the content is removed? jrbush82 Linux - Software 6 05-05-2004 04:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:19 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