Ollie Pis Ollie Pis - 2 months ago 7
Bash Question

copy text in another file and append different strings shell script

file=$2
isHeader=$true
while read -r line;
do
if [ $isHeader ]
then
sed "1i$line",\"BATCH_ID\"\n >> $file
else
sed "$line,1"\a >> $file
fi
isHeader=$false
done < $1

echo $file


In the first line I want to append a string and to the others lines I want to append the same string for the rest of the lines. I tried this but it doesn't work. I don't have any ideas, can somebody help me please?

Answer

Not entirely clear to me what you want to do, but if you simply want to append text at the end of each line, use echo in place of sed:

file=$2
isHeader=1
while read -r line;
do
  if [ $isHeader ]
  then
    #sed "1i$line",\"BATCH_ID\"\n >> $file
    echo "${line},\"BATCH_ID\"\n" > $file
  else
    #sed "$line,1"\a >> $file
    echo "${line},1\a" >> $file
  fi
  isHeader=0
done < $1

cat $file
Comments