
November 8th, 2012, 10:22 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 34 m 54 sec
Reputation Power: 0
|
|
|
Replace a line in a file
Hi All,
I have a quick question wrt to a specific scenario in UNIX where I am trying to loop through lines of a file and trying to replace the line with another
File :
abcd.sh:501:/export/home/test/scripts_test:N
load.sh:501:/export/home/test/scripts_test,UK:N
unload.sh:501:/export/home/test/scripts_test,UK,501:N
defs.sh:501:/export/home/test/scripts_test,UK,501:N
hjkl.sh:501:/export/home/test/scripts_test,UK,501:N
uio.sh:501:/export/home/test/scripts_test,UK,501:N
inmu.sh:501:/export/home/test/scripts_test,UK,501:N
Requirement:
Read each line of the file and based on the status of the script execution in field 1 of the above file update the last field in the file to F or S (failed or success) instead of "N"
The code should replace the current line in the file and not append to the file
I tried the sed -i option and it does not work and errors out.I tried an awk and it dint work as well
The Shell is a KSH
Code snippet I am using:
while read line
do
./$script_name $params
# echo $?
if [ $? -ne 0 ]
then
echo "Inside Updating status F"
awk NR==$line '{sub( "N" ,"F" ); print }' file.txt
done < file.txt
Looking forward to suggestions
Thanks
|