Problem with searching and then editing a file through shell.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with searching and then editing a file through shell.
# 1  
Old 05-27-2011
Problem with searching and then editing a file through shell.

Hi,

I have searched through this forum as there are many similar entries but could'nt get one of them to work, either that or they were just different to what I needed.

Basically I have a file, recordsDatabase. In this file are a few different fields. There is a unique identifier eg 001 which is a variable the user enters into the application.

I then want to search through the file for the unique identifier field and once the right line has been found prompt the user to enter new information which will then write over the old record and save.

The only part of the code I am struggling with is the part to actually open and edit the file.. I know sed is definately the way forward but I can't for the life of me figure out how to do it...

Any help would be greatly appreciated.

Mikey
# 2  
Old 05-27-2011
Can you post your sample file and the output required
# 3  
Old 05-27-2011
Hi,

Code:
 
function amendRecord() {
clear
 
        read -p "Please enter the Staff ID of the record you wish to edit: " strID
        output=$(grep "$strID" recordDatabase)
 
        strID=$(echo $output | cut -d ":" -f1)
        strForename=$(echo $output | cut -d ":" -f2)
        strSurname=$(echo $output | cut -d ":" -f3)
        strProf=$(echo $output | cut -d ":" -f4)
        strTech=$(echo $output | cut -d ":" -f5)
        strComm=$(echo $output | cut -d ":" -f6)
 
                        if [[ -z "$output" ]] ; then
                                echo "You have entered an invalid ID, please try again."
                                sleep 2
                                amendRecord
                        fi
                        if [[ -n "$output" ]] ; then
                                read -p "The record: ($output) is about to be amended.. Press Y to continue or N to cancel: " yesNo
                                        case $yesNo in
                                                [Yy]) echo "Editing Record"
                                                        #EDIT CODE GOES HERE!!! ;;
                                                [Nn]) echo "Cancelling edit.."
                                                      sleep 0.5
                                                      loadWelcomeScreen ;;
                                                *)    echo "You did not press Y or N, please try again.."
                                                      sleep 0.5
                                                      amendRecord ;;
                                        esac
                        fi
 
 
 }

Hope that helps.

Last edited by U_C_Dispatj; 05-27-2011 at 05:44 AM.. Reason: spelling!
# 4  
Old 05-27-2011
I think as per your code, it seems that the strID is unique inside the recodedatabase file, i.e. we will have only one record having strID.

In this case, if you aren't bothered about the record ordering, you could simply remove the row which having strID using grep -v and add the new row at the end of recorddatabase file.

you can populate the new row in sequence of read command like

Code:
read -p "Enter surname : " surname
read -p "Enter tech: " tech
new_rec = `echo $surname \: $tech`

But if you are allowed to edit strID, then you may have to check for duplicates.

Please let us know if we are assuming anything wrong
This User Gave Thanks to kumaran_5555 For This Post:
# 5  
Old 05-27-2011
Hi, yeah I had a play over last hour or so, the best way I figured out was to use SED and delete the whole line, then to just call the function which inputs new data and do it as such... would be nice if i could simply get it to delete the line then add data back into the same line..

And yes, strID is a unique identifier, there are checks to make sure no two are the same.

thanks for your reply !
# 6  
Old 05-27-2011
If you want the new record to be in the same line, then you can use awk to do that. or few head and tail commands with line number will help.

I am giving a simple example with awk here,

Code:
user@host> (/home/user/c_test) $
user@host> (/home/user/c_test) $
user@host> (/home/user/c_test) $ cat test.txt
one
two
three
fourth
fifth
sixth

this was similar to your recorddatabase file, i am trying to enter new value instead of three
Code:
xti@host> (/home/user/c_test) $ awk '{if($0~/^three/){system("read -p \"Entre : \" temp;echo $temp")}else {print $0}} ' test.txt >new.txt
Entre : Hello

Now the new.txt file will have new records
Code:
user@host> (/home/user/c_test) $ cat new.txt
one
two
Hello
fourth
fifth
sixth

