LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 04-25-2013, 10:01 PM   #1
maddyfreaks
Member
 
Registered: May 2011
Posts: 70

Rep: Reputation: 0
help needed in scripting


Hi All...

Am new to linux .. need your expertise in getting this done.
I have 2 variables like
DEST1=IN_25_PATH
DEST2=IN_RT_SMARTN

i want to pull from 7th to last character using cut how do i do

so my output for DEST1 should be PATH and otherone should be SMARTN

Looking something with CUT/SED and my OS is Linux 6.5 and Solaris 5.10

Appreciate your help
 
Old 04-25-2013, 10:28 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Just try cut or awk, as:
Code:
~$ echo $DEST1 | cut -d"_" -f3
~$ echo $DEST2 | cut -d"_" -f3

~$ echo $DEST1 | awk -F"_" '{print $3}'
~$ echo $DEST2 | awk -F"_" '{print $3}'

Last edited by shivaa; 04-25-2013 at 10:30 PM.
 
Old 04-25-2013, 11:14 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,507

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
If we are assuming that up until the last underscore is to be removed, just keep it in bash:
Code:
echo ${DEST1##*_}
 
Old 04-25-2013, 11:17 PM   #4
maddyfreaks
Member
 
Registered: May 2011
Posts: 70

Original Poster
Rep: Reputation: 0
Thanks shiva .

But sometime. My variables looks like below
Dest1="def:23-34:std_df"

At that time I need output from 34:std_df

So how to get it

---------- Post added 04-26-13 at 12:18 AM ----------

[QUOTE=maddyfreaks;4939041]Thanks shiva .

But sometime. My variables looks like below
Dest1="def:23-34:std_df"

At that time I need output as 34:std_df

So how to get it using cut or sed
 
Old 04-26-2013, 02:03 AM   #5
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 273Reputation: 273Reputation: 273
[QUOTE=maddyfreaks;4939041]Thanks shiva .

But sometime. My variables looks like below
Dest1="def:23-34:std_df"

At that time I need output from 34:std_df

So how to get it

---------- Post added 04-26-13 at 12:18 AM ----------

Quote:
Originally Posted by maddyfreaks View Post
Thanks shiva .

But sometime. My variables looks like below
Dest1="def:23-34:std_df"

At that time I need output as 34:std_df

So how to get it using cut or sed
Code:
echo $Dest1 | cut -d"-" -f2
anyway
man cut
http://lowfatlinux.com/linux-columns-cut.html
http://linux.101hacks.com/linux-comm...mand-examples/
 
Old 04-26-2013, 02:08 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
Code:
Dest1="def:23-34:std_df"
echo ${Dest1:7}

34:std_df
http://wiki.bash-hackers.org/syntax/pe
 
Old 04-26-2013, 02:39 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
@maddyfreaks:
In that case just change the delimiter of cut command from '_' to ':'. As kooru said, go through the manual of cut cmd for better understanding.
http://linux.die.net/man/1/cut
 
Old 04-29-2013, 07:29 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
I think you need to define what you are trying to do a bit more clearly.

Do you need to extract a substring based on a pattern of content that exists in the string, i.e. everything after the hyphen?

Or do you need to print a certain exact count of columns, no matter what the contents?

In either case the shell's built-in parameter substitution patterns are all you need. Once a text string is stored in a variable, shell built-ins are almost always the most efficient solutions. Only use cut, awk or other external commands when you have to process external files or other large amounts of text in bulk.


For grabbing substrings based on content, The basic "#,##,%,%%" substring removal and replacement patterns should generally do the job, as previously demonstrated.

For extracting substrings based on length, use the ":x:y" pattern, as shown in chrism01's post. Do note, however, that this pattern is found only in bash, ksh and some other advanced shells, so if you need something more portable, you have to get a bit fancier.

Code:
$ Dest1="def:23-34:std_df"
$ x=${Dest1%?????????}          #remove the last 9 digits and store in a new variable
$ echo ${Dest1#$x}              #remove that substring from the original variable
34:std_df


$ echo ${Dest1#${Dest1%?????????}}   #more compact, but harder to read
34:std_df

There are a lot of other built in string manipulations available too. Try to consider them first.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Scripting help needed. ZAMO Linux - General 2 01-12-2009 03:05 AM
help needed in scripting ZAMO Linux - General 2 10-31-2008 08:23 AM
Scripting help needed. ZAMO Linux - General 2 09-09-2008 06:11 AM
a little scripting help needed!! vatsan1 Linux - General 6 09-02-2007 08:50 AM
Scripting help needed. stonelee Linux - Software 2 09-29-2003 09:47 AM


All times are GMT -5. The time now is 05:21 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration