0

Within a Batch submission script I have a file generated called "nodefile.123" that looks like:

bear-pg0208u33b

bear-pg0208u33b

bear-pg0208u33b

bear-pg0210u19a

bear-pg0210u19a

bear-pg0210u07a

It always has 1 entry per line, this is the location of each core that my program is to run on so there are 3 cores on -33b and and 2 cores on -19a and 1 core on -07a. I need to count the number of cores per node and format the data like this:

bear-pg0208u33b:3:bear-pg0210u19a:2:bear-pg0210u07a:1

So far I have the following code:

sort nodefile.3314190 | uniq -c > sorted.txt

rev="$(awk '{for(i=NF;i>=1;i--) printf "%s ", $i;print ""}' sorted.txt)"

Which has counted and reversed the inputs. I believe I just now need to turn them into one line and change the space to a colon?

0

You can use awk :

awk 'BEGIN{c=1;OFS=":";ORS=":"} {if(u!=$0 && u!=""){print u,c;u=$0;c=1} else if(u!=$0){u=$0}else{c=c+1}} END{ORS="";print u,c;}' file

And your file should not have empty lines so you can remove them like:

sed -i '/^$/d' file
  • Thank you so much, I had just figured it out in am much more convoluted way, but had an unwanted ":" at the end of my script: sort nodefile.3314190 | uniq -c > sorted.txt awk '{for(i=NF;i>=1;i--) printf "%s ", $i;print ""}' sorted.txt > rev.txt awk 'BEGIN { ORS = " " } { print }' rev.txt > conv.txt sed -e 's/\s\+/:/g' conv.txt > machines.txt – CFD-Tester Mar 13 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.