user@host> (/home/user/c_test) $

This User Gave Thanks to kumaran_5555 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell quoting problem while editing a remote file using sed

value of i = solarisbox ssh $i "cat /etc/hosts | sed "s/$i\.local\.//" | sed "s/$i\./$i/" | sed "s/$i/$i.sol.com/" > /usr/users/chidori/edit_hosts"While running the above one liner its i am not able to make the changes and write it to the file /usr/users/chidori/edit_hosts . I know there is a... (2 Replies)
Discussion started by: chidori
2 Replies

2. Shell Programming and Scripting

problem in using sed command in editing a file

Hi all, I have a conf file, i want to update some entries in that conf file. Below is the code for that using a temporary file. sed '/workgroup=/ c\workgroup=Workgroup' /usr/local/netx.conf > /usr/local/netx.conf.tmp mv -f /usr/local/netx.conf.tmp /usr/local/netx.conf Sample contents of... (9 Replies)
Discussion started by: ranj14r
9 Replies

3. Shell Programming and Scripting

editing a file using shell script

HI all, I have file in the below format 1111111111_222222222_3333333 111111_22222_33333 11111111_222222_33333333333 i need to display this file like this 2222222_1111111111 22222_11111111 22222222222_1111111111111 can anyone help me with this Thanks in advance (1 Reply)
Discussion started by: saravanan71184
1 Replies

4. Shell Programming and Scripting

editing excel file through shell script

Hi, I am having a business file in excel having charts based on data already present on it. I would like to add new rows after the existing data and refesh the chart on it using shell script. For example-- In excel file in "sheet1", There is some data in first 10 rows ( from column A to F).... (0 Replies)
Discussion started by: sanjay1979
0 Replies

5. Shell Programming and Scripting

problem while searching in a file by 'AWK'

Hi , i am writing a script in which i am using following command to grep some string and then storing it's output to v, as like below :- v=$(awk -F, '{ if ( $NF ~ /DEV/ ) print $0 "_BLD01";else print $0 "_RC01" }' mytest) Here i am facing following issues:- 1. it is searcing DEV in the... (1 Reply)
Discussion started by: inderpunj
1 Replies

6. Shell Programming and Scripting

editing a file via a shell script ??

Morning All: I know this might be easy but since I don't do this very often I get stumped real quick... Sun box Solaris 8 ksh... I need to edit a file via a shell script. In this file I need to locate one specific line and then remove that line plus the next 20 line below that.... Any... (2 Replies)
Discussion started by: jimmyc
2 Replies

7. Shell Programming and Scripting

SED - editing file names (End of line problem?)

For lists in sed, to say what to replace, is this correct: I am hoping that this would recognise that either a "." is present, or that the substitution happens at the end of the line. For files with extensions , my script works perfectly. My problem is, files without extentions, i.e. . ... (1 Reply)
Discussion started by: busillis
1 Replies

8. Shell Programming and Scripting

searching and editing file contents

Can you please help me to edit parts of a file and write into a new file. ===================================== Suppose I have a huge data dump in a file I need to search for a tag in that and cut few lines around that tag in the file. Is there a way to keep track of line numbers and operate on... (18 Replies)
Discussion started by: jayana
18 Replies

9. UNIX for Dummies Questions & Answers

Editing a file in a shell script

Using Solaris 8. I need to create a shell script that will edit a text file. I need to look in the text file and do a search and replace. For instance, the text file name is always 'filename'. I need to open filename and replace every instance of 'oldtext' with 'newtext'. 'oldtext' is static. ... (3 Replies)
Discussion started by: jowpup
3 Replies

10. UNIX for Dummies Questions & Answers

problem editing big file in vi

I have a big file, which vi opens it with message not sufficient space with file system. I am not adding any data in the file but changing some values within. To make these changes effective, it asks for forced write (w!), even after doing this, I see this particular record, change is not... (4 Replies)
Discussion started by: videsh77
4 Replies
Login or Register to Ask a Question