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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to compare to a previous value of a variable in a while loop for a file
# 1  
Old 11-11-2015
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 belowSmilieReportID,Sub_reportID,Sub_reportName)

Code:
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

Code:
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

Last edited by Scrutinizer; 11-11-2015 at 11:53 PM..
# 2  
Old 11-11-2015
Just having fun (and teaching the filtering way). Assumes the triples are in a file called reports.txt:

Code:
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.
# 3  
Old 11-12-2015
If there are only a few ReportID's and the records are grouped together on the field ReportID, then this may be enough:
Code:
awk -F, '{print >("Report_ID_" $1 ".txt")}' file

Otherwise try something like this:
Code:
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:
Code:
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:
Code:
awk -F, '$1!=p{close(f); f="Report_ID_" $1 ".txt"; p=$1; if(!A[$1]++) printf "">f} {print>>f}' file

# 4  
Old 11-12-2015
I guess you're talking of shell while loops. Try
Code:
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

# 5  
Old 11-12-2015
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.
# 6  
Old 11-14-2015
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,
# 7  
Old 11-14-2015
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.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare previous and current item in for loop in bash?

Hey, I am trying to compare formated login and logout dates from one user at a host which I have stored in a tmp directory in order to find out the total login time. I need to compare them in order to find overlapping intervals. At first I tried to store each log in and logo date in an array... (3 Replies)
Discussion started by: Mumu123
3 Replies

2. Shell Programming and Scripting

Unable to set Global variable outside while loop

Below is my code: count=0 if ... ... else ... find * -prune -type d | sort -r -n | while read d; do count=1 if ; then echo "Count1:$count" ... ... break 2; fi ... done ... fi echo "Count2:$count" (9 Replies)
Discussion started by: mohtashims
9 Replies

3. UNIX for Dummies Questions & Answers

awk unable to compare the shell variable

Hi Could anyone please help with Awk. The below code prints the PID of the matching process with condition with $8 and $9 ps -ef |awk '($8~/proc/) && ($9~/rPROC2/) {print $2}' Now i want to change the Constant PROC2 from Shell variable PROC2 is already declared in shell variable SRVNAME... (9 Replies)
Discussion started by: rakeshkumar
9 Replies

4. Shell Programming and Scripting

AWK Compare previous value with current.

Hi, I have one small doubt how to go ahead and process the below requirement. File Content 1,abc,10 2,xyz,11 3,pqr,12 4,pqr,13 5,pqr,14 Output file expected: 1,mnq,1 1,ddd,2 1,qqq,3 1,sss,4 1,ddd,5 1,eee,6 1,fff,7 1,ddr,8 1,rrd,9 (3 Replies)
Discussion started by: dikesm
3 Replies

5. UNIX for Dummies Questions & Answers

Unable to write to a file within a loop

Hi All, Following is the program that i have written in cygwin. The redirection of the unfound $param1 to error.txt file doesnt work.Can any one help? #!/usr/bin/sh fname=$1 sed 's/ //g' "$fname" > fname1 while read i do echo $i > file1 #param1 is script name ... (1 Reply)
Discussion started by: janardhanamk
1 Replies

6. Shell Programming and Scripting

KSH: Compare variable to $1 in an input file

Hello, I am working with KSH on AIX and I have 2 files generated from different sources... as seen below: FILE1 FILE2 AAA AAA@ABS0001C BBB BBB@ABS0003D CCC CCC@ABS0023A DDD DDD@ABC0145D EEE EEE@ABS0090A FFF FFF@ABS0002A GGG GGG@ABC0150D HHH FILE1 is main main data source,... (4 Replies)
Discussion started by: right_coaster
4 Replies

7. Shell Programming and Scripting

Unable to access variable outside loop

I am unable to access the value set inside the loop from outside loop . Thought of taking this to forum , I had seen other replies also , where a pipe takes the execution to another shell and mentioned thats the reason we do not get the variable outside loop . But I am getting an issue and I am... (1 Reply)
Discussion started by: Armaan_S
1 Replies

8. Shell Programming and Scripting

to write a script to compare the file size in the current directory and previous dir

hi, i am new to this site. i want to write a script to compare the file size of the files in the current dir with the files in the previous directory. the files name will be same, but the filename format will be as xyzddddyymm.txt. the files will arrive with the month end date(i want to... (5 Replies)
Discussion started by: tweety
5 Replies

9. Shell Programming and Scripting

unable to access a variable not local to a while loop

I have a while loop like this cat ${filename} | while read fileline do ... done I need to access a variable value $newfile inside this while loop How will i do that?? (6 Replies)
Discussion started by: codeman007
6 Replies

10. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies
Login or Register to Ask a Question