Storing string with space (urgent)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing string with space (urgent)
# 1  
Old 11-03-2008
Java Storing string with space (urgent)

hi i have a file content like

my_file:
1US8738297897918[space][space][space]0[space][space]0[space][space]0
[1US8738297897918][space][space][space]0[space][space]0[space][space]0
[1US8738297897918][space][space][space]0[space][space]0[space][space]0

i used
while IFS= read line
do
echo "$line" | grep '^\['
if [ "$?" -eq 0 ]
then
last=`echo "$line" | cut -c 3-20`
echo "$last"
fi
done < $my_file


but it is writing blank spaces into the o/p fie

please help

thanks
Satya
# 2  
Old 11-03-2008
First off, i'd like to state for the record that i usually skip threads with "urgent" in their title out of habit. If you have an urgent demand for work you can't do on your own please consider hiring an expert.

Ok, having said this, it would also be a nice gesture to enclose code in "code"-tags. This is what they are for and they are here for a reason - perhaps for the same reason the telephone directory is printed in a tabular, formatted way instead of a stream of names and numbers: READABILITY.

Now back to the problem. I took the liberty to reformat your script a little:

Quote:
Originally Posted by Satyak
Code:
while IFS= read line ; do
     echo "$line" | grep '^\['
     if [ "$?" -eq 0 ] ; then
          last=`echo "$line" | cut -c 3-20`
          echo "$last"
     fi
done < $my_file

The first thing i notice is a useless use of "grep". Your goal is to limit your operation to all lines starting with a "[" character, yes? (If this is not the case your script is just wrong.) This could also be done in a sed (awk, ...) script:

Code:
sed -n '/^\[/ {
               some command
               some other command
               ....
               p
             }' /some/file > /some/other/file

A second thing is the - quite antiquated - use of backticks. Do NOT use them any more! We are in the 21st century now, we do not use morse code (we have picture telephones now), horse-pulled carts (we have automobiles) and we do not use backticks for similar reasons - we have the "$(....)" construct, which can be nested and even enclosed in normal double quotes.

(As an aside: some hold that backticks are backwards compatible with older shells. True. I hope these are writing their scripts in cuneiform writing on clay tablets to be "backwards compatible" with sumeric texts. Antiquity has ended - get over it.)

In this case not even the "$(....)", which opens a subshell, is necessary. There are enough powerful shell textprocessing commands to cut character 3-20 out of a string:

Code:
a="123456789012345678901234567890"
b="${a##??}"                  # chop off the first two characters, leaving char 3-
c="${b%%??????????????????}"  # chop off everything but the first 18 chars, leaving char 3-20 from the original string

You might want to find out what a big impact on execution time this has by reading this thread.

But in your case even this is not necessary because it can be done inside the sed script we already use to filter for lines:

Code:
sed -n '/^\[/ {
               s/^..\(.\{18\}\).*$/\1/p
             }' /some/file > /some/other/file

which does the same as the shell construct: replace the complete line with characters 3-20 and put it to <stdout>. This is what is left of your script, it will do exactly the same as the shell script you presented here: it takes every line which starts with "[" and extracts

If doesn't do what you want it to do you might tell us what your goals are and we might find out together.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Storing string in array(triangular form)

I have a string like below. Now i want to split this string like below and put the value in the array using awk. I am able to do it using bash but getting no clue for doing it in awk O/P A=0 A=01 A=014 A=0143 A=01438 A=014387 A=0143876 A=01438765 A=014387650 ... (7 Replies)
Discussion started by: siramitsharma
7 Replies

2. Shell Programming and Scripting

Cutting a string and storing it in a variable

Hello I have a script: path=test1 echo "${path%?}" till this the program is successful in deleting hte last character i.e. "1" and displays an output --> test. After this how can i save this output to another variable. (2 Replies)
Discussion started by: Kishore920
2 Replies

3. Programming

Splitting string and storing in int

I have a string containing 2 integers separated by /, for example 12/8 or 8/6 am want to store the numbers in two integers. (3 Replies)
Discussion started by: kristinu
3 Replies

4. Shell Programming and Scripting

Extracting particular string in a file and storing matched string in output file

Hi , I have input file and i want to extract below strings <msisdn xmlns="">0492001956</ msisdn> => numaber inside brackets <resCode>3000</resCode> => 3000 needs to be extracted <resMessage>Request time getBalances_PSM.c(37): d out</resMessage></ns2:getBalancesResponse> => the word... (14 Replies)
Discussion started by: sushmab82
14 Replies

5. Red Hat

Urgent help on Linux Redhat / out of space

Hi, I'm running Redhat Entreprise. My / is 0% available. I don't know what processes are keeping writting to files. I don't know where to check. Please help. (3 Replies)
Discussion started by: lamoul
3 Replies

6. Shell Programming and Scripting

searching and storing unknown number of lines based on the string with a condition

Dear friends, Please help me to resolve the problem below, I have a file with following content: date of file creation : 12 feb 2007 ==================== = name : suresh = city :mumbai #this is a blank line = date : 1st Nov 2005 ==================== few lines of some text this... (7 Replies)
Discussion started by: swamymns
7 Replies

7. Shell Programming and Scripting

sed - searching for string and storing in variable

Hi I'm trying to find a way to search a text file for a specific string. I have a file which contains i.p. addresses and port numbers in the following format: 'ip="www.xxx.yyy.zzz"' 'port="xx""' I want to print only the parts between the double quotes for use in seperate variables,... (4 Replies)
Discussion started by: melias
4 Replies

8. Shell Programming and Scripting

Very urgent :- How to compare string using if statement

Hi How can i compare a string using if statement in a script? For eg: I have filename="abc.sh" if ;then { ....... ....... } fi but this doesnot work .. How will i execute this ?? Thanks in advance (1 Reply)
Discussion started by: jisha
1 Replies

9. Shell Programming and Scripting

Storing string with space

Hi all I am trying this simple command: a="abc abc" echo $a output is: abc abc But expected output shoould be :abc abc i.e spaces in real string are geting truncated to one space everytime. Plz Help. (4 Replies)
Discussion started by: rahul303
4 Replies

10. Shell Programming and Scripting

Storing space delimited line in var with loop?

I have a script that converts a file into an html table. This script works fine for a 1 column table. However, I'm trying to do this for a multi-column table. My input file will look something like this: a b c d e f g h i My script basically works by taking in each line and putting that... (2 Replies)
Discussion started by: eltinator
2 Replies
Login or Register to Ask a Question