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!
![Question Smilie](https://www.unix.com/images/icons/icon51.png)
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
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
2. UNIX for Advanced & Expert Users
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
3. Shell Programming and Scripting
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
4. Shell Programming and Scripting
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
5. Shell Programming and Scripting
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
6. Shell Programming and Scripting
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
7. Shell Programming and Scripting
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
8. Shell Programming and Scripting
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
9. Shell Programming and Scripting
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
10. UNIX for Dummies Questions & Answers
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
IGAWK(1) Utility Commands IGAWK(1)
NAME
igawk - gawk with include files
SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ...
igawk [ all gawk options ] [ -- ] program-text file ...
DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1).
AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like
@include getopt.awk
in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path.
OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports.
EXAMPLES
cat << EOF > test.awk
@include getopt.awk
BEGIN {
while (getopt(ARGC, ARGV, "am:q") != -1)
...
}
EOF
igawk -f test.awk
SEE ALSO
gawk(1)
Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995.
AUTHOR
Arnold Robbins (arnold@skeeve.com).
Free Software Foundation Nov 3 1999 IGAWK(1)