Register
It is currently Wed Sep 17, 2014 3:33 pm

Amending fields in a txt file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Tue Nov 15, 2011 8:06 am   

Joined: Fri Nov 11, 2011 1:40 pm
Posts: 8
Hi guys,

I'm now trying to write a script which amends a specified field in a line and replace that old text with a newly specified one. The code is incomplete but I have the following:

Code:
#!/bin/bash

echo "######################### Amend Record ########################"

echo

echo "Please enter the username of the record you wish to amend"
read username

if grep $username information_file
then
   echo "Which field of user would you like amended?"
   echo
   echo "1) Username"
   echo "2) Address"
   echo "3) E-mail address"
   echo "4) Telephone extension number"
   echo "5) Manager username"
   echo "6) Start date"
   echo "7) End date"

   read option

   case $option in
    1) echo "Please enter new username you wish to replace old"
       read new_username
       `sed 's/$username/$new_username/ information_file`
       ;;
    esac
else
   echo "Username is not found in Information File! Now closing"
   exit 1
fi


For now, I have an EOF error occurring somewhere in the Case statement and would like to know what it is I'm doing wrong. Hope you can help. Thanks!


Top
 Profile  
 PostPosted: Tue Nov 15, 2011 11:01 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
The regular expression for sed is missing the right quote. Also, consider double-quoting $username in grep (what happens if username is empty?); what if someone's username is the same as someone other's e-mail? Using the output of sed as a command is also probably something you do not want to do.


Top
 Profile  
 PostPosted: Tue Nov 15, 2011 4:37 pm   

Joined: Fri Nov 11, 2011 1:40 pm
Posts: 8
Thanks Choroba. That got rid of the errors. Now I just gotta find out why the sed command isn't substituting the text :))


Top
 Profile  
 PostPosted: Thu Nov 17, 2011 9:58 am   

Joined: Tue Sep 27, 2011 12:45 pm
Posts: 8
Put single quotes around your variables in the sed command.
Code:
sed 's/'${username}'/'${new_username}'/' information_file


Top
 Profile  
 PostPosted: Thu Nov 17, 2011 5:27 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 546
or use regular/double quotes
Code:
sed "s/${username}/${new_username}/" information_file

and don't put sed command inside command substitution.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP