LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2006, 06:58 AM   #1
pawan_songara
LQ Newbie
 
Registered: Aug 2006
Posts: 6

Rep: Reputation: 0
How to remove spaces from a string


Hi All,

I am new to scripting. I need your help to removing spaces from a string and assign them to different variables.

Iwant to call script with one command line argument which is file name which containes different attributes their type and different values eg

fdxE1ConfigAdminStatus int 1 2
fdxE1LineType int 1 2 3
.
.
.

Now I want script take one line every time from this file then in that line it will go till end and puts value in different attributes eg

attr1 = fdxE1ConfigAdminStatus
attr2 = int
attr3 = 1 and 2 [possibly an array]

then it goes to second line and do the same and so on

I wrote follwing program

if [ "$1" = "" ]; then
echo "$0 <attr filename >";
exit;
fi
while read line; do
status="";
echo $line;
done

First traversal of loop containes fdxE1ConfigAdminStatus int 1 2
Now I want to remove spaces after fdxE1ConfigAdminStatus, int, 1 and 2 and put them in different attributes.

This is bash script.

Could you all please help to me to find out a solution.

Thanks in advance
 
Old 08-26-2006, 10:41 AM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
This is difficult to do in bash, that is getting it into variables that way. Maybe there's an alternate means to get to your end result, if you could explain where the data is eventually going to end up.

In most cases to process this type of input you'll need to read the data, parse it and save it to a temporary file... then do something with the temporary file.

If you can guarantee that the input data is a limited number of lines then reading it into arrays is possible, but this breaks down when the input file can be large without bounds.
 
Old 08-26-2006, 06:52 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you know Python, you can do it easily

>>> example = "fdxE1ConfigAdminStatus int 1 2"
>>> split_example = example.split(" ",2)
>>> print split_example
['fdxE1ConfigAdminStatus', 'int', '1 2']
>>> attr1,attr2,attr3 = split_example
>>> print attr1
fdxE1ConfigAdminStatus
>>> print attr2
int
>>> print attr3
1 2
>>>
 
Old 08-26-2006, 08:37 PM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,955

Rep: Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875
I think you can do it like this:

You should have the line containg your string in the variable $line but you already managed to do that.

Code:
i=0
for args in $line
do
        arg[$i]=$args
        let "i = i+1"
done
# access array members in curly brackets
echo "${arg[1]} ${arg[2]}"
You don't have to use and array, if you know the variable where you want to store something beforehand.

jlinkels
 
Old 08-27-2006, 05:00 AM   #5
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
#!/bin/bash
if [ "$1" = "" ]; then
echo "$0 <attr filename >";
exit;
fi
echo "Method 1"
while read line; do
status=""
ARRAY=( ${line} )
#LEN=${#ARRAY[*]}
attr1=${ARRAY[0]}
attr2=${ARRAY[1]}
attr3=( ${ARRAY[@]:2} )
echo "attr1 = $attr1"
echo "attr2 = $attr2"
echo "attr3 = ${attr3[@]}"
done <file

#The second method
echo "Method 2"
while read attr1 attr2 attr3; do
status=""
echo "attr1 = $attr1"
echo "attr2 = $attr2"
echo "attr3 = $attr3"
done <file
 
Old 08-27-2006, 05:26 AM   #6
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
You can also have it read into an array immediately:
Code:
#!/bin/bash

