LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-23-2009, 05:14 PM   #1
sravanth.svk
LQ Newbie
 
Registered: May 2006
Posts: 25

Rep: Reputation: 0
Question how can we cut the last 8 characters of a word?


Hi All,
my word/the string looks something like this
"sri_ropk_fdslfo_jun_09.lst" for which
we can use awk and get the following " sri_ropk_fdslfo_jun_09 "
as a single word in which i want to cut "jun_09" and put it in a variable.
NOTE: the name "sri_ropk_fdslfo_jun_09.lst" is dynamic and the no of characters might change
so if we have a way to cut it from the last or any other way to pick the last 6 characters then i can get the things done.

i want this using any of the shell scripts.
as i don't know any thing about perl or php
 
Old 06-23-2009, 05:22 PM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Not particularly elegant, but it should work:
Code:
echo sri_ropk_fdslfo_jun_09 | rev | cut -c1-6 | rev
Python definitely has a more succinct way of doing this, as does perl, I'm sure, but this was the first thing that came to mind using traditional shell tools.

Last edited by pwc101; 06-23-2009 at 05:23 PM.
 
Old 06-23-2009, 06:11 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Code:
$ echo "sri_ropk_fdslfo_jun_09.lst"| sed -r 's/.*(......)\.lst/\1/'
jun_09

Cheers,
Tink
 
Old 06-30-2009, 06:05 PM   #4
sravanth.svk
LQ Newbie
 
Registered: May 2006
Posts: 25

Original Poster
Rep: Reputation: 0
Thank you guys,
both of them are working fine in linux.
i was trying them on SUNos i thought the basic commands would not differ but i came to know that even basic commands may differ.
so i found a solution using awk along with substring and length.

Thank you all
 
Old 06-30-2009, 07:39 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by sravanth.svk View Post
Thank you guys,
both of them are working fine in linux.
i was trying them on SUNos i thought the basic commands would not differ but i came to know that even basic commands may differ.
so i found a solution using awk along with substring and length.

Thank you all
My sed-line, with slight modifications:
Code:
$ uname -a
SunOS app01 5.9 Generic_122300-13 sun4u sparc SUNW,Sun-Fire-V440
$ echo "sri_ropk_fdslfo_jun_09.lst"| sed  's/.*\(......\)\.lst/\1/'
jun_09


Cheers,
Tink
 
Old 07-01-2009, 06:21 AM   #6
sravanth.svk
LQ Newbie
 
Registered: May 2006
Posts: 25

Original Poster
Rep: Reputation: 0
Hi,

Thanks a ton for the of above

a new one

i have one more issue related to the same script i am doing.
i thought of making a new post but felt this would be better.

The normal out put of the date command would be something like
date '%b'
Jul
but i want the out put to be
Aug
that is when i am using the date command value to a variable i want the variable to hold the next month value

do we have any options in date or any other way to do this

Thanks
sravanth.svk
 
Old 07-01-2009, 10:01 AM   #7
BeacoN
Member
 
Registered: Nov 2008
Distribution: Linux Mint
Posts: 56

Rep: Reputation: 17
are you using BASH?
also-are you sure there's a 3 char suffix on all files?
assuming you want the last 6 chars BEFORE the suffix you can use this...

base=`basename $file`;
echo ${base:${#base}-10:6}

1) get the basename of the file (incase it's a full path)
2) syntax is ${variable:startindex:numberofchars}
-of course ${#base} is all the characters in the file.
-start 10 chars back because of the period

hope this is what you're looking for. if you're not sure all files have a 3 char suffix you'll need another script to strip the last part off, let me know if this is the case.

Last edited by BeacoN; 07-01-2009 at 10:03 AM.
 
Old 07-01-2009, 10:08 AM   #8
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
in case you are on bash, for you second problem

date +%b -d "+1 month"

Check this out it is working for me
 
Old 07-01-2009, 10:33 PM   #9
sravanth.svk
LQ Newbie
 
Registered: May 2006
Posts: 25

Original Poster
Rep: Reputation: 0
Hi,

The file name will change for every directory that i create.
i am able to copy the file name from an older date which is some thing like
"sri_ropk_fdslfo_jun_08.lst". and i am making the user to rename it manually.

In which there are entries which i have to substitue with the current values of the upcoming month.
in the script i am making the user give the input for this dates like
echo "enter the date in the format of yyyymm
and
echo "enter the month and year in the format of JUN_09"

instead i want to make these automated using the date command or any other simple script or combinations of commands.
 
  


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
Word Processor That Finds/Replaces Undisplayed Characters (like Line Breaks)? Teasdale Linux - Software 11 01-31-2008 10:09 AM
Could not display characters in word .doc files MichaelYoung Debian 1 11-10-2006 10:05 AM
[BASH] How to filter characters away? !{param#word} {param%word}! Dark Carnival Programming 8 03-17-2005 02:49 PM
Cut last 3 characters of stdin? jago25_98 Linux - General 2 09-04-2004 12:04 PM
Copy Paste of data from word to Yahoo/rediff in opera is limited to few characters sachin_keluskar Linux - Software 0 08-19-2004 10:46 AM


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