Unable to compare to a previous value of a variable in a while loop for a file

Hello All,

I'm working on a script that will actually read a file consisting of data like the below:(ReportID,Sub_reportID,Sub_reportName)

1,1,ABC
1,2,DEF
1,3,GHI
2,1,JKL
2,2,MNO
3,1,PQR

I want to read the Sub Report details for a Report_ID using while loop and write these values into a file created by Report_ID wise.

In simple, I want the Sub Report names ABC & DEF & GHI to written into Report_ID_1.txt

JKL & MNO to Report_ID_2.txt
PQR          to Report_ID_3.txt

I have tried various methods implement the above scenario using the variables in the while loop but unsuccessful.

Can you please advise ?

Thanks in advance

Just having fun (and teaching the filtering way). Assumes the triples are in a file called reports.txt:

sort reports.txt | sed -e 's/^\([^,]*\),1,\(.*\)/echo \"\2\" >Report_ID_\1.txt/' -e 's/^\([^,]*\),[^,]*,\(.*\)/echo \"\2\" >>Report_ID_\1.txt/' | sh

Is that what you want? If the sub reports are actually filenames and you want the contents in the Report_ID files, change the echos above to cat.

If there are only a few ReportID's and the records are grouped together on the field ReportID, then this may be enough:

awk -F, '{print >("Report_ID_" $1 ".txt")}' file

Otherwise try something like this:

awk -F, '$1!=p{close(f); f="Report_ID_" $1 ".txt"; p=$1} {print>f}' file

If the file is not grouped, you could sort first:

sort -nt, file | awk -F, '$1!=p{close(f); f="Report_ID_" $1 ".txt"; p=$1} {print>f}' 

Or, without sorting, try appending to files and emptying them first:

awk -F, '$1!=p{close(f); f="Report_ID_" $1 ".txt"; p=$1; if(!A[$1]++) printf "">f} {print>>f}' file

I guess you're talking of shell while loops. Try

while IFS="," read ID SUB DET REST; do FN="Report_ID_$ID.txt"; [ -f "$FN" ] || > "$FN"; echo $DET >> $FN; done < file
cf Report_ID_*
Report_ID_1.txt:
ABC
DEF
GHI
Report_ID_2.txt:
JKL
MNO
Report_ID_3.txt:
PQR

Thank you cjcox & Scrutinizer for the response . The solution suggested by you works but I would need it inside the while loop.

@RudiC:
This is is what I was looking for. Now I have got the logic inside the while loop.
Thank you so much.

Hi RudiC,

could you please explain below

  1. What is the purpose of REST in the code, because no value assigned to it.
  2. In code [ -f "$FN" ] || > "$FN" , what is the role of highlighted part in red. ?
    Thanks,

REST is a variable used as a "catch all", should there be any residuals in the line read. It is meant to stay empty if the line holds exactly what is expected.
The || is an "OR" operator executed when the test fails (c.f. man bash ). The > is a redirector op., used to create an empty file.

1 Like