2

I am using sed like this:

 sed -e 's/ *| */|/g'
   ${array_export_files[$loopcount]}>>$TEMPDIR/"export_file"_${testid}_${loopcount}_$$

under a while loop but problems come when the file is empty or contains nothing.

  1. I don't want the sed to run if the file exists but is empty;
  2. I don't want the sed to run if the file doesn't exist.

The complete code snippet is

while [ $loopcount -le $loopmax ]
do 
    if [ "$loopcount" -eq "$loopcount" ]
    then
        sed -e 's/ *| */|/g' ${array_export_files[$loopcount]}>>$TEMPDIR/"export_file"_${testid}_${loopcount}_$$
        tr "|" "\t" <"export_file"_${testid}_${loopcount}_$$>${array_export_files[$loopcount]}
        cp ${array_export_files[$loopcount]} "export_file"_${loopcount}_${testid}
        echo "Testing Starts Here"
        echo ${array_export_files[$loopcount]} "export_file"_${loopcount}_${testid}
        echo "Testing Ends Here"
    fi
  (( loopcount=`expr $loopcount+1`))
done    

So I can't replace or use AND operator on the above if statement is there anyway to solve that? if I use AND operator then it may skip the below whole code section will nor run I only wanted to skip the sed section conditionally.

10
  • You should make a new question with that new problem you introduce. Point 1 and 2 were answered. When you make the new question explain why you cannot replace or use AND operator, because that doesn't make sense. You can only replace what is already there (i.e. in use). What does [ "$loopcount" -eq "$loopcount" ] do? IMO that is always true.
    – Anthon
    Apr 1, 2015 at 11:24
  • @Anthon if I use AND operator then it may skip the below whole code section will nor run I only wanted to skip the sed section conditionally.
    – Aman
    Apr 1, 2015 at 11:27
  • @Anthon if you can use if inside the if statement mentioned it will be ok
    – Aman
    Apr 1, 2015 at 11:30
  • Please look at that if statement and explain me what it does. It test if X equals X. That is always true, just replace it.
    – Anthon
    Apr 1, 2015 at 11:35
  • @Anthon I am totally agree with you and I have question why you use ; after if condition and double then symbol means what does ; signifies.
    – Aman
    Apr 1, 2015 at 11:40

1 Answer 1

4

Bash has the -s option to test for existence and size greater than zero:

 -s file
          True if file exists and has a size greater than zero.

so you can do

if [ -s "${array_export_files[$loopcount]}" ]; then
   sed .......
fi

within the loop. Since the if [ "$loopcount" -eq "$loopcount" ] is always true you can just replace that:

while [ "$loopcount" -le "$loopmax" ]
do 
    if [ -s "${array_export_files[$loopcount]}" ]
    then
        sed -e 's/ *| */|/g' "${array_export_files[$loopcount]}" >>" $TEMPDIR/export_file_${testid}_${loopcount}_$$"
        tr "|" "\t" <"export_file_${testid}_${loopcount}_$$">"${array_export_files[$loopcount]}"
        cp "${array_export_files[$loopcount]}" "export_file_${loopcount}_${testid}"
        echo "Testing Starts Here"
        echo "${array_export_files[$loopcount]}" "export_file_${loopcount}_${testid}"
        echo "Testing Ends Here"
    fi
    (( loopcount = loopcount + 1 ))
done
8
  • Hi Anthon please see edited question your answer is correct but how to use it.
    – Aman
    Apr 1, 2015 at 11:04
  • additional if options (and how to use them) are listed here
    – Wilf
    Apr 1, 2015 at 11:14
  • @Aman, Just copy the line starting with if in the line before the one with the sed statement, and the line with fi after that line. If you update your question with the loop you are actually using, than I could have incorporated that wihtin the loop itself.
    – Anthon
    Apr 1, 2015 at 11:15
  • This will fail if file contains only blank lines.
    – cuonglm
    Apr 1, 2015 at 11:16
  • @cuonglm Why? echo -e '\n' > abcd ; if [ -s abcd ] ; then echo ok ; fi gives ok.
    – Anthon
    Apr 1, 2015 at 11:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .