LinuxQuestions.org
Latest LQ Deal: Linux Power User 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 09-12-2012, 10:14 AM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
a new BASH question, howto get the 3rd - 5th char out of a name in a file


I need to know how to get the 3rd - 5th character in this situation:

Code:
[103050_rx30@rx30 ~]$ set - `cat deaopts.cnf`
[103050_rx30@rx30 ~]$ echo $1
NDXX0000147
I am trying to generate a variable from the above file for archiving reasons. the basic file name will be the 3rd - 5th character of echo $1. The first two chars are meaningless for the file name, but do represent other bits of info that I don't need.

I'm sure there is some kind of awk or sed command that I can use to grab those characters, but sadly I just dont know enough yet to figure that out.

Thanks in advance for the help.
 
Old 09-12-2012, 10:17 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
something like this:
echo $1 | sed ' s/^..\(...\).*$/\1/'
 
1 members found this post helpful.
Old 09-12-2012, 10:23 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Or if you want to stick with BASH:

echo ${1:2:3}

In normal-speak, extract 3 characters from variable "1", starting at position 2 (it's 0-based).

Last edited by suicidaleggroll; 09-12-2012 at 10:24 AM.
 
1 members found this post helpful.
Old 09-12-2012, 10:37 AM   #4
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Original Poster
Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
perfect guys, thanks.
 
  


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
[SOLVED] w. bash open file, cut n char from begin of each line, wrt shortened lines to new fil DearWebby Programming 3 12-14-2010 02:28 AM
Programing Question: Reading file line by line.. then char by char in each line adriumroot Programming 8 05-30-2010 02:55 AM
Howto Encrypt & Run Bash script File nabeeliumattack Linux - General 7 05-17-2008 05:58 PM
howto pick bash variables from file imagineers7 Linux - Newbie 3 05-08-2006 02:56 AM
howto create a file based on date in bash rohan208 Linux - Newbie 2 05-07-2004 04:54 PM


All times are GMT -5. The time now is 12:23 PM.

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