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 05-02-2011, 11:13 AM   #1
hayloiuy
LQ Newbie
 
Registered: Apr 2011
Location: Malaysia
Distribution: Ubuntu
Posts: 6

Rep: Reputation: 0
Extracting nth length of character from a variable to another variable


Example, I want to extract the first four character from serial and store it into version.

Code:

serial= 10001100
version=...(codes)....
echo $version


output in the terminal:
1000

Just to show you guys i havent been lazy...

Code:
#!/bin/bash

serial=10110011
echo $serial> serial.txt

version= cut -c1-4  serial.txt

echo $version
but this code saves the value into a text file. I want a code that does not save into any file whatsoever. Help? Thanks.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 05-02-2011, 11:17 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
http://tldp.org/LDP/abs/html/string-manipulation.html.

Last edited by Nylex; 05-02-2011 at 11:21 AM.
 
2 members found this post helpful.
Old 05-02-2011, 11:17 AM   #3
cepheus11
Member
 
Registered: Nov 2010
Location: Germany
Distribution: Gentoo
Posts: 142

Rep: Reputation: 39
The "cut" man page says: "With no FILE, or when FILE is -, read standard input."

Code:
version=$(echo $serial | cut -c1-4)
echo $version
 
0 members found this post helpful.
Old 05-02-2011, 12:01 PM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by hayloiuy View Post
Example, I want to extract the first four character from serial and store it into version.

Code:

serial=10001100
version=...(codes)....
echo $version


output in the terminal:
1000
Code:
foo$ serial=10001100
foo$ version=${serial:0:4}
foo$ echo $version
1000
http://www.gnu.org/software/bash/man...eter-Expansion

HTH

Last edited by Telengard; 05-02-2011 at 12:06 PM.
 
Old 05-02-2011, 04:47 PM   #5
speedy64
LQ Newbie
 
Registered: Apr 2011
Location: Kentucky
Distribution: Ubuntu 10.10
Posts: 7

Rep: Reputation: 0
I've had some success using typeset with ksh, does not work with bash.

Code:
typeset -L4 version

serial=10001100
version=$serial

echo $version

Last edited by speedy64; 05-02-2011 at 04:49 PM.
 
  


Reply

Tags
bash, parameter substitution, string


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] How to parse files with variable record length btacuso Programming 4 08-11-2010 11:49 AM
Variable length console prompt statquant Linux - General 7 07-15-2010 06:55 PM
how to generate variable length packets in iperf rohit83.ken Linux - Networking 1 03-10-2009 09:53 PM
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 06:21 PM
Variable length objects kamransoomro84 Programming 4 10-28-2004 01:56 PM


All times are GMT -5. The time now is 09:07 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration