LinuxQuestions.org
Review your favorite Linux distribution.
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 10-02-2012, 11:53 PM   #1
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Rep: Reputation: Disabled
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.
 
Old 10-03-2012, 12:18 AM   #2
nugat
Member
 
Registered: Sep 2012
Posts: 122

Rep: Reputation: 31
Quote:
Originally Posted by yahoosam View Post
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.
 
Old 10-03-2012, 01:35 AM   #3
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Original Poster
Rep: Reputation: Disabled
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

Last edited by yahoosam; 10-03-2012 at 01:36 AM.
 
Old 10-03-2012, 03:02 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
#!/bin/bash

echo 'title 3 : java
ip=100.200.300.400
user=java
pass=home & office' >> myfile.txt
 
Old 10-05-2012, 01:34 AM   #5
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-05-2012, 04:04 AM   #6
ip_address
Member
 
Registered: Apr 2012
Distribution: RedHat
Posts: 42

Rep: Reputation: 2
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
 
Old 10-05-2012, 08:57 PM   #7
nugat
Member
 
Registered: Sep 2012
Posts: 122

Rep: Reputation: 31
Quote:
Originally Posted by yahoosam View Post
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?
 
Old 10-09-2012, 09:29 AM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
  


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
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 10 07-17-2012 09:36 AM
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 1 07-13-2012 01:03 AM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
Tokenize a string in C++ russoue Linux - Software 2 01-11-2005 12:00 AM

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

All times are GMT -5. The time now is 09:53 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
Open Source Consulting | Domain Registration