Data selection

Hi,

Please note that as a programmer I cannot tell the file format or details of my production files so I have re-framed my file and taken a case of departments. Kindly guide as its related to a production requirement of data containing recors of production customer NOT A HOMEWORK :slight_smile:

I have a file containing details of different departments . Infomration of departments is in various tags
file is as below

DEPTNUM 1|
FACULTY 20|
STUDENTS 300|
COURSES 3|
DEPTNUM 2|
FACULTY 25|
STUDENTS 500|
COURSES 30|
DEPTNUM 3|
FACULTY 22|
STUDENTS 600|
COURSES 31|
AND SO ON 

I want to create a new file from the above file which should contain only two fields belonging to one department
format

DEPTNUM,STUDENTS
1,50
2,500
3,600 

There are multiple files containing department details of individiual college
I tried below

for file in `ls -1 pwd`
do
  var1=`grep DEPTNUM $file |cut -c 9`
  var2=`grep STUDENTS $file |cut -c 10-12`
done

However it did not serve my purpose and not giving expected result

Please suggest some alternative solution

Many thanks in advance

Try:

awk -F"[ |]" 'BEGIN{print "DEPTNUM,STUDENTS"}/DEPTNUM/{s=$2}/STUDENTS/{print s "," $2}' file

assuming your files are well formatted, something like:

#  nawk -F "[| ]" 'BEGIN{print "DEPTNUM,STUDENTS"}/DEPTNUM/{D=$2}/STUDENTS/{print D","$2}' infile
DEPTNUM,STUDENTS
1,300
2,500
3,600

should probably be close....

Hi Franklin and TYtalus
Many thanks, the solution is working

However I added third field and its not working Below is the code I am using

It works for me with your input file.

Regards

It works, you only need to adjust the heading: DEPTNUM,FACULTY,STUDENTS instead of DEPTNUM,STUDENTS,FACULTY

Hi,

I am facing another issue in the approach . I have new file structure like dummy exmaple below

 
DEPT 1|
DEPTNAME Science|
FACULTY 20|
STUDENTS 300|
COURSES 3|
DEPT 2|
DEPTNAME Humanities|
FACULTY 25|
STUDENTS 500|
COURSES 30|
DEPT 3|
DEPTNAME Engineering|
FACULTY 22|
STUDENTS 600|
COURSES 31|
AND SO ON

I want to get o/p like below
DEPT,STUDENTS,FACULTY
But as DEPTNAME is coming after DEPT in the input file the awk approach is matching DEPTNAME instead of DEPT .
Please guide

awk -F "[| ]" 'BEGIN{print "DEPT,STUDENTS,FACULTY"}/DEPT/{D=$2}/FACULTY/{E=$2}/STUDENTS/{print D","E","$2}' infile

just add a space after DEPT :slight_smile:

awk -F "[| ]" 'BEGIN{print "DEPT,STUDENTS,FACULTY"}/DEPT /{D=$2}/FACULTY/{E=$2}/STUDENTS/{print D","E","$2}' infile

Thanks

atrangely it works for this dummy file but not for my exact file.. I am cross checking the structure

---------- Post updated at 10:57 AM ---------- Previous update was at 10:26 AM ----------

Its working now as i reused the command with space
Now I want to print only if value of FACULTY > 22

I am using this but getting error. plz guide

awk -F "[| ]" 'BEGIN{print "DEPT,STUDENTS,FACULTY"}/DEPT /{D=$2}/FACULTY/{E=$2}/STUDENTS/{if E > 22 }{print D","E","$2}' infile

small mistake in awk..

awk -F "[| ]" 'BEGIN{print "DEPT,STUDENTS,FACULTY"}/DEPT /{D=$2}/FACULTY/{E=$2}/STUDENTS/{if (E > 22) }{print D","E","$2}}' 

Thanks vidyadhar

Its working fine