while read -a ARRAY
do
  echo "attribute: ${ARRAY[0]}"
  echo "type: ${ARRAY[1]}"
  echo -n "values: "
  for i in $(seq 2 $(( ${#ARRAY[*]} - 1)) )
  do
    echo -n "'${ARRAY[$i]}' "
  done
  echo
done < filename
 
Old 08-27-2006, 04:24 PM   #7
pawan_songara
LQ Newbie
 
Registered: Aug 2006
Posts: 6

Original Poster
Rep: Reputation: 0
string Parser

Spirit receiver,

Script is running the way I want I just wanted to know the meaning of following line


for i in $(seq 2 $(( ${#ARRAY[*]} - 1)) )


Thanks in advance

and thanks to all other also who chip in to help
 
Old 08-27-2006, 08:29 PM   #8
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,955

Rep: Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875
Spirit,

I know a little bit about bash programming, enough to write my scripts and give an advise to someone now and then.

But this statement:
Code:
for i in $(seq 2 $(( ${#ARRAY[*]} - 1)) )
makes me realize that I just started to learn.

This is the most obfuscated statement I ever saw. LOL!

jlinkels
 
Old 08-28-2006, 05:26 AM   #9
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
for i in $(seq 2 $(( ${#ARRAY[*]} - 1)) )
${#ARRAY[*]} <- This gives you the number of elements in ARRAY.
$(( ${#ARRAY[*]} - 1)) ) <- This gives you the number of elements -1 (since array elements start at 0.)
So ${ARRAY[2]} would be element #3.
So the for loop is count from array element #3 to the last element.

This whole section:
for i in $(seq 2 $(( ${#ARRAY[*]} - 1)) )
do
echo -n "'${ARRAY[$i]}' "
done

Can be replaced by one line:
echo "${ARRAY[@]:2}"
Which means everything from the 3rd (remember arrays are 0 based) element to the last.
 
Old 08-28-2006, 05:35 AM   #10
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33

I also realized that it looks a little intricate, but that's what I came up with immediately from what I wanted at that point:
The 'values' are stored separately in the components of ARRAY, beginning wih ARRAY[2]. So, to put each value in quotes and print it, I need to loop over the indices of ARRAY, beginning with 2.
That's essentially what
Code:
for i in $( seq 2 ${#ARRAY[*]} )
would do. Unfortunately, ${#ARRAY[*]} doesn't give the final index but the total number of values, so that loop would end up printing an additional ''. We need to make one iteration less, and $(( ${#ARRAY[*]} - 1 )) subtracts 1 from the value of ${#ARRAY[*]} . It's as simple as that. One could have used some temporary variables to make it more readable instead of putting everything into a single line, but I'd consider that as obfuscation.
After all, the only point of my previous post was to mention the -a switch of read.

Edit: Ouch, this time I was really slow.

Last edited by spirit receiver; 08-28-2006 at 05:38 AM.
 
Old 08-29-2006, 06:04 AM   #11
pawan_songara
LQ Newbie
 
Registered: Aug 2006
Posts: 6

Original Poster
Rep: Reputation: 0
String Parser

Thanks .bin/bash and spirit

Is it possible to check in the for loop that i will always be more than 2 then only go into for loop otherwise not.

Because what is happening is - when array is of size 2
the for loop is trying to execute for i == 2 nd i == 1
What I want is loop shud execute for values which are more than two.

Thanks in advance
 
Old 08-29-2006, 07:32 AM   #12
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
I don't see your problem, the for loop is not executed if the array contains less than three elements. But of course you can add a test like this:
Code:
  if [ "${#ARRAY[*]}" -ge 3 ]
  then
  [...]
  fi
I think you should go through one of the Bash scripting guides like this: http://www.tldp.org/LDP/abs/html/
 
Old 08-29-2006, 10:52 PM   #13
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
This would also work:
[ -z ${ARRAY[@]:2} ]
 
Old 08-30-2006, 06:05 PM   #14
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
Is this what the OP wanted?
Code:
while read attr1 attr2 attr3; do
    echo "attr1 is $attr1"
    echo "attr2 is $attr2"
    echo "attr3 is $attr3"
done
?
 
Old 08-30-2006, 09:20 PM   #15
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
Yeah thats also in post #5 above. Method #2
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
problems in removing white spaces from string of text monil Programming 7 03-08-2005 11:28 AM
Bash - Strip Spaces from string then cat cmfarley19 Programming 8 07-25-2004 12:01 PM
Removing spaces from string qcoder Programming 3 07-05-2004 12:35 PM
spaces in a string Longinus Programming 18 03-08-2004 06:02 PM
need to innitialize string variable with multiple spaces clsonnt Programming 3 08-11-2003 10:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:16 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration