1

I have comma separated (sometimes tab) text file as below:

parameters.txt:

STD,ORDER,ORDER_START.xml,/DML/SOL,Y
STD,INSTALL_BASE,INSTALL_START.xml,/DML/IB,Y

with below code I try to loop through the file and do something

while read line; do
if [[ $1 =  "$(echo "$line" | cut -f 1)" ]] && [[ "$(echo "$line" | cut -f 5)" = "Y"  ]] ; then
//do something...
if [[ $? -eq 0 ]] ; then
// code to replace the final flag
fi
fi
done < <text_file_path>

I wanted to update the last column of the file to N if the above operation is successful, however below approaches are not working for me:

  1. sed 's/$f5/N/'
  2. '$5=="Y",$5=N;{print}'
  3. $(echo "$line" | awk '$5=N')

Update: Few considerations which need to be considered to give more clarity which i missed at first, apologies!

  1. The parameters file may contain lines with last field flag as "N" as well.
  2. Final flag needs to be update only if "//do something" code has successfully executed.
  3. After looping through all lines i.e, after exiting "while loop" flags for all rows to be set to "Y"

3 Answers 3

3

perhaps invert the operations do processing in awk.

$ awk -v f1="$1" 'BEGIN             {FS=OFS=","}
                  f1==$1 && $5=="Y" { // do something
                                      $5="N"}1'  file

not sure what "do something" operation is, if you need to call another command/script it's possible as well.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, I have to change the flag only if "do something" command has successfully executed so the order would be as in code
1

with bash:

(
IFS=,
while read -ra fields; do
    if [[ ${fields[0]} == "$1" ]] && [[ ${fields[4]} == "Y" ]]; then
        # do something
        fields[4]="N"
    fi
    echo "${fields[*]}"
done < file | sponge file
)

I run that in a subshell so the effects of altering IFS are localized.

This uses sponge to write back to the same file. You need the moreutils package to use it, otherwise use

done < file > tmp && mv tmp file

Perhaps a bit simpler, less bash-specific

while IFS= read -r line; do
    case $line in 
        "$1",*,Y) 
            # do something
            line="${line%Y}N"
            ;; 
    esac
    echo "$line"
done < file

2 Comments

I tried this but after the execution the lines which already has "N" as the flag are missed out completely in the final file, perhaps i should have mentioned that file may contain lines with "N" as well, if its already "N" it should ignore it. Edited the question
I added else condition to your code and result is as expected,thanks.
0

To replace ,N at the end of the line($) with ,Y:

sed 's/,N$/,Y/' file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.