Dynamic command line generation with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dynamic command line generation with awk
# 1  
Old 03-18-2010
Dynamic command line generation with awk

Hi,
I'm not an expert in awk but i need a simple script to do this:

I'd like to AutoCrop PDF files.
I found 2 simple script that combined together could help me to automatize Smilie

The first utiliti is "pdfinfo" that it gives the MediaBox and TrimBox values from the pdf.
The pdfinfo output is:
Code:
[bags@TheBagsMan Scrivania]$ pdfinfo -box '/home/bags/Scrivania/0280.u3_capitolo15.pdf' Creator: Adobe InDesign CS3 (5.0.4) Producer: Adobe PDF Library 8.0 CreationDate: Sun Nov 8 18:52:01 2009 ModDate: Tue Mar 16 14:48:00 2010 Tagged: no Pages: 18 Encrypted: no Page size: 680.268×898.535 pts MediaBox: 0.00 0.00 680.27 898.53 CropBox: 0.00 0.00 680.27 898.53 BleedBox: 28.32 28.32 651.94 870.21 TrimBox: 42.50 42.50 637.77 856.04 ArtBox: 42.50 42.50 637.77 856.04 File size: 29864683 bytes Optimized: no PDF version: 1.6

Once obtained the values I've to give them as imput for another utility "pdfcrop"
Code:
[bags@TheBagsMan Scrivania]$ pdfcrop --margins '-42.50 -42.50 -42.50 -42.49' '/home/bags/Scrivania/0280.u3_capitolo15.pdf' '/home/bags/Scrivania/0280.u3_capitolo15.pdf'

The problem is that the "--margins" option values should be calculated as
Code:
MediaBox - TrimBox single values (the first -42.5 --> 0.00-42.50; second -42.50 --> 0.00-42.50; third -42.50 --> 680.27-637.77; last -42.49 --> 898.53-856.04)

Id like to have an AWK that recreate the pdfcrop command...
Someone can help me?

thx,
Giovanni

Last edited by radoulov; 03-18-2010 at 08:52 AM.. Reason: zaxxon: use code tags please, ty; radoulov - title fixed
# 2  
Old 03-18-2010
Code:
margins=`pdfinfo -box ’/home/bags/Scrivania/0280.u3_capitolo15.pdf’  |awk '/MediaBox/ {a=$2;b=$3;c=$4;d=$5} /TrimBox/ {print a-$2,b-$3,c-$4,d-$5}'`

pdfcrop --margins "$margins" '/home/bags/Scrivania/0280.u3_capitolo15.pdf' '/home/bags/Scrivania/0280.u3_capitolo15.pdf'


Last edited by rdcwayx; 03-18-2010 at 08:35 AM..
# 3  
Old 03-18-2010
Code:
second -42.50 --> 0.00-42.50; third -42.50 --> 680.27-637.77; last -42.49 --> 898.53-856.04

last two values should be positive.

Code:
file=/home/bags/Scrivania/0280.u3_capitolo15.pdf
pdfcrop --margins "$(pdfinfo -box "$file" |awk '/^MediaBox:/ {m1=$2;m2=$3;m3=$4;m4=$5} /^TrimBox:/{print m1-$2,m2-$3,m3-$4,m4-$5}')" "$file"

# 4  
Old 03-18-2010
Another one. Check the output first without the coloured part:

Code:
#!/bin/sh

file="/home/bags/Scrivania/0280.u3_capitolo15.pdf"

pdfinfo -box "'""$file""'" |	
awk -v f="$file" '
/MediaBox:/{a=$2; b=$3; c=$4; d=$5}
/TrimBox:/{printf("pdfcrop --margins  %.2f %.2f %.2f %.2f \047%s\047 \047%s\047\n", a-$2, b-$3, c-$4, d-$5, f, f)}
' | sh

# 5  
Old 03-18-2010
And another one (untested!):


Code:
awk 'BEGIN {
pdfi = "pdfinfo"
pdfc = "pdfcrop"
for (i = 0; ++i <= ARGC - 1;) {
  pdficmd = pdfi " -box \47" ARGV[i] "\47"
  mb = x
  while ((pdficmd | getline) > 0) {
    /^MediaBox/ && mb = $0
    mb && split(mb, t)
    /^TrimBox/ && pdfccmd = sprintf( "%s --margins \47%.2f %.2f %.2f %.2f\47 \47%s\47 \47%s\47", pdfc,  \
    t[2] - $2, t[3] - $3, t[4] - $4, t[5] - $5, ARGV[i], ARGV[i])
  }
  close(pdficmd); system(pdfccmd); close(pdfccmd)
  }
}' file1.pdf [file2.pdf .. ]


Last edited by radoulov; 03-18-2010 at 09:01 AM..
# 6  
Old 03-18-2010
Quote:
Originally Posted by anchal_khare
Code:
second -42.50 --> 0.00-42.50; third -42.50 --> 680.27-637.77; last -42.49 --> 898.53-856.04

last two values should be positive.

Code:
file=/home/bags/Scrivania/0280.u3_capitolo15.pdf
pdfcrop --margins "$(pdfinfo -box "$file" |awk '/^MediaBox:/ {m1=$2;m2=$3;m3=$4;m4=$5} /^TrimBox:/{print m1-$2,m2-$3,m3-$4,m4-$5}')" "$file"

