#!/bin/bash
# COMP 198  Spring 2026
# bash10.sh
# Read a file (command line argument) with at least 3 items per line
# and create a new file ("newfile") with only one item per line

# first check if "newfile" exists
checkfile="newfile"
file=`ls | grep newfile`
if [[ $file == "newfile" ]];     # == for comparing strings!
then
   rm newfile
fi

# read file and put elements in "newfile"
while read -a A;                 # reads one line, splitting items (-a) into array A
do
   for i in 0 1 2                
   do
      echo $i " " ${A[$i]} >> newfile   # output one item at a time to file
   done
done < $1                        # continue reading lines until end of file
