LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to tokenize string in shell script (https://www.linuxquestions.org/questions/programming-9/how-to-tokenize-string-in-shell-script-4175430201/)

yahoosam 10-02-2012 11:53 PM

how to tokenize string in shell script
 
have a file likewist:
Code:

ip=
user=
pass=

i want to add values in it through shell script.

nugat 10-03-2012 12:18 AM

Quote:

Originally Posted by yahoosam (Post 4795633)
have a file likewist:
Code:

ip=
user=
pass=

i want to add values in it through shell script.

Hi,

Do you mean something like this?
Code:

#!/bin/bash

# define values here
ip='1.2.3.4'
user='joeblow'
pass='secret'

# the file you have
file='file.txt'
! [ -f $file ] && echo "$file: No such file" && exit 1

# insert values
sed -i "s|^ip=.*$|ip=$ip|" $file || exit 1
sed -i "s|^user=.*$|user=$user|" $file || exit 1
sed -i "s|^pass=.*$|pass=$pass|" $file || exit 1

you would put the above code in an editor, save it to a file, say "script.sh", then make it executable:

Code:

chmod +x ./script.sh
make sure it is in the same dir as your text file (I am assuming it is called "file.txt", and then call the script like this:

Code:

./script.sh
it should find the text file, and insert whatever values you populate at the top of the script into your text file in the appropriate locations.

yahoosam 10-03-2012 01:35 AM

giving you full description of my file:
Code:

default=0
title 1 : C
ip=1.2.3.4
user=c
pass=home

title 2 : C++
ip=11.22.33.44
user=c++
pass=office

i just want to create another title named java & edit the same file.
like:
title 3 : java
ip=100.200.300.400
user=java
pass=home & office

thanx

catkin 10-03-2012 03:02 AM

Code:

#!/bin/bash

echo 'title 3 : java
ip=100.200.300.400
user=java
pass=home & office' >> myfile.txt


yahoosam 10-05-2012 01:34 AM

thnx catkin & nugat:
that was really a path making group steps.
but if in case we try to put the same set of values written in file & put it back at the end.
then inserting values for new entries.
can you please suggest set of ways..
it kind of "NO SELF WRITING" on the file

ip_address 10-05-2012 04:04 AM

try something like this -

# Input file

Code:

$ cat tokenize.txt

ip=
user=
pass=

# Bash script

Code:

#!/bin/bash

filename="$1"

entries=(10.16.102.34 java work)

counter=0
while read line;do
       
echo "$line" ${entries[counter]}
counter=$((counter+1))

done < "$filename" > output_"$filename"

# Usage

Code:

$ ./tokenize.sh tokenize.txt
# Output

Code:

$ more output_tokenize.txt

ip= 10.16.102.34
user= java
pass= work


nugat 10-05-2012 08:57 PM

Quote:

Originally Posted by yahoosam (Post 4797792)
thnx catkin & nugat:
that was really a path making group steps.
but if in case we try to put the same set of values written in file & put it back at the end.
then inserting values for new entries.
can you please suggest set of ways..
it kind of "NO SELF WRITING" on the file

sorry, i didn't get this. can you explain it more, and use examples of what you have and what you want?

theNbomr 10-09-2012 09:29 AM

You've used the phrases 'i want to add values', 'create another title' & 'inserting values for new entries'. No where have you said where the script/program is supposed to get the new data. As this is somewhat central to the overall problem, it makes sense to describe how your data is to be acquired. It could come from many places, such as being read from a file, be embedded in the script/program, read from a commandline, read from standard input, read from a network connection of some sort, read from environment variables... The list is long. Sample data would be a very good thing.

--- rod.


All times are GMT -5. The time now is 10:59 PM.