This works fine Smilie

Thx a lot
Giovanni

---------- Post updated at 01:45 PM ---------- Previous update was at 01:29 PM ----------

Quote:
Originally Posted by gbagagli
This works fine Smilie

Thx a lot
Giovanni
mhhhh
if i use:
Code:
file="/home/bags/Scrivania/0280.u3_capitolo15.pdf"
pdfinfo -box "$file" | awk '/^MediaBox:/ {m2=$2;m3=$3;m4=$4;m5=$5} /^TrimBox:/{print m2-$2 ,m3-$3 ,$4-m4 ,$5-m5}

I obtain right values.
but If I use the whole part:
Code:
file="/home/bags/Scrivania/0280.u3_capitolo15.pdf"
pdfcrop --margins $(pdfinfo -box "$file" | awk '/^MediaBox:/ {m2=$2;m3=$3;m4=$4;m5=$5} /^TrimBox:/{print m2-$2 ,m3-$3 ,$4-m4 ,$5-m5}')" "$file"

I obtain an error Smilie
Code:
PDFCROP 1.23, 2010/01/09 - Copyright (c) 2002-2010 by Heiko Oberdiek.
!!! Error: Input file `' not found!

seems wrong in command expression Smilie

I should have:
Code:
pdfcrop --margins '-42.50 -42.50 -42.50 -42.49' '/home/bags/Scrivania/0280.u3_capitolo15.pdf' '/home/bags/Scrivania/0280.u3_capitolo15.pdf'

Giovanni
# 7  
Old 03-18-2010
You didn't start the quote.
by the way, you can also use any of the solutions.

Code:
pdfcrop --margins "$(pdfinfo -box "$file" | awk '/^MediaBox:/ {m2=$2;m3=$3;m4=$4;m5=$5} /^TrimBox:/{print m2-$2 ,m3-$3 ,$4-m4 ,$5-m5}')" "$file"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Tar command generation with Find

Hello, I'm trying to generate a TAR command and fill it with the result of a Find command. What I did is not elegant and I'm sure that it can be in a different way to be faster. I need to Find all files with an extension to ".sf". If a file is found, I need to replace the extension ".sf" to... (11 Replies)
Discussion started by: royinfo.alain
11 Replies

2. Shell Programming and Scripting

Using awk and grep for sql generation

Hi, I have a file pk.txt which has table related data in following format TableName | PK Employee | id Contact|name,phone,country I have another file desc.txt which lists datatype of each field like this: Table|Field|Type Employee|id|int Contact|name|string Contact|country|string... (7 Replies)
Discussion started by: wahi80
7 Replies

3. Shell Programming and Scripting

awk concatenation issue - SQL generation

Greetings Experts, I have an excel file and I am unable to read it directly into awk (contains , " etc); So, I cleansed and copied the data into notepad. I need to generate a script that generates the SQL. Requirement: 1. Filter and select only the data that has the "mapping" as "direct"... (4 Replies)
Discussion started by: chill3chee
4 Replies

4. Shell Programming and Scripting

Dynamic file generation using shell

I have to generate the file dynamically from the source file based on the below control file. control_file.txt 1,3,5,-1,8,-1,4 The control file contain the position of column which i required from the source file, Example 1 column ,3 column ,5 column ,blank column(-1 indicates blank... (2 Replies)
Discussion started by: rspwilliam
2 Replies

5. Shell Programming and Scripting

Pass awk field to a command line executed within awk

Hi, I am trying to pass awk field to a command line executed within awk (need to convert a timestamp into formatted date). All my attempts failed this far. Here's an example. It works fine with timestamp hard-codded into the command echo "1381653229 something" |awk 'BEGIN{cmd="date -d... (4 Replies)
Discussion started by: tuxer
4 Replies

6. Shell Programming and Scripting

Modify line with dynamic variable in awk

Hi, I'm guessing this is probably relatively straight forward to do in awk, but I just can't get my head round it! I have a log file of the following format: 3:03:35 (lmgrd) TIMESTAMP 10/14/2011 3:20:41 (MLM) IN: "MATLAB" user1@host1.private.dns.zone 3:21:05 (MLM) IN: "MATLAB"... (2 Replies)
Discussion started by: chrissycc
2 Replies

7. Shell Programming and Scripting

Random word generation with AWK

Hi - I have a word GTTCAGAGTTCTACAGTCCGACGAT I need to extract all the possible "chunks" of 7 or above letter "words" from this. SO, my out put should be GTTCAGA TTCAGAG TCAGAGT CAGAGTTCT TCCGACGAT CAGTCCGACG etc. How can I do that with awk or any other language? I have no... (2 Replies)
Discussion started by: polsum
2 Replies

8. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

9. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

10. Shell Programming and Scripting

awk- report generation from input file

I have input file with below content: Person: Name: Firstname1 lastname1 Address: 111, Straat City : Hilversum Person: Name : Fistname2 lastname2 Address: 222, street Cit: Bussum Person: Name : Firstname2 lastname3 Address: 333, station straat City: Amsterdam I need... (6 Replies)
Discussion started by: McLan
6 Replies
Login or Register to Ask a Question