outputting selected characters from within a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers outputting selected characters from within a variable
# 1  
Old 10-14-2009
outputting selected characters from within a variable

Hi all,

if for example I had a variable containing the string 'hello', is the any way I can output, for example, the e and the 2nd l based on their position in the string not their character (in this case 2 and 4)?

any general pointers in the right direction will be much appreciated, at present i'm looking at awk but only seem to be able to find references to fields and lines.

thanks

skinnygav p.s. Using Bash shell by the way

---------- Post updated at 08:17 AM ---------- Previous update was at 06:00 AM ----------

I've solved this issue to an extent with

Code:
var1=hello
var2=3
echo $var1 | awk -F "" '{print $2}'

this will output the 2nd character e.

Is there any way I can modify this to output the character in the position stored in var2?

Code:
var1=hello
var2=3
echo $var1 | awk -F "" '{print $var2}'

does not work...
# 2  
Old 10-14-2009
A different approach:

Code:
#! /bin/bash

TXT='halo'
POS=3

echo "${TXT:$(($POS-1)):1}"

exit 0

Code:
[house@leonov] sh test.bash
l

# 3  
Old 10-14-2009
ok now have

Code:
echo $var1 | eval awk -F \"\" \'{print \$$var2}\'

which works fine, thanks for all your help guys!

---------- Post updated at 08:31 AM ---------- Previous update was at 08:29 AM ----------

oh thanks dr house, i'll have a look at that syntax as it means I will b able to echo -n so that all characters are output to the same line.

does print within awk have a similar option?
# 4  
Old 10-14-2009
Quote:
Originally Posted by skinnygav
i'll have a look at that syntax as it means I will b able to echo -n so that all characters are output to the same line
No need for "echo -n" - you could echo more than one substring with the same line of code Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Outputting characters after a given string and reporting the characters in the row below --sed

I have this fastq file: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGGGGGGGGGGGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCA +test-1 GGGGGGGGGGGGGGGGGCCGGGGGFF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8... (10 Replies)
Discussion started by: Xterra
10 Replies

2. Shell Programming and Scripting

Get first four characters from the variable name

Hi all I have a variable name as below runbatch=FCSTworLOADfx_CYR Now i need to search for FCST in this variable and run a program I am doing like this but it is failing if ; then ****RUN PROGRAM** Please help DJ Please use CODE tags when displaying sample input,... (3 Replies)
Discussion started by: Hypesslearner
3 Replies

3. Shell Programming and Scripting

Bash string variable outputting incorrectly

Hello All, I am learning BASH scripting and I would appreciate any help with a small problem I am having... I am writing a script that builds a simple hosts file for DNS reasons related to a piece of software called netdb by parsing another application's config files for IP's and their... (4 Replies)
Discussion started by: Wesley545
4 Replies

4. Shell Programming and Scripting

getting first two characters of variable

From a shell script, I'm trying to get the first two characters of an environment variable. If I type at the command promot: XX=`echo $MYVAR | cut -c1-2` echo $XX It works just fine However, if I execute the exact same thing from a shell script, I get: cut: you must specify a list of... (2 Replies)
Discussion started by: Boomn4x4
2 Replies

5. Shell Programming and Scripting

Stripping characters from a variable

I'm using a shell script to get user input with this command: read UserInput I would then like to take the "UserInput" variable and strip out all of the following characters, regardless of where they appear in the variable or how many occurrences there are: \/":|<>+=;,?*@ I'm not sure... (5 Replies)
Discussion started by: nrogers64
5 Replies

6. Shell Programming and Scripting

trying to print selected fields of selected lines by AWK

I am trying to print 1st, 2nd, 13th and 14th fields of a file of line numbers from 29 to 10029. I dont know how to put this in one code. Currently I am removing the selected lines by awk 'NR==29,NR==10029' File1 > File2 and then doing awk '{print $1, $2, $13, $14}' File2 > File3 Can... (3 Replies)
Discussion started by: ananyob
3 Replies

7. Shell Programming and Scripting

Cutting characters in a variable

I have a variable var which contains "ABCDEFDH" Now I have to remove the first 4 characters that is "ABCD" so my variable should contain only "DEFH" plzz tell me how to do that . i am using bash shell (1 Reply)
Discussion started by: cynosure2009
1 Replies

8. Shell Programming and Scripting

how to assign to each variable after selected?

Hi Everyone, I need to assign the value to the variable after selection. Anyone can help me? for example: data_type_SQL=$($CONNECT cat <<-__EOF__ SET NOCOUNT ON select location_type, location_id, warehouse from JEALOCATION where LOCATION_ID="$data_LocID_SQL" for "data_type_SQL"... (9 Replies)
Discussion started by: ryanW
9 Replies

9. UNIX for Dummies Questions & Answers

Perl - converting selected characters to upper/lower case

Using STDIN, how can I use perl to take an input string, with all lower case letters in the first five characters, and convert them to uppercase... then take all uppercase letters in the second five characters and convert them to lowercase. Example: MichaelSmith to michaELSMIth Thank you! (2 Replies)
Discussion started by: doubleminus
2 Replies

10. UNIX for Dummies Questions & Answers

To get the characters from a variable

Hi , I need to get the specified word from the variable.can some one help me out in this ? input=ASD34567P i need to get the value as : output1=34567 So i need to ignore the last character and get the remaining values. THANKS IN ADVANCE. (6 Replies)
Discussion started by: ithirak17
6 Replies
Login or Register to Ask a Question