Shell script - Replace just part of a single line in a file.....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script - Replace just part of a single line in a file.....
# 1  
Old 12-12-2012
Shell script - Replace just part of a single line in a file.....

Hey guy's....

I new here,
But im working on a school project, and I am not really good at programming. In fact, this is the only programming class that I need because programming is not what I am majoring in.

But I have everything done in this shell script except for this last part.....

Basically I have a file of an employee record book,
Formatted like so:
Firstname Lastname Phone OnCallDay

ex:
Henry Test 000-000-0000 Wed

There are more people in my file though. I have a shell script made to make changes to the record book. (add, delete records). But I need to add to my shell script, to let a person change a record's on call day.

It must only prompt the user for the person's name, and then the new on call day. It can not ask for the user's old on call day. So I really have no idea I would search for just a part of a line. Like is there is some way to just edit the last 3 characters of a given line?

Also two people cannot have the same on call day. (I have already figured this part out)

Here is that part of the script so far:
Code:
echo "Enter the name of the employee, to change their on-call day: "
read name
echo "enter new on call day: "
read newday
days=`grep $newday phonebook | wc -l`   #This is to check availability day
if [ $days -ne 0 ]
then
   echo "Sorry there is already an employee on call for that day"
   exit
else
   and this is where I would need to somehow pull the previous day (Mon,wed,Tue,Thu,Fri,Sat,Sun) out of a line and change it to the $newday. But I have no idea where to start

I know that I would start here by doing a grep to search for the person's line. But then I need to change the 3char on-call day of that line.....


Could anyone help me figure this last part out?
Any help would be greatly appreciated.....
Thanks a lot

Last edited by hxdrummerxc; 12-12-2012 at 09:57 PM..
# 2  
Old 12-12-2012
Use awk:-
Code:
awk -v N=$name -v D=$newday '$0 ~ N { $NF=D; print $0; } ' record_book_file

# 3  
Old 12-12-2012
You can used 'sed' to update the 'day' on the record, for example:
Code:
$ name="Henry Test"

$ day="FRI"

$ echo 'Henry Test 000-000-0000 Wed' | sed "s/^\($name [0-9]\{1,3\}-[0-9]\{1,3\}-[0-9]\{1,4\}\) .../\1 $day/"
Henry Test 000-000-0000 FRI

# 4  
Old 12-12-2012
Quote:
Originally Posted by bipinajith
Use awk:-
Code:
awk -v N=$name -v D=$newday '$0 ~ N { $NF=D; print $0; } ' record_book_file

Thanks for the reply, Smilie

That seems to do exactly what I wan't, it's just not writing it to the file (my file named phonebook).

Code:
echo "Enter the name of the employee, to change their on-call day: "
read name
echo "enter new on call day: "
read newday
days=`grep $newday phonebook | wc -l`   #This is to check availability day
if [ $days -ne 0 ]
then
   echo "Sorry there is already an employee on call for that day"
   exit
else
   awk -v N=$name -v D=$newday '$0 ~ N { $NF=D; print $0 } ' phonebook
fi
echo "Success";;


I changed the on call day from Wed to Sun and it came up and showed
Henry Test 000-000-0000 Sun
But it didn't write it to the file. Im not familiar with awk, but could I make the awk line a variable to use to add to the file?
like:
add = awk -v N=$name ..... etc
echo "$add" >> phonebook

or would that not work?

Last edited by hxdrummerxc; 12-12-2012 at 10:52 PM..
# 5  
Old 12-12-2012
To write result to a file:-
Code:
awk -v N=$name -v D=$newday '$0 ~ N { $NF=D; print $0 } $0 !~ N { print $0 } ' phonebook > tmp
# mv tmp phonebook

Note: I am writing result to a temporary file: tmp and then I am renaming file: tmp back to file: phonebook. But I have commented out that line because first you have to check and verify if file: tmp is having desired result. If yes, then please un-comment it. I hope this helps.
# 6  
Old 12-12-2012
Quote:
Originally Posted by bipinajith
To write result to a file:-
Code:
awk -v N=$name -v D=$newday '$0 ~ N { $NF=D; print $0 } $0 !~ N { print $0 } ' phonebook > tmp
# mv tmp phonebook

Note: I am writing result to a temporary file: tmp and then I am renaming file: tmp back to file: phonebook. But I have commented out that line because first you have to check and verify if file: tmp is having desired result. If yes, then please un-comment it. I hope this helps.

Ok that makes sense now. Yep that works.....
Thanks a lot!.... that was killing me

One last question, Say I have my record book formatted right now. Is it hard to make awk format/space each part out?

Like have the firstname span to 10 spaces, the last name span to 10 spaces etc?

I was able to do that earlier in my script file (the part where you could add a user to the file) by using:

Code:
first=$(printf "%-16s" $first)
last=$(printf "%-13s" $last)

So that way multiple different length names will space out the same....
Ex:
Henry Test 000-000-0000 Sun
Timothy Lastname 000-000-0000 Sun


EDIT: Once again, thank you!
# 7  
Old 12-12-2012
Adjust the length as per your requirement:-
Code:
awk ' { printf "%s %-10s %s %s\n", $1, $2, $3, $4 } ' phonebook > tmp
mv tmp phonebook

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merge multi-lines into one single line using shell script or Linux command

Hi, Can anyone help me for merge the following multi-line log which beginning with a " and line ending with ": into one line. *****Original Log***** 087;2008-12-06;084403;"mc;;SYHLR6AP1D\LNZW;AD-703;1;12475;SYHLR6AP1B;1.1.1.1;0000000062;HGPDI:MSISDN=12345678,APNID=1,EQOSID=365;... (3 Replies)
Discussion started by: rajeshlinux2010
3 Replies

2. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

3. Shell Programming and Scripting

Single/Multiple Line with Special characters - Find & Replace in Unix Script

Hi, I am creating a script to do a find and replace single/multiple lines in a file with any number of lines. I have written a logic in a script that reads a reference file say "findrep" and populates two variables $FIND and $REPLACE print $FIND gives Hi How r $u Rahul() Note:... (0 Replies)
Discussion started by: r_sarnayak
0 Replies

4. Shell Programming and Scripting

replace part of text of a line

Gurus, You know, I believe you do:-), the comnand uname -r give you the kernel version: serverA:~# uname -r 2.6.26-1-xen-amd64So, I want to replace this output inside in the line below that is inside the file: kernel = '/boot/vmlinuz-2.6.26-1-xen-amd64'Suppose, you move this file to ther... (2 Replies)
Discussion started by: iga3725
2 Replies

5. Shell Programming and Scripting

Script to multiple find and replace in a single file

Dear all I need a script for multiple find and replace in a single file. For example input file is - qwe wer ert rty tyu asd sdf dgf dfg fgh qwe wer det rtyyui jhkj ert asd asd dfgd now qwe should be replace with aaaaaa asd should be replace with bbbbbbbb rty should be replace... (6 Replies)
Discussion started by: wildhorse
6 Replies

6. Shell Programming and Scripting

shell script to find and replace a line using a identifier

Hi all im having trouble starting with a shell script, i hope someone here can help me i have 2 files file1: 404905.jpg 516167 404906.jpg 516168 404917.psd 516183 404947.pdf 516250 file2: 516250 /tmp/RecyclePoster18241.pdf 516167 /tmp/ReunionCardFINAL.jpg 516168... (7 Replies)
Discussion started by: kenray
7 Replies

7. Shell Programming and Scripting

replace space or spaces in a line of a file with a single :

I am searching while I await a response to this so if it has been asked already I apologize. I have a file with lines in it that look like: bob johnson email@email.org I need it to look like: bob:johnson:email@email.org I am trying to use sed like this: sed -e 's/ /:/g' file >... (5 Replies)
Discussion started by: NewSolarisAdmin
5 Replies

8. UNIX for Dummies Questions & Answers

replace part of single string in a file

hi! i have a file consisting of the following lines: (BTW, = space) . . . 12ME_T1mapping_flip30bshortf 13DCE_whole_brainbshortf 13DCE_3Dbshortf . . . the list of scans starts at 1 and goes on sometimes up to 60 scans. i would like to change only the lines that contain 'whole' to... (2 Replies)
Discussion started by: nixjennings
2 Replies

9. Shell Programming and Scripting

how can i replace / with new line in shell script or sed ?

1. how can i replace ' / ' with new line in shell script or sed ? 2. how can set a conditon untill null in while loop while ( i== null ) do ...... done (3 Replies)
Discussion started by: mail2sant
3 Replies

10. UNIX for Advanced & Expert Users

how to replace a line in a file using shell script

I have a property file in which the DB name is specified and when i run my servers they will point to the DB specified in that property file. Now i'm gonna write a script which will start all the services. But before that i just want to dynamically change the DB name in that property file by... (3 Replies)
Discussion started by: cs_sakthi
3 Replies
Login or Register to Ask a Question