I'm writing a program in GW-BASIC. For some reasons, I have the following error :
"Numéro de fichier illégal en 4712"
which can be translated in english by,
" illegal file number in 4712"
Here is a part of my code :
51 Chemin$ = "T:\Basic\Calculs\" + NF$
52 ON ERROR GOTO 60
53 MKDIR Chemin$
54 END
... ( a lot of code not important to solve this problem :) )
4711 CHDIR Chemin$
4712 OPEN "Intdrcrc.doc" FOR APPEND AS #3
4712 PRINT #3, "*---------------------------------------------------------------------------------------------------------------*"
4713 PRINT #3, USING "* Centre ##### \ \#######.### #######.### Intersect Droite Cercler *";IC,NC$,XC#,YC#
4714 PRINT #3, USING "* Point ##### \ \#######.### #######.### R=#######.### *";IP,NP$,XP#,YP#,R#
4715 PRINT #3, USING "* 1er Intersection M1 ##### \ \ #######.### #######.### *";I1,N1$,XM1#,YM1#
4716 PRINT #3, USING "* 2e Intersection M2 ##### \ \ #######.### #######.### *";I2,N2$,XM2#,YM2#
4717 PRINT #3, "*---------------------------------------------------------------------------------------------------------------*"
4718 CLOSE #3
4719 CHDIR "T:\Basic"
I had the same problem in previous lines, so I changed the # after "APPEND", but here, at the line 4712, changing the # doesn't solve the problem..
I hope I'm clear enough,
thank you very much for your suggestions !
:)
It seems the Intdrcrc.doc file does not exist (although I can't be sure about that without looking at the rest of your code).
What you can try is,
replace OPEN "Intdrcrc.doc" FOR APPEND AS #3 with OPEN "Intdrcrc.doc" FOR OUTPUT AS 3 and try if it gives an error. This is just to test of course. You should revert back to APPEND later. We want to see if the error is gone with OUTPUT. If so, it probably means, the file does not exist, as you expected it to.
Secondly, you need to implement some error-handling after the OPEN command.
What you can do is something like this,
4710 ...
4711 SHARED errorflag
4712 OPEN "Intdrcrc.doc" FOR APPEND AS 1
4713 IF errorflag <> 0 THEN
4714 errorflag = 0
4715 CLOSE
4716 PRINT "File not found - press return to continue."
4717 INPUT "", a$
4718 EXIT SUB
4719 END IF
4720 PRINT #3, "*------------------------------------------*"
4721 ...
So that we may know, something more if an error happens.
So i wrote this :
4702 CHDIR Chemin$
4703 OPEN "Intdrcrc.doc" FOR APPEND AS 3
4704 IF errorflag <> 0 THEN
4705 errorflag = 0
4706 CLOSE
4707 PRINT "File not found - press return to continue"
4708 INPUT "", a$
4709 EXIT SUB
4710 END IF
4712 PRINT 3, "*---------------------------------------------------------------------------------------------------------------*"
4713 PRINT 3, USING "* Centre ##### \ \#######.### #######.### Intersect Droite Cercler *";IC,NC$,XC#,YC#
4714 PRINT 3, USING "* Point ##### \ \#######.### #######.### R=#######.### *";IP,NP$,XP#,YP#,R#
4715 PRINT 3, USING "* 1er Intersection M1 ##### \ \ #######.### #######.### *";I1,N1$,XM1#,YM1#
4716 PRINT 3, USING "* 2e Intersection M2 ##### \ \ #######.### #######.### *";I2,N2$,XM2#,YM2#
4717 PRINT 3, "*---------------------------------------------------------------------------------------------------------------*"
4718 CLOSE 3
4719 CHDIR "T:\Basic"
Result : in gwbasic cmd window it is written : "File not found - press return to continue"
And then the file "intdrcrc.doc" is created. But it is empty, as if "PRINT 3" didn't work
Why not try:
4702 CHDIR Chemin$
4703 OPEN "Intdrcrc.doc" FOR OUTPUT AS #3
4712 PRINT #3, "*---------------------------------------------------------------------------------------------------------------*"
4713 PRINT #3, USING "* Centre ##### \ \#######.### #######.### Intersect Droite Cercler *"; IC, NC$, XC#, YC#
4714 PRINT #3, USING "* Point ##### \ \#######.### #######.### R=#######.### *"; IP, NP$, XP#, YP#, R#
4715 PRINT #3, USING "* 1er Intersection M1 ##### \ \ #######.### #######.### *"; I1, N1$, XM1#, YM1#
4716 PRINT #3, USING "* 2e Intersection M2 ##### \ \ #######.### #######.### *"; I2, N2$, XM2#, YM2#
4717 PRINT #3, "*---------------------------------------------------------------------------------------------------------------*"
4718 CLOSE #3
Is that 2nd line numbered as 4712 replacing the first one? If so, the program will try to print into file number #3 which was not opened.
4712 OPEN "Intdrcrc.doc" FOR APPEND AS #3
4712 PRINT #3, "*---------------------------------------------------------------------------------------------------------------*"
Related
I have a specific column in a file which contains strings like this :
1.1*1024
1.0*1024
1.1*1024
...
I want the numeric result :
1126.4
1024.0
1126.4
So I tried this, please note that the sizes are on the 6th column of my file :
$ cat file | awk '{col=$6;size=sprintf("%f", col);print "=> col = "col" size = "size}'
=> col = 1.1*1024 size = 1.100000
=> col = 1.0*1024 size = 1.000000
=> col = 1.1*1024 size = 1.100000
EDIT1 My input file looks like this :
$ cat file
[[ ]] toto1.mp4 2019-04-16 22:11 1.1*1024
[[ ]] toto2.mp4 2019-04-16 21:41 1.0*1024
[[ ]] toto3.mp4 2019-04-16 22:40 1.1*1024
[[ ]] toto4.mp4 2019-04-16 22:09 1.1*1024
...
Somehow the multiplication is ignored.
Can you help ?
This is generic way to to evaluate the string , it could process * ,+,-,/ etc. If you can use perl then there is option to eval the string, this will evaluate the string for the math operation from the input.
CASE-1: When evaluating entire line:
cat inputfile
1+1
2*2
3/3
4.5*2
5/2
perl -nle ' print eval ' inputfile
2
4
1
9
2.5
CASE-2: When evaluating one column only: In this case 6th column. supply the column number to get evaluated, F[5] is the 6th column.
perl -nale 'print eval $F[5]' inputfile
1126.4
1024
1126.4
1126.4
case-3: When evaluating 6th column, but printing entire record. Evaluate 6th column and update its value, later print the whole line.
perl -nale '$F[5] = eval $F[5] ;print "#F"' inputfile
[[ ]] toto1.mp4 2019-04-16 22:11 1126.4
[[ ]] toto2.mp4 2019-04-16 21:41 1024
[[ ]] toto3.mp4 2019-04-16 22:40 1126.4
[[ ]] toto4.mp4 2019-04-16 22:09 1126.4
Could you please try following(tested and written after seeing provided samples only).
awk '{split($NF,array,"*");printf("%.01f\n",array[1]*array[2])}' Input_file
Awk can't evaluate expressions in variables, you need to build an evaluator for it or evaluate some other way, like:
$ awk '{
cmd="awk \47BEGIN{print " $6 "}\47" # prepare an external awk command
cmd | getline retval # execute
print retval # output evaluated value
close(cmd)
}' file
Output:
1126.4
1024
1126.4
1126.4
There are awk programs floating around that can evaluate, for example calc3. Google is your friend, my friend.
Is this what you're trying to do?
$ awk -F'[[:space:]*]+' '{printf "%.1f\n", $(NF-1) * $NF}' file
1126.4
1024.0
1126.4
1126.4
sprintf can't evaluate operations itself. You need to parse and evaluate them, and then sprintf can be used for converting the result into a string with a specific format.
$ awk -v OFS=' ' 'split($NF,a,/\*/)>1{$NF=a[1]*a[2]} 1' file
[[ ]] toto1.mp4 2019-04-16 22:11 1126.4
[[ ]] toto2.mp4 2019-04-16 21:41 1024
[[ ]] toto3.mp4 2019-04-16 22:40 1126.4
[[ ]] toto4.mp4 2019-04-16 22:09 1126.4
if data in d file, tried on gnu awk:
awk -F'\\*' '{a=$1*$2; printf "%-6.1f\n",a}' d
I want to pretty-print the output of a find-like script that would take input like this:
- 2015-10-02 19:45 102 /My Directory/some file.txt
and produce something like this:
- 102 /My Directory/some file.txt
In other words: "f" (for "file"), file size (right-justified), then pathname (with an arbitrary number of spaces).
This would be easy in awk if I could write a script that takes $1, $4, and "everything from $5 through the end of the line".
I tried using the awk construct substr($0, index($0, $8)), which I thought meant "everything starting with field $8 to the end of $0".
Using index() in this way is offered as a solution on linuxquestions.org and was upvoted 29 times in a stackoverflow.com thread.
On closer inspection, however, I found that index() does not achieve this effect if the starting field happens to match an earlier point in the string. For example, given:
-rw-r--r-- 1 tbaker staff 3024 2015-10-01 14:39 calendar
-rw-r--r-- 1 tbaker staff 4062 2015-10-01 14:39 b
-rw-r--r-- 1 tbaker staff 2374 2015-10-01 14:39 now or later
Gawk (and awk) get the following results:
$ gawk '{ print index($0, $8) }' test.txt
49
15
49
In other words, the value of $8 ('b') matches at index 15 instead of 49 (i.e., like most of the other filenames).
My issue, then is how to specify "everything from field X to the end of the string".
I have re-written this question in order to make this clear.
Looks to me like you should just be using the "stat" command rather than "ls", for the reasons already commented upon:
stat -c "f%15s %n" *
But you should double-check how your "stat" operates; it apparently can be shell-specific.
The built-in awk function index() is sometimes recommended as a way
to print "from field 5 through the end of the string" [1, 2, 3].
In awk, index($0, $8) does not mean "the index of the first character of
field 8 in string $0". Rather, it means "the index of the first occurrence in
string $0 of the string value of field 8". In many cases, that first
occurrence will indeed be the first character in field 8 but this is not the
case in the example above.
It has been pointed out that parsing the output of ls is generally a bad
idea [4], in part because implementations of ls significantly differ in output.
Since the author of that note recommends find as a replacement for ls for some uses,
here is a script using find:
find $# -ls |
sed -e 's/^ *//' -e 's/ */ /g' -e 's/ /|/2' -e 's/ /|/2' -e 's/ /|/4' -e 's/ /|/4' -e 's/ /|/6' |
gawk -F'|' '{ $2 = substr($2, 1, 1) ; gsub(/^-/, "f", $2) }
{ printf("%s %15s %s\n", $2, $4, $6) }'
...which yields the required output:
f 4639 /Users/foobar/uu/a
f 3024 /Users/foobar/uu/calendar
f 2374 /Users/foobar/uu/xpect
This approach recursively walks through a file tree. However, there may of course be implementation differences between versions of find as well.
http://www.linuxquestions.org/questions/linux-newbie-8/awk-print-field-to-end-and-character-count-179078/
How to print third column to last column?
Print Field 'N' to End of Line
http://mywiki.wooledge.org/ParsingLs
Maybe some variation of find -printf | awk is what you're looking for?
$ ls -l tmp
total 2
-rw-r--r-- 1 Ed None 7 Oct 2 14:35 bar
-rw-r--r-- 1 Ed None 2 Oct 2 14:35 foo
-rw-r--r-- 1 Ed None 0 May 3 09:55 foo bar
$ find tmp -type f -printf "f %s %p\n" | awk '{sub(/^[^ ]+ +[^ ]/,sprintf("%s %10d",$1,$2))}1'
f 7 tmp/bar
f 2 tmp/foo
f 0 tmp/foo bar
or
$ find tmp -type f -printf "%s %p\n" | awk '{sub(/^[^ ]+/,sprintf("f %10d",$1))}1'
f 7 tmp/bar
f 2 tmp/foo
f 0 tmp/foo bar
It won't work with file names that contain newlines.
I would like to print everything between two two lines that match a certain pattern. For example, if my input file looks like
--- START ---
line 1
line 2
--- END ---
I would like to have as output
line 1
line 2
Can this be done (e.g. using grep or awk?)
This is how to do it with awk
awk '/END/{f=0} f; /START/{f=1}' file.txt
line 1
line 2
You should easily find solution for this using Google.
Another version:
awk '/START/{f=1;next} /END/{f=0} f' file.txt
line 1
line 2
you can do
sed -n '/--- START ---/,/--- END ---/{/--- START ---\|--- END ---/!p}' < input
or
awk '/--- END ---/{exit}; flag {print}; /--- START ---/{flag=1} ' < input
Using perl :
perl -0777 -ne 'print $1 if /^--- START ---\s*\n(.*?)--- END ---/s' file
Here is a simple solution which uses awk and grep:
awk '/-- START ---/,/--- END ---/ {print $0}' file.txt \
| grep -v -- '--- START ---' \
| grep -v -- '--- END ---'
I want to reassemble my PDF documents to print them on A5 using the A4 format from my printer.
Also I need to print two sites (slides) on each A5 page which of course should be double sided. Hence the A4 page is in landscape format. Than I want to cut it in the middle.
A short example for 9 slides:
first A4 first back second A4
[1][5] [3][7] [9][-]
[2][6] [4][8] [-][-]
After cutting it it would get me slides 1-4, 5-8, 9 on a two-sided A5 paper.
Printing this using "Booklet Printing" has not worked yet.
I thought of reassembling the PDF pages automatically with a shell script using pdftk and calculates mod 8 as changing the order manually is not an option. After I rearanged the order I can easily print the slides using the options of my printer.
How could that be done or is there an easier way to it?
Thanks
as far I understand, you are trying to Impose 4 pages on every portrait A4, that will give to you 4 A6 single pages (two A5 sheets for A4 page) since dividing the A4 area into 4 parts, means to divide every side by two, so we'll have:
29.7 cm /2 = 14.8 cm (approximated)
21 cm divided by two = 10.5 cm
is is obvious, then, that in the same A4 portrait area, can find place, 4 A6 portrait single pages, and 2 A5 landscape A5 pages both, like in picture:
so, You don't want be able to make a booklet into A6 format, but only to cut portrait A4 into half to produce 2 A5 sheets for A4 page? Are you not interested to cut further these A5 landscape pages to have an A6 booklet to bind?
Since, as far I understand, it seems you are looking for a script able to produce a booklet made imposing 4 A6 pages for each side of an A4 sheet, recalculating in right order the imposition sequence, to have the correct front/back matching, take a look to my script
note: before to start, resize to A6 your input multipage pdf (that has A4 size)
this can be done with he help of
Multivalent.jar
https://rapidgator.net/file/c6bd7f31bf8885bcaa69b50ffab7e355/Multivalent20060102.jar.html
(latest free version with tools included)
java -cp path...to/Multivalent.jar tool.pdf.Impose -dim 1x1 -paper A6 input.pdf
then take the resulting output file (remember to work on a copy) rename this into your starting input file and use this script
to perform this task, and to to assure that your pdf multipage file has the right number of pages needed to perform imposition, (8 or a integer multiple of 8), you will use this script based on pdftk (at least 1.41 version that has stamp feature and Multivalent.jar - -https://rapidgator.net/file/c6bd7f31bf8885bcaa69b50ffab7e355/Multivalent20060102.jar.html
(latest free version with tools included).
usage: namescript file.pdf multivalentpath
the multivalentpath is relative: if you downloaded Multivalent.jar into your home, then the multivalentpath will be /home/. You can also customize the script and replace directly the value for second argument (multivalentpath, so you don't need to type every time the multivalentpath; in this case, replace multivalentpath=$2 with
multivalentpath=/home/Multivalent.jar
the script also will add the crop marks to final output pdf
9 pages pdf sample
http://ge.tt/7smk7Lk/v/1
resulting file
http://ge.tt/7smk7Lk/v/2
view animation or resulting output file with crop marks added to make easy cut two times the amount of sheets (horizontally first and vertically then)
once you finished to print and put the last sheet on other sheets, take a look to your last imposed sheet
this need to be cutted (I highlighted using a dash line) in half horizzontally, in order to be able to close the two series of sheets (upper and lower) one another (the upper over the lower) obtaining a single series of sheets from the two original series of sheets,
at this stage, you can furtherly cut following the dashed line the sheet to be able to collate single A6 sheets and biding with glue, clip or other, or using two points of stapler an bend the booklet you have produced
note: the blank pages you seen, are placed automatically after last written page of your original pdf, they are needed to, as you rightly stated in your question, to perform the right imposition sequence, they don't interfere with sequential logic order, in resulting binded booklet the order, in the case of our example, will be:
1,2,3,4,5,6,7,8,9,10,11,1,2,13,14,15,16
the pages in bold, will be left blank and only needed to perform the right imposition, impossible to perform if the number of pages is not a multiple of 8
#!/bin/bash
#
#
############################
#
# use: namescript file.pdf multivalentpath
#
############################
#
#
#
#
file=$1
multivalentpath=$2
pages="`pdftk $file dump_data | grep NumberOfPages | cut -d : -f2`"
echo $pages
halfpages="`echo -n $(( $pages / 2 ))`"
echo $halfpages
incr="$(echo "scale=0; $halfpages+1" |bc -l)"
dividedby4="$(echo "scale=0; $pages/8" |bc -l)"
lastupperpage="$(echo "scale=0; $pages-2" |bc -l)"
u="u"
#first case
h="$(pdfinfo $file | grep "Page size" | cut -d x -f1 | tr 'Page size:' ' ' | xargs)"
w="$(pdfinfo $file | grep "Page size" | cut -d x -f2 | tr 'pts' ' ' | xargs)"
echo $h
echo $w
doubleheight="$(echo "scale=0; $h * 2" |bc -l)"
doublewidth="$(echo "scale=0; $w * 2" |bc -l)"
echo $doubleheight
echo $doublewidth
if [ $(( $pages % 8 )) -eq 0 ]
then
echo " the file has already a number of pages multiple by eight"
sequence="$(for ((x=$halfpages, y=$incr, z=$pages, w=1;x>=4, y<=4, z>=2, w<=$lastupperpage;x--, y++, z--, w++)); do echo "$x$u $y$u;$z $w"; done | tr ";" "\n" | tr " " "," | awk -F "," '{ print $2 "," $1; getline; print; getline; print; getline; print $2 "," $1 }' | tr "\n" "," | cut -d "," -f 1-`seq -s, 1 $pages`)"
echo "sequence is $sequence"
java -cp "$multivalentpath"Multivalent.jar tool.pdf.Impose -verbose -dim 2x2 -paper "$doubleheight"x"$doublewidth"pt -layout "$sequence" $file
cat << EOF |uudecode
begin-base64 644 /tmp/grid.pdf
JVBERi0xLjMKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVy
IC9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nFWPuw7CUAiGd56C2USEw+EUnsDZ
OhonbzHRwTr4+p5Wq20YgP//wuWBTFyQ+xiLwx1WbYOXJ+xQEhbcV/EIBV8g
eIWBxXYNSyfR5IIlXEkV7+CaSKz8lBtsIdydfAIFZyeTGbT7LslhjKIRhXJF
v20dSTJwg+CZKYWOPv+dJrNS9tEpVpTEP76lILfJHaYNk+TZHYJ9dBfIjRtF
/cQlSLH+SWzxy90JzgvYwBusBz89ZW5kc3RyZWFtCmVuZG9iago2IDAgb2Jq
CjE3OAplbmRvYmoKNCAwIG9iago8PC9UeXBlL1BhZ2UvTWVkaWFCb3ggWzAg
MCA1OTUgODQyXQovUGFyZW50IDMgMCBSCi9SZXNvdXJjZXM8PC9Qcm9jU2V0
Wy9QREZdCi9FeHRHU3RhdGUgOCAwIFIKPj4KL0NvbnRlbnRzIDUgMCBSCj4+
CmVuZG9iagozIDAgb2JqCjw8IC9UeXBlIC9QYWdlcyAvS2lkcyBbCjQgMCBS
Cl0gL0NvdW50IDEKPj4KZW5kb2JqCjEgMCBvYmoKPDwvVHlwZSAvQ2F0YWxv
ZyAvUGFnZXMgMyAwIFIKPj4KZW5kb2JqCjcgMCBvYmoKPDwvVHlwZS9FeHRH
U3RhdGUKL09QTSAxPj5lbmRvYmoKOCAwIG9iago8PC9SNwo3IDAgUj4+CmVu
ZG9iagoyIDAgb2JqCjw8L1Byb2R1Y2VyKEVTUCBHaG9zdHNjcmlwdCA4MTUu
MDQpCi9DcmVhdGlvbkRhdGUoRDoyMDEyMTIwMTIzNDMzNCkKL01vZERhdGUo
RDoyMDEyMTIwMTIzNDMzNCk+PmVuZG9iagp4cmVmCjAgOQowMDAwMDAwMDAw
IDY1NTM1IGYgCjAwMDAwMDA0NzIgMDAwMDAgbiAKMDAwMDAwMDU5MCAwMDAw
MCBuIAowMDAwMDAwNDEzIDAwMDAwIG4gCjAwMDAwMDAyODIgMDAwMDAgbiAK
MDAwMDAwMDAxNSAwMDAwMCBuIAowMDAwMDAwMjYzIDAwMDAwIG4gCjAwMDAw
MDA1MjAgMDAwMDAgbiAKMDAwMDAwMDU2MSAwMDAwMCBuIAp0cmFpbGVyCjw8
IC9TaXplIDkgL1Jvb3QgMSAwIFIgL0luZm8gMiAwIFIKL0lEIFso1xqpwgd/
HzDmRPwLQT3dEyko1xqpwgd/HzDmRPwLQT3dEyldCj4+CnN0YXJ0eHJlZgo3
MDEKJSVFT0YK
====
EOF
pdftk ${file%%.pdf}-up.pdf stamp /tmp/grid.pdf output gridded.pdf && mv gridded.pdf ${file%%.pdf}-up.pdf && echo "finished" && exit 0
else
echo "number of pages is not a multiple of 8, adding needed blank pages to complete the imposition sequence"
heightxwidth="`pdfinfo -box $file| grep MediaBox | cut -d : -f2 | awk '{print $3 FS $4}'`"
echo "%PDF-1.4
1 0 obj
<<
/CreationDate (D:20121202145344)
/Producer (text2pdf v1.1 (\251 Phil Smith, 1996))
/Title (blank.txt)
>>
endobj
2 0 obj
<<
/Type /Catalog
/Pages 3 0 R
>>
endobj
4 0 obj
<<
/Type /Font
/Subtype /Type1
/Name /F1
/BaseFont /Courier
>>
endobj
5 0 obj
<<
/Font << /F1 4 0 R >>
/ProcSet [ /PDF /Text ]
>>
endobj
6 0 obj
<<
/Type /Page
/Parent 3 0 R
/Resources 5 0 R
/Contents 7 0 R
>>
endobj
7 0 obj
<<
/Length 8 0 R
>>
stream
BT
/F1 10 Tf
1 0 0 1 50 798 Tm
12 TL
()'
ET
endstream
endobj
8 0 obj
44
endobj
3 0 obj
<<
/Type /Pages
/Count 1
/MediaBox [ 0 0 595 841 ]
/Kids [ 6 0 R ]
>>
endobj
xref
0 9
0000000000 65535 f
0000000009 00000 n
0000000132 00000 n
0000000524 00000 n
0000000181 00000 n
0000000259 00000 n
0000000330 00000 n
0000000410 00000 n
0000000506 00000 n
trailer
<<
/Size 9
/Root 2 0 R
/Info 1 0 R
>>
startxref
609
%%EOF" | sed -e "s/595 841/$heightxwidth/g">/tmp/blank.pdf
fi
if [ $(( $pages / 8 )) -eq 0 ]
then val0="`echo "scale=0; 8-$pages" | bc -l`"
else val1="`echo "scale=0; $pages/8" | bc -l`"; echo $val1
fi
if [ $(( $pages / 8 )) -eq 0 ] ; then echo "case 2: adding $val0 blank pages" ; sleep 1s && numpages=`for ((a=1; a <= $val0; a++)); do echo -n " B1 "; done` && pdftk A=$file B=/tmp/blank.pdf cat A $numpages output pagesadded.pdf && mv pagesadded.pdf $file
#new variable values for second case
unset pages
unset halfpages
unset incr
unset dividedby4
unset lastupperpage
pages="`pdftk $file dump_data | grep NumberOfPages | cut -d : -f2`"
halfpages="`echo -n $(( $pages / 2 ))`"
incr="$(echo "scale=0; $halfpages+1" |bc -l)"
dividedby4="$(echo "scale=0; $pages/8" |bc -l)"
lastupperpage="$(echo "scale=0; $pages-2" |bc -l)"
echo $pages
else
val2="`let ADDITION=$val1+1; echo $ADDITION`"
val3="`let MULTIPLICATION=$val2*8; echo $MULTIPLICATION`"
val4="`echo "scale=0; $val3-$pages" |bc -l`"
echo " case 3: adding $val4 blank pages" ; sleep 1s
numpages="`for ((a=1; a <= $val4; a++)); do echo -n " B1 "; done`"
echo $numpages
pdftk A=$file B=/tmp/blank.pdf cat A $numpages output pagesadded.pdf && mv pagesadded.pdf $file
fi
#new variable values for third case
unset pages
unset halfpages
unset incr
unset dividedby4
unset lastupperpage
pages="`pdftk $file dump_data | grep NumberOfPages | cut -d : -f2`"
halfpages="`echo -n $(( $pages / 2 ))`"
incr="$(echo "scale=0; $halfpages+1" |bc -l)"
dividedby4="$(echo "scale=0; $pages/8" |bc -l)"
lastupperpage="$(echo "scale=0; $pages-2" |bc -l)"
sequence="$(for ((x=$halfpages, y=$incr, z=$pages, w=1;x>=4, y<=4, z>=2, w<=$lastupperpage;x--, y++, z--, w++)); do echo "$x$u $y$u;$z $w"; done | tr ";" "\n" | tr " " "," | awk -F "," '{ print $2 "," $1; getline; print; getline; print; getline; print $2 "," $1 }' | tr "\n" "," | cut -d "," -f 1-`seq -s, 1 $pages` | xargs)"
echo $sequence
java -cp "$multivalentpath"Multivalent.jar tool.pdf.Impose -verbose -dim 2x2 -paper "$doubleheight"x"$doublewidth"pt -layout "$sequence" $file
cat << EOF |uudecode
begin-base64 644 /tmp/grid.pdf
JVBERi0xLjMKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVy
IC9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nFWPuw7CUAiGd56C2USEw+EUnsDZ
OhonbzHRwTr4+p5Wq20YgP//wuWBTFyQ+xiLwx1WbYOXJ+xQEhbcV/EIBV8g
eIWBxXYNSyfR5IIlXEkV7+CaSKz8lBtsIdydfAIFZyeTGbT7LslhjKIRhXJF
v20dSTJwg+CZKYWOPv+dJrNS9tEpVpTEP76lILfJHaYNk+TZHYJ9dBfIjRtF
/cQlSLH+SWzxy90JzgvYwBusBz89ZW5kc3RyZWFtCmVuZG9iago2IDAgb2Jq
CjE3OAplbmRvYmoKNCAwIG9iago8PC9UeXBlL1BhZ2UvTWVkaWFCb3ggWzAg
MCA1OTUgODQyXQovUGFyZW50IDMgMCBSCi9SZXNvdXJjZXM8PC9Qcm9jU2V0
Wy9QREZdCi9FeHRHU3RhdGUgOCAwIFIKPj4KL0NvbnRlbnRzIDUgMCBSCj4+
CmVuZG9iagozIDAgb2JqCjw8IC9UeXBlIC9QYWdlcyAvS2lkcyBbCjQgMCBS
Cl0gL0NvdW50IDEKPj4KZW5kb2JqCjEgMCBvYmoKPDwvVHlwZSAvQ2F0YWxv
ZyAvUGFnZXMgMyAwIFIKPj4KZW5kb2JqCjcgMCBvYmoKPDwvVHlwZS9FeHRH
U3RhdGUKL09QTSAxPj5lbmRvYmoKOCAwIG9iago8PC9SNwo3IDAgUj4+CmVu
ZG9iagoyIDAgb2JqCjw8L1Byb2R1Y2VyKEVTUCBHaG9zdHNjcmlwdCA4MTUu
MDQpCi9DcmVhdGlvbkRhdGUoRDoyMDEyMTIwMTIzNDMzNCkKL01vZERhdGUo
RDoyMDEyMTIwMTIzNDMzNCk+PmVuZG9iagp4cmVmCjAgOQowMDAwMDAwMDAw
IDY1NTM1IGYgCjAwMDAwMDA0NzIgMDAwMDAgbiAKMDAwMDAwMDU5MCAwMDAw
MCBuIAowMDAwMDAwNDEzIDAwMDAwIG4gCjAwMDAwMDAyODIgMDAwMDAgbiAK
MDAwMDAwMDAxNSAwMDAwMCBuIAowMDAwMDAwMjYzIDAwMDAwIG4gCjAwMDAw
MDA1MjAgMDAwMDAgbiAKMDAwMDAwMDU2MSAwMDAwMCBuIAp0cmFpbGVyCjw8
IC9TaXplIDkgL1Jvb3QgMSAwIFIgL0luZm8gMiAwIFIKL0lEIFso1xqpwgd/
HzDmRPwLQT3dEyko1xqpwgd/HzDmRPwLQT3dEyldCj4+CnN0YXJ0eHJlZgo3
MDEKJSVFT0YK
====
EOF
pdftk ${file%%.pdf}-up.pdf stamp /tmp/grid.pdf output gridded.pdf && mv gridded.pdf ${file%%.pdf}-up.pdf
echo "finished 2"
exit 0
Even though its been a little while since you asked this question, you might try jPdf Tweak to arrange your (current/future) pdf slides.
Supposing your base slides are printed as landscape A4, a possible config string for the n-up layout you initially asked for, would be
8:!+1N0.5+0.0%+100.0%,+5N0.5+100.0%+100.0%,+2N0.5+0.0%+0.0%,+6N0.5+100.0%+0.0%,!+3N0.5+0.0%+100.0%,+7N0.5+100.0%+100.0%,+4N0.5+0.0%+0.0%,+8N0.5+100.0%+0.0%
the corresponding layout looks like (ignore the preset title here):
If you start with postscript files a similar string could be used with pstops.
I'm definitely interested in getting a book on awk. I've been won over, despite playing with it for little time. However, in the mean time, I have a problem I suspect I could solve exclusively with [g]awk. To demonstrate, I'll use some fdisk output. In this example, the desired end result would be something like:
Disks: 2 [240 GB total]
sda=120 GB
sdb=120 GB
Here's what I have:
fdisk -l 2>/dev/null | awk '/^Disk \/dev\/[vsh]d./ {bytes+=$5} END {print "Disks: "NR" ["; gb=bytes/1024/1024/1024; print gb" GB total]"}'
My NR apparently prints out 73.. on my laptop with two hard drives, anyway. This, I do not understand. So far, well.. I'm maybe half-way there. Any tips or succinct tutorials would be greatly appreciated!
After two major excellent answers, I ended up with the following:
echo DISKS: $(fdisk -l 2>/dev/null | awk -F/ '/^Disk \/dev\/[vsh]d.|^Disk \/dev\/xvd./{print$3}' | awk '{d++;bytes+=$4;disk[$1]=$2" "$3}END{gb=bytes/1024/1024/1024; printf gb" GB total, "d" disks\n";for (i in disk) print " "i,disk[i]}' | sed -e "s/,$/ /" -e "s/: / /")
Which gave me output like this:
DISKS: 78.3585 GB total, 2 disks
sda 80.0 GB
sdb 4110 MB
Looking forward to the day I can do that all in one awk command with no sed. :) But for now, thanks folks!
$ fdisk -l 2>/dev/null | awk '/^Disk \/dev\/[vsh]d./{d++;bytes+=$5;disk[$2]=$3$4} END {printf "Disks: "d" ["; gb=bytes/1024/1024/1024; printf gb" GB total]\n";for (i in disk) print i,disk[i]}'
Disks: 1 [93.1604 GB total]
/dev/sda: 100.0GB,
You're more than half way. The only issue is that NR is the number of rows (so far), not the number of rows matching your expression. So just use another variable for that.
fdisk -l 2>/dev/null | awk '/^Disk \/dev\/[vsh]d./ {bytes+=$5; disks+=1} END {print "Disks: "disks" ["; gb=bytes/1024/1024/1024; print gb" GB total]"}'
That gives:
Disks: x [
yyy GB total]
To remove the newline, and add output for each drive, try:
fdisk -l 2>/dev/null | awk '/^Disk \/dev\/[vsh]d./ {cur_bytes=$5; bytes+=cur_bytes; disks+=1; gsub(/dev|:|\//, "", $2); print $2 "=" cur_bytes/1024/1024/1024 " GB";} END {printf "Disks: "disks" ["; gb=bytes/1024/1024/1024; print gb" GB total]"}'
That gives:
sda=xx GB
sdb=yy GB
sdc=zz GB
Disks: 3 [ww GB total]
It doesn't print the header first. You could use concatenation to do that.