I have the following file in the S3 bucket:
field1;field2;field3
VAL11,VAL12;VAL13
VAL21,VAL22;VAL23
VAL31,VAL32;VAL33
The final goal is to remove only the first line of the file field1;field2;field3
. This is the desired result:
VAL11,VAL12;VAL13
VAL21,VAL22;VAL23
VAL31,VAL32;VAL33
For the moment I have the following function that is reading all the content form the file. And breaking down using \n
, removing the first line and then concatenating all again using \n
.
So is there a way to remove this first line without breaking down and assembling again?
async function createRef (version,FIRMWARE_BUCKET_NAME) {
const refFile = version + '/ref.ref'
const ref = await getObject(FIRMWARE_BUCKET_NAME, refFile)
const refString = ref.toString('utf8')
let arrRef = refString.split('\n')
arrRef.shift()
let refConcatenated=''
for (let i = 0; i < arrRef.length; i++) {
if (arrRef[i] !== '' ) {
let line = arrRef[i]
refConcatenated = refConcatenated + line + '\n'
}
}
return refConcatenated
}
It's not possible to remove one line without re-reading and writing the whole file in some way — you can replace lines as in this answer, or use streaming to avoid needing to read the whole file into memory as in this answer. If you wanted to truncate the file you could use fs.truncate, but that won't help you for removing the first line. You can at least probably simplify last part of your function by using .join('\n')
rather than looping.