LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 10: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, 10: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 10:21 AM.
 
2 members found this post helpful.
Old 05-02-2011, 10:17 AM   #3
cepheus11
Member
 
Registered: Nov 2010
Location: Germany
Distribution: Gentoo
Posts: 286

Rep: Reputation: 91
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, 11:01 AM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
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 11:06 AM.
 
Old 05-02-2011, 03: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 03:49 PM.
 
  


Reply

Tags
bash, parameter substitution, string



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

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

All times are GMT -5. The time now is 12:47 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
Open Source Consulting | Domain Registration