2

Is there an easier way to do the below? I am reading in a large cvs file and want to only display ranges on the index per line and output back into a csv format.

while  IFS=$',' read -r -a myArray
do
    echo ${myArray[44]},${myArray[45]},${myArray[46]},${myArray[47]},${myArray[48]},${myArray[65]},${myArray[66]},${myArray[67]}
done < $SHEET
0

3 Answers 3

4

You can use the substring operator with array parameter expansion:

while IFS=, read -r -a myArray
do
    ( IFS=,; echo "${myArray[*]:44:5},${myArray[*]:65:3}" )
done
Sign up to request clarification or add additional context in comments.

Comments

2
awk 'BEGIN{FS=OFS=","}{print $44, $45, $46, $47, $48, $65, $66, $67}' "$SHEET"

We set the Input Field Separator (FS) and Output Field Separator (OFS) to , so that output will be in csv format.

Comments

0

Looks like this is tailor-made job of awk. Use this awk command:

awk -F"," '{printf("%s,%s,%s,%s,%s,%s,%s,%s\n", 
            $44, $45, $46, $47, $48, $65, $66, $67)}' $SHEET

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.