Sorting a string


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Sorting a string
# 8  
Old 01-12-2006
Just change vino's code to this:
Code:
#!/bin/bash
VAL=1234567890abcdefghij
echo ${VAL:2:1} ${VAL:10:8}

This seems to be limited to bash so if you are using anything else, you are better off using sed or something.
# 9  
Old 01-12-2006
Quote:
Originally Posted by blowtorch
Just change vino's code to this:
Code:
#!/bin/bash
VAL=1234567890abcdefghij
echo ${VAL:2:1} ${VAL:10:8}

This seems to be limited to bash so if you are using anything else, you are better off using sed or something.
I get the error:
***
${VAL:2:1} : bad substitution
***
# 10  
Old 01-12-2006
Quote:
Originally Posted by Khoomfire
I get the error:
***
${VAL:2:1} : bad substitution
***
What shell are you using? This will only work with bash.
# 11  
Old 01-12-2006
with slight modification,

Code:
echo 1234567890abcdefghij | sed -e 's/\(..\)\(.\)\(.......\)\(.*\)\(..\)/\2 \4/'

3 abcdefgh
# 12  
Old 01-12-2006
just a curiosity why do you not want to use cut ?
# 13  
Old 01-12-2006
Quote:
Originally Posted by linuxpenguin
just a curiosity why do you not want to use cut ?
Because cut will not leave spaces between the feilds (or will it?)
# 14  
Old 01-13-2006
Try...
Code:
echo "1234567890abcdefghij" | awk '{print substr($0,3,1) " " substr($0,11,8)}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help on Sorting

Hello Everyone, I need help here . I need to sort a file for one of my requirement , The file has to be sorted using a key with 4 columns. Sorting is working fine on those 4 columns but when the key is matching for many rows the other columns are also getting sorted which is not required . ... (11 Replies)
Discussion started by: richa_240889
11 Replies

2. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

3. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

string sorting in unix

Hi I need how to sort string characters for Example i have a file that contains this data example string "fan" but i want to display "afn" contained words "afn" is in sorted format for fan. File data faty gafny gaifny dafan gafnniunt O/p gafny gafnniunt (3 Replies)
Discussion started by: polineni
3 Replies

5. Shell Programming and Scripting

[Perl] Sorting an String-Array

Hi, i have a txtfile with the format <Nr>tab<word>tab<other stuff>new line and i want to sort the <word>-colum with a perl script. My textfile: <Nr>tab<word>tab<other stuff>new line 6807 die ART.Acc.Sg.Fem 6426 der ART.Gen.Sg.Fem 2 die ART.Nom.Sg.Fem 87 auf APPR.-- 486 nicht PTKNEG.--... (1 Reply)
Discussion started by: buckelede
1 Replies

6. Shell Programming and Scripting

Sorting

Let's say that I have a database that I call part ID. This database has the following grouping: Dart1=4 Dart2=8 Dart3=12 Fork1=68 Fork2=72 Fork3=64 Bike1=28 Bike2=24 Bike3=20 Car1=44 Car2=40 Car3=36 I want to write a program that would read this database and tell me when the... (19 Replies)
Discussion started by: Ernst
19 Replies

7. Shell Programming and Scripting

Sorting string with date and number

Hi, We have files coming in the system and we want to sort it in ascending order with date and sequence. The file pattern are inbound_crp_date_sequence.xml example we have file as below: inbound_crp_20100422_10.xml inbound_crp_20100422_2.xml inbound_crp_20100422_3.xml... (2 Replies)
Discussion started by: sreejitnair123
2 Replies

8. UNIX for Dummies Questions & Answers

sorting ASCII string containing numbers

I have the following output where I need to sort the second column numerically (starting with IBMULT3580-TD10 and ending in IBMULT3580-TD123) Drv DriveName 0 IBMULT3580-TD13 1 IBMULT3580-TD18 2 IBMULT3580-TD14 3 IBMULT3580-TD10 4 IBMULT3580-TD11 5 IBMULT3580-TD17 ... (8 Replies)
Discussion started by: GKnight
8 Replies

9. Shell Programming and Scripting

Perl: sorting by string

I have an array full of string values that need to be sorted, but if a value starts with (regex) 0^ it should be at the beginning of the array. Otherwise the array should be sorted normally using ascii sort. Please help me create the sub to pass to the sort function. (7 Replies)
Discussion started by: dangral
7 Replies

10. Shell Programming and Scripting

Sorting an address string

I'm in an introduction to Unix class and well I'm kind of stuck on one part of the lab for this week or shell scripts. Basically we're given a file named address.data and we're supposed to create a script to sort it according to zip code, last name, and first name (not at the same time of course).... (0 Replies)
Discussion started by: Minimum
0 Replies
Login or Register to Ask a Question