#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep 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
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    Hi,

    I finally got this working using the

    sed -i "s/$line/$line1/g" file

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo