LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 09:14 AM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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, 09:17 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

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

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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 09:24 AM.
 
1 members found this post helpful.
Old 09-12-2012, 09:37 AM   #4
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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 01:28 AM
Programing Question: Reading file line by line.. then char by char in each line adriumroot Programming 8 05-30-2010 01:55 AM
Howto Encrypt & Run Bash script File nabeeliumattack Linux - General 7 05-17-2008 04:58 PM
howto pick bash variables from file imagineers7 Linux - Newbie 3 05-08-2006 01:56 AM
howto create a file based on date in bash rohan208 Linux - Newbie 2 05-07-2004 03:54 PM

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

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