1

I have a folder from part of my script output to tmp. The rev cut is necessary to remove parts of it that are not needed. I want to output each line separately to to part of my script I am using an array and I can get it to output the first line 2nd etc. if I put that in my [] as in ${myarray[0]}. What I really need is to have this section filled out for each line of the file.

The contents of tmp are like so.

C:\xxxx\DXF FILES\20038100.SLDPRT
C:\xxxx\DXF FILES\20136210.SLDPRT
C:\xxxx\DXF FILES\4_2-1.igs
C:\xxxx\DXF FILES\KC900.igs
C:\xxxx\DXF FILES\MetalSheet_Pusher.step
C:\xxxx\DXF FILES\Sheet Metal Part 8.igs

This is what I have so far.

#!/bin/bash

set -x
cat tmp | rev | cut -d"\\" -f1 | rev | cut -d '.' -f1 > 1.txt
    declare -a myarray
let i=0
while IFS=$'\n' read -r line_data; do
        myarray[i]="${line_data}"
        ((++i))
done < 1.txt
echo "<File>"${myarray[0]}"</File>" > out.txt
rm 1.txt

The output looks like this. and I need each line filled out in succession with the array. Thanks

<File>20038100</File>

@roaima Thanks for the help I really appreciate it. I should elaborate a bit further because the hard part is not getting this output but using it to fill out another section of the script. I already have an array to fill out a section for each file in the folder but it is just the location and works well enough using ls on the folder. This runs part of the script for every file in the folder. My issue is, I need to take the contents of the folder and put each section in 4 different places for each file in the folder. It is filling out an xml file that I need to run a batch from. Been scratching my head on this and I need to do it with Bash for now, might be able to go Python in the future I hope.

    while IFS=$',' read -r -a arry;
do
  echo '        <Part>
            <Input>' >> $file_out
  echo '                <File>'${arry[0]}'</File>' >> $file_out

The section below is where I need to place each line from tmp into 4 places in my script for each line in tmp. This can be many files or as little as 1 but I need 4 entries into my xml for each line in the original folder.

    cat tmp | rev | cut -d"\\" -f1 | rev | cut -d '.' -f1 > 1
   declare -a myarray
   let i=0
        while IFS=$'\n' read -r line_data; do
        myarray[i]="${line_data}"
        ((++i))
        done < 1
echo '                  <File>'${myarray[0]}'</File>
                        </NCFile>
                        <Graphics>
                            <Save>true</Save>
                                <Directory>C:\xxxx\OUTPUT\NC FILES</Directory>
                                <File>'${myarray[0]}'</File>
                        </Graphics>
                        <FlatPatternDXF>
                                <Save>true</Save>
                                <Directory>C:\xxxx\OUTPUT\DXF FILES</Directory>
                                <DXFSetting>xxxx</DXFSetting>
                                <File>'${myarray[0]}'</File>
                        </FlatPatternDXF>
                        <xxxxile>
                                <Save>true</Save>
                                <Directory>C:\xxxx\OUTPUT\xxxx FILES</Directory>
                        <File>'${myarray[0]}'</File>
                        </xxxxFile>
                        <ProcessDocumentation>
                            <Save>true</Save>
                                <Directory>C:\xxxx\OUTPUT\PDF FILES</Directory>
                                <File>'${myarray[0]}'</File>
                        </ProcessDocumentation>
                </SaveSettings>
                </Input>' >> $file_out

1 Answer 1

1

You can use readarray and avoid the entire input loop

readarray -t myarray <1.txt

But I think it would be easier to transform the file directly

sed -E 's!^.*[\\.]([^.]+)\..*!<File>\1</File>!' tmp >out.txt

cat out.txt
<File>20038100</File>
<File>20136210</File>
<File>4_2-1</File>
<File>KC900</File>
<File>MetalSheet_Pusher</File>
<File>Sheet Metal Part 8</File>
2
  • 1
    Thanks for your consistently high-quality answers. I don't understand why you use the character class [\\.] which I gather means "either a backslash or a period". I find the sed example works equally well without the .. Am I missing something?
    – Jim L.
    Commented Oct 29, 2021 at 21:36
  • 1
    @JimL it's matching on the first \ or . immediately before the part of the filename the OP wanted to extract. I was trying to reverse engineer their rev | cut | rev | cut expression Commented Oct 29, 2021 at 22:17

You must log in to answer this question.

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