How to edit specific variable in file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to edit specific variable in file?
# 1  
Old 07-17-2010
How to edit specific variable in file?

HI guys i have a question.
Question 1: how do i modify a particular string?
e.g
Code:
echo "Please enter Book Title: "
    read a
    echo "Please enter Author: "
    read b
if [[ $(grep "^$a:$b" file.txt | wc -l) -gt 0 ]]
    then    echo " Record found!"

which will then pop out a menu with the follow output
Code:
1. Update Name
2. Update email
3.Update age
4. Update Marital staus

The question is, what codes do i haave to put in in order to change only the searched record shown?
e.g
Code:
Please enter Name: Simon
Please enter email: username@email.com
Record found!
Update name : Alex ( option 1)
Updated email: username6@gmail.com (option 2)

edited file.txt should have
Code:
Alex:username6@gmail.com:17:Married
Desmond:username4@email.com:18:Single
Harry:username12@email.com:21:Married
Alex:username1@email.com:78:Married

my file.txt has
Code:
Simon:username@email.com:17:Married
Desmond:username4@email.com:18:Single
Harry:username12@email.com:21:Married
Alex:username1@email.com:78:Married

am using bash script.
# 2  
Old 07-19-2010
if you are asking how will you edit the particular string if the record is found. then you want to take the full line from file.txt then below thing will come in handy.

Code:
echo "Please enter Book Title: "
    read a
    echo "Please enter Author: "
    read b
if [[ $(grep "^$a:$b" file.txt | wc -l) -gt 0 ]]
    then    
echo " Record found!"
for line in file.txt  ###this is the file that contains all the 4 records
do
line >> out_file
done
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing specific and incrementing lines of text from file via variable

This is part of a larger script where I need to pass only 1 line of a file to the script, based on a variable and not a direct reference. As part of a for loop : # for((line=0;line<50;line++)); do # awk ‘NR==$line' PhraseList.txt; done ... (5 Replies)
Discussion started by: Seth
5 Replies

2. Shell Programming and Scripting

Update a specific field in file with Variable value based on other Key Word

I have an input file with A=xyz B=pqr I would want the value in Second Field (xyz or pqr) updated with a value present in Shell Variable based on the value passed in the first field. (A or B ) while read line do NEW_VALUE = `some functionality done on $line` If $line=First Field-... (1 Reply)
Discussion started by: infernalhell
1 Replies

3. UNIX for Beginners Questions & Answers

Selecting specific variable in log file

Hi there I am trying to look for a specific word in the log file and I am aware this can be done by grep for example. As there will be multiple entries for this I want to grep the last one to enter the log... how would I go about this - would I have to use tail? Thanks in advance Alex (4 Replies)
Discussion started by: simpsa27
4 Replies

4. UNIX for Dummies Questions & Answers

To count total of specific character in a file and save its value to a variable

Hi all, I have a file that contains characters. How do I get total of spesific character from that file and save the count to a variable for doing for calculation. data.txt 1 2 2 2 2 3 3 4 5 6 7 8 5 4 3 4 (5 Replies)
Discussion started by: weslyarfan
5 Replies

5. Shell Programming and Scripting

How to merge variable data from another file into specific place?

Hello, I'm trying to create multiple commands using a variable input from another file but am not getting any successful results. Basically, file1.txt contains multiple lines with single words: <file1.txt> yellow blue black white I want to create multiple echo commands with these... (8 Replies)
Discussion started by: demmel
8 Replies

6. Shell Programming and Scripting

ksh script trying to pass a variable to edit a file

I'm trying to create a ksh script that will ask the user for the port number. $PORT1 is the variable I want to use that will contain whatever numbers the user inputs. The script would edit ports.txt file, search and delete "./serv 110.1.0.1.$PORT1 200;=3" . So if the user types 50243 then the... (5 Replies)
Discussion started by: seekryts15
5 Replies

7. Shell Programming and Scripting

Edit variable in a file

Hello, I have a script which reads variable values from /system/pt.conf I sourced it already. But the probelm is I don't know how to edit only the value from the variable "res" in the conf file. pt.conf: res=1280x720 log=1 And I want to edit the value "1280x720" from "res". I was... (5 Replies)
Discussion started by: FD1999
5 Replies

8. UNIX for Dummies Questions & Answers

Deny to edit a specific file in sudoers

How do I deny a user to edit a specific file in directory but the user will have a capability to use sudo and execute any command? I will just deny him/her to edit sayy 5files in different directories in linux? example. He cannot edit /etc/modprobe.d/blacklist.conf and /etc/sshd.config? Then the... (6 Replies)
Discussion started by: lhareigh890
6 Replies

9. Shell Programming and Scripting

[solved] how to get specific env variable values into a file

greetings! how do i get an env variable that looks like this when echoed: into a file that looks like this: keeping in mind that the two constants are the fields from the env variable will always be in odd positions of the string that need to go into the file AND they will always start... (3 Replies)
Discussion started by: crimso
3 Replies

10. Shell Programming and Scripting

Edit file content at the specific line.

How to edit file content at the specific line? For example at below The things to edit --> This is line 2. And it is below line 1. This is line 1. This is line 2. # i want to append some words at this row line. How? This is line 3. (8 Replies)
Discussion started by: alvin0618
8 Replies
Login or Register to Ask a Question