Help me please: UNIX command to extract substring not squeeze spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help me please: UNIX command to extract substring not squeeze spaces
# 1  
Old 08-20-2012
Question Help me please: UNIX command to extract substring not squeeze spaces

Hi experts,

Please help me!...

I have a string " test1 test2 test3 ".
There are two spaces before "test1"; There are four spaces between "test1" and "test2"; there are two spaces between "test2 and "test3".

I want to extract a substring "2 test3" using positions.

Below is my test script:
Code:
************
str=" test1 test2 test3 "
start_pos=16
val_end_pos=23
temp_val=`echo $str | cut -c$start_pos-$val_end_pos`
echo $temp_val
************

The result is "t3". It seems the multiple spaces are squeezed into one space between words.

How can I get "2 test3"?

Thank you so much in advance,
Sophie

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by vbe; 08-20-2012 at 12:20 PM..
# 2  
Old 08-20-2012
Did you try to display the value of $str after?
try again, but with
Code:
export str=" \ test1 \ \   test2 test3 "

...
# 3  
Old 08-20-2012
What's your shell? In bash or new enough ksh, you can use substring operators:

Code:
echo "${STRING:BEGIN:LENGTH}"

where begin and length are numbers...
# 4  
Old 08-20-2012
Hi vbe,

Thank you so much for the reply...

Yes, your code works (export str=" \ test1 \ \ test2 test3 ").

But I cannot insert \ into the str. The str=" test1 test2 test3 " is what I got by retrieving the data row by row from a feed file.

Is there a way to not sequeeze spaces by using str=" test1 test2 test3 "?

Thank you again,
Sophie

---------- Post updated at 11:57 AM ---------- Previous update was at 11:49 AM ----------

Hi Corona688,

The command works :-)

echo "${STRING:BEGIN:LENGTH}"


I am using ksh.

Thank you so much!
Sophie
This User Gave Thanks to sophiez16 For This Post:
# 5  
Old 08-20-2012
try with double quotes

Code:
echo "$str"

example :

Code:
$ a="  ABC  EFG HIJ"
$ echo $a
ABC EFG HIJ
$ echo "$a"
  ABC  EFG HIJ

# 6  
Old 08-20-2012
Thank you so much for the reply - itkamaraj

Sophie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script Shell Extract substring

Hi all, Please, i'd like to extract string just before '.fr'. Here is some lines of my file: g-82.text.text1.fr.worker1 g-xx.yyyyyy.zzzz.fr.worker2 i'd like to extract this text: g-82.text.text1 g-xx.yyyyyy.zzzz Please, which command i have to use in my script shell ? ... (16 Replies)
Discussion started by: chercheur111
16 Replies

2. Shell Programming and Scripting

Extract a substring from a file

Hello, A question please. A have a file that contains a string. Ex: AAAABBCCCCCDDEEEEEEEEEEFF I'd want to recover 2 substrings, 'BB' and 'FF' and then leave them in a new file. From position 5, 2 caracters (ex:"BB") and from position 25, 2 caracters (ex:"FF") in a file. Could anoyone help me... (3 Replies)
Discussion started by: nolo41
3 Replies

3. Shell Programming and Scripting

Extract substring in a file

Hello, A question please. A have a file that contains a string. Ex: AAAABBCCCCCDDEEEEEEEEEEFF I'd want to recover 2 substrings, 'BB' and 'FF' and then leave them in a new file. Could anoyone help me please? Thanks in advance (3 Replies)
Discussion started by: nolo41
3 Replies

4. Shell Programming and Scripting

How to extract a substring from a string

Hi, I have an input string say for example: ABC,DEF,IJK,LMN,...,XYZ The above string is comma delimited. Now I have to extract the last part after the comma i.e. XYZ. :b: (3 Replies)
Discussion started by: bghosh
3 Replies

5. Solaris

Extract substring from a string

i have srtring i.e. "NAME,CLASS,AGE" (length of string is not constant) and from this string i've extract each word delimited by "," (comma). INPUT: "NAME,CLASS,AGE" OUTPUT: NAME CLASS AGE how can i do that? i have tried some string manipulation function like... (5 Replies)
Discussion started by: jadoo_c2
5 Replies

6. Shell Programming and Scripting

Extract a substring.

I have a shell script that uses wget to grab a bunch of html from a url. URL_DATA=`wget -qO - "$URL1"` I now have a string $URL_DATA that I need to pull a substring out of..say I had the following in my string <p><a href="/scooby/929011567.html">Dog pictures check them out! -</a><font... (3 Replies)
Discussion started by: shellpower
3 Replies

7. Shell Programming and Scripting

Need Help... to extract the substring

> tnsping $TWO_TASK | grep HOST Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.12.10.212)(PORT = 1540)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = OMTST15))) I want to extract like this HOST = 10.12.10.212 PORT = 1540 SERVICE_NAME = OMTST15 I... (4 Replies)
Discussion started by: dashok.83
4 Replies

8. Shell Programming and Scripting

Sed extract substring on (OS X)

On OS 10.4.11 I have filenames like: 670711 SA T2 v1-1_DS_EF.doc CT_670520 AM T1 v1-2_DS_EF.doc CT_670716 - 2 SA T4 v1-2_DS_EF.doc CT_670713 SA T3 v1-1_DS_EF.doc 670421 PA DYP1 v1-1_DS_EF.doc CT_670425 PA DYP2 v1-1_DS_EF.doc CT_670107 RA T3 v1-2_DS_EF.doc CT_670521 AM T2 v1-2_DS_EF.doc... (3 Replies)
Discussion started by: mlommel
3 Replies

9. UNIX for Dummies Questions & Answers

how to extract a substring froma file

hi all, I'm really newbie on this and I need some help. how is the best way to extract a strig or substring from a each line in a file. e.g. I want to print only this ERROR=JUD+the followed numbers from one line like this one, considering the numbers change related to different errors ... (1 Reply)
Discussion started by: morena
1 Replies

10. Shell Programming and Scripting

command/script to extract a substring from a string

I have a long string "<ID type="Oid">{}</ID>" I need to extract "GigabitEthernet0/1" from the above string. How can it be done? :) (5 Replies)
Discussion started by: girisha
5 Replies
Login or Register to Ask a Question