Dynamically modify data in file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dynamically modify data in file?
# 1  
Old 01-10-2011
Dynamically modify data in file?

Hiya fellas,

I have a data file which contains these fields...
EMPID
EMPNAME
GENDER
DESIGNATION
AGE
SALARY

this datafile looks like this..
Quote:
EMPID EMPNAME AGE GENDER DESIGNATION SALARY
101 EMPNAME1 25 M ADMIN 90000
102 EMPNAME2 22 F ASSITANT 20000
103 EMPNAME3 26 M PROGRAMMER 80000
Now, i'm making a script which will allow me to edit anything on execution.
ie. First it will ask which EMPID is to be modified.
and then it will ask which field should be modified.
and then with what it should modified.

I came up with (WHICH DOESN'T WORK)

Quote:
temp=$(mktemp tmp.XXXXX)
temp2=$(mktemp tmp.XXXXX)
temp3=$(mktemp tmp.XXXXX)
read -p "Enter the employee ID, you want to edit" empid
grep -v "$empid" < "$datafile" > $temp
grep "$empid" < "$datafile" > "$temp2"
read -p "Enter Field NO to edit: " fno
if test "$fno" = 1; then
olddata=`cat "$temp2" | awk '{print $1}'`
read -p "Enter new Empid" newempid
sed s/$oldata/$newempid/g < "$temp2" > $temp3
elif test "$fno" = 2; then
olddata=`cat "$temp2" | awk '{print $2}'`
read -p "Enter new name" newname
sed s/$oldata/$newname/g < "$temp2" > $temp3
elif test "$fno" = 3; then
olddata=`cat "$temp2" | awk '{print $3}'`
read -p "Enter new age: " newage
sed s/$olddata/$newage/g < "$temp2" > $temp3
elif test "$fno" = 4; then
olddata=`cat "$temp2" | awk '{print $4}'`
read -p "Enter Gender: " newgen
sed s/$oldata/$newgen/g < "$temp2" > $temp3
elif test "$fno" = 5; then
olddata=`cat "$temp2" | awk '{print $5}'`
read -p "Enter new Designation: " newdes
sed s/$oldata/$newdes/g < "$temp2" > $temp3
elif test "$fno" = 6; then
olddata=`cat "$temp2" | awk '{print $6}'`
read -p "Enter new salaray: " newsal
sed s/$oldata/$newsal/g < "$temp2" > $temp3
fi
mv "$temp3" "$temp2"
cat "$temp2" >> $temp
cat "$temp" | uniq > "$datafile"
rm "$temp"
;;
I am bound to solve this only within bash environment without perl. with sed and awk and any other command which may help..

PLEASE HELP! Smilie
# 2  
Old 01-10-2011
# 3  
Old 01-10-2011
No.. Exams.

Moderator's Comments:
Mod Comment Please repost in the Homework forum as suggested. Thanks.

Last edited by Scott; 01-10-2011 at 12:10 PM..
# 4  
Old 01-10-2011
Well, it does not work and executes sed and grep.

Should you be "modifying data in file" 'in place', not making a new file you then swap in place of the original? Writing the middle of one existing file is not the usual "shell" task, never mind all fields must be fixed length padded!

(If this is schoolwork, you need to follow the template and right forum rules.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Modify Column Data using Shell Script

HI Guys, Input :- P081 wr1 12p0d5: 22.8 P081 wr1 12p2d18: 23.1 P149 wr1 1pxcud6/port_0_dev_7: 20.4 P149 wr1 1pxcud4/port_1_dev_10: 22.4 OutputP081 wr1 120 22.8 P081 wr1 122 23.1 P149 wr1 10 20.4 P149 wr1 11 22.4 In in First two line delete p and after d untill : In Last two line... (4 Replies)
Discussion started by: pareshkp
4 Replies

2. Shell Programming and Scripting

How can I modify my script to include or substitute missing data?

Let me start off by saying I am a self taught sometimes scripter so what you will see below won't be pretty. I have created a script to parse through a file with a large amount of data and simply pull out what I need. In doing this I create several files and then paste them together in order to... (2 Replies)
Discussion started by: fsanchez
2 Replies

3. Shell Programming and Scripting

Dynamically creating file names from portions of another file name

Not sure how to do the following, but any help would be appreciated. Has to be done using C shell (sorry about that). I have about 300 files that I need this done for, but I am only going to give one example. I will just need to know how to execute your solution through some type of loop to get... (2 Replies)
Discussion started by: jclanc8
2 Replies

4. Shell Programming and Scripting

Shell Script to Dynamically Extract file content based on Parameters from a pdf file

Hi Guru's, I am new to shell scripting. I have a unique requirement: The system generates a single pdf(/tmp/ABC.pdf) file with Invoices for Multiple Customers, the format is something like this: Page1 >> Customer 1 >>Invoice1 + invoice 2 >> Page1 end Page2 >> Customer 2 >>Invoice 3 + Invoice 4... (3 Replies)
Discussion started by: DIps
3 Replies

5. Shell Programming and Scripting

Read data from .csv file through shell script & modify

I need to read data from a file called "test.csv" through shell script where the file contains values like name,price,descriptor etc. There are rows where descriptor (& in some rows name) are written as string & other characters like "car_+" OR "bike*" etc where it should contains strings like... (3 Replies)
Discussion started by: raj100
3 Replies

6. Shell Programming and Scripting

Modify log files to get uniq data

Hello, I have a log file that has following output as below. LAP.sun5 CC LAP.sun5 CQ perl.sun5 CC perl.sun5 CQ TSLogger.sun5 CC TSLogger.sun5 CQ TSLogger.sun5 KR WAS.sun5 CC WAS.sun5 MT WAS.sun5 CQ I want to output to be in the way below, i tried using awk but could not do it. ... (12 Replies)
Discussion started by: asirohi
12 Replies

7. Shell Programming and Scripting

Need to dynamically create a file

I'm trying to write a script that generates a file of varying size filled with random text and am having some difficulties. The major problems are that I'm limited to csh and a lot of the systems I have to run this on don't have a /dev/random (so a wrapper around the dd if=/dev/random or dd... (14 Replies)
Discussion started by: stareja
14 Replies

8. Shell Programming and Scripting

Spliting the file dynamically

i am creating the file , when this file reaches the size 2 GB, i need one message or fire (4 Replies)
Discussion started by: kingganesh04
4 Replies

9. UNIX for Advanced & Expert Users

dynamically linked file

Hi friends, i have a dynamically linked file on my solaris system.this is script that runs regularly. How can i read the contents of that ? when i tried to say "vi filename " then it says executable and nothing is seen. Please help. thanks in advance Veera (5 Replies)
Discussion started by: sveera
5 Replies

10. Shell Programming and Scripting

dynamically linked file

Hi friends , how do i view a dynamically linked file in unix ? its there on other system and do i have to ftp it in ASCII format or binary ? and after the ftp how do i view it ? thanks in advance veeras (1 Reply)
Discussion started by: sveera
1 Replies
Login or Register to Ask a Question