Archive for the ‘Platform’ Category.

concatenate (join together) several *.pdf files

pdf is a document format. there are occasions where I find it easier to work with one large document rather than several single documents (most recently, while working with several spec sheets for electronic components), the following command will join two or more *.pdfs into a single document using ghostscript.

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=OUTPUT.pdf INPUT01.pdf INPUT02.pdf ...

-dBATCH - exit after last file
-dNOPAUSE - no pause after page
-q - “quiet”, fewer messages
-sDEVICE=<devname> - select device, in this case the pdf writer
-sOutputFile=<file> - select output file

the following were not, but could be, included:
-g<width>x<height> - page size in pixels
-r<res> - pixels/inch resolution

converting *.pdf to an image series

pdf is a document format. on the occasion where it is easier to reference an image rather than the full document, the following command will convert the pdf.

convert -density 96x96 -trim INPUTFILE.pdf OUTPUTFILE.jpg

-density is the image density in pixels per square inch
-trim will trim the image edges

the output will be in the form of OUTPUTFILE-#.jpg, where # is an increasing “page” number.
this command will also work for OUTPUTFILE.png, OUTPUTFILE.gif, et. al..

also, imagemagick uses ghostscript to convert, so the quality of the final images will only be as good as ghostscript can provide.

Converting *.flac to *.mp3

flac is an audio format (Free Lossless Audio Codec) that is excellent for archiving, but few if any portable players will be able to play it, so a method to convert to mp3 is shown below.

convert .flac to .mp3

flac -cd "FILENAME.flac" | lame -h - "FILENAME.mp3"

flac: -c is output to console, -d is decode
lame: -h is shorthand for “-q 2″ and -q is quality (in this case a high quality), “-” is input from console

same but for all .flac files in a directory

for FILE in *.flac; do $(flac -cd "$FILE" | lame -h - "${FILE%.flac}.mp3"); done

same options in a bash for loop, the ${FILE%.flac} strips “.flac” from the name

Notes:
id3 options for lame:
–tt <title> –ta <artist> –tl <album> –ty <year> –tc <comment> –tn <track> –tg <genre>
will automatically add an id3v1 or id3v2 as needed (choice is based on the size of the fields),
“–add-id3v2″ “–id3v2-only” “–id3v1-only” can be used to modify choice.

more complicated operations are beyond the scope of this article, but I’d suggest using K3B.

Extracting RPM Package Contents

firstly, there is a package available called “alien” that will convert to/from packages for nearly any distro, I use this on machines that it can be installed on.

however, there are times that using standard tools is faster and simpler,

piping the output of “rpm2cpio” (extract cpio archive from RPM Package Manager (RPM) package.) into “cpio” (copy files to and from archives) will extract the RPM package into the current directory.

rpm2cpio package.rpm | cpio -dimv

where:
-d = make directories, -i = “in” mode, -m = keep date/time stamps, -v = verbose.

Search and Replace Text

Find and replace all instances of a string in all files in current (and any subs) directory.

find ./ -name “*” | xargs perl -pi -e ’s/SEARCH/REPLACE/g’

Using Pure Bash to Parse Strings

while looking through my snippets for a script to parse a simple “several fields to a line, one record to a line” type of data set, I realized I was probably over engineering the solution, the following is the result of a few whiles playing with a few ideas:
 
 

#!/bin/bash
ITEMS='field 1;field 2;field 3;field 4;field 5'
LEN=${#ITEMS}
while [ "$LEN" -gt "0" ]; do
	echo -------------
	echo ${LEN}
	echo "post:"${ITEMS}
	#get the shortest match from front of the source string
	# by matching the longest from the back of the source string
	THIS_FIELD=${ITEMS%%;*}
	#remove the match from the source string
	ITEMS=${ITEMS:$((${#THIS_FIELD}+1))}
	#calc the length of the source string
	LEN=${#ITEMS}
	#show results
	echo "anno:"${ITEMS}
	echo ${THIS_FIELD}
done

will output:

————-
39
post:field 1;field 2;field 3;field 4;field 5
anno:field 2;field 3;field 4;field 5
field 1
————-
31
post:field 2;field 3;field 4;field 5
anno:field 3;field 4;field 5
field 2
————-
23
post:field 3;field 4;field 5
anno:field 4;field 5
field 3
————-
15
post:field 4;field 5
anno:field 5
field 4
————-
7
post:field 5
anno:
field 5

 
 
the idea can be taken further by using an array:

#!/bin/bash
#declare the array
declare -a FIELDS
ITEMS='field 1;field 2;field 3;field 4;field 5'
LEN=${#ITEMS}
while [ "$LEN" -gt "0" ]; do
	echo -------------
	echo ${LEN}
	echo "post:"${ITEMS}
	#get the shortest match from front of the source string
	# by matching the longest from the back of the source string
	THIS_FIELD=${ITEMS%%;*}
	#remove the match from the source string
	ITEMS=${ITEMS:$((${#THIS_FIELD}+1))}
	#calc the length of the source string
	LEN=${#ITEMS}
	#store result in the array
	FIELDS[ (${#FIELDS[*]}) ]=${THIS_FIELD}
	#show results
	echo "anno:"${ITEMS}
	echo ${THIS_FIELD}
	echo ${FIELDS[ (${#FIELDS[*]}-1) ]}
done

echo -------------
#show the array count
echo ${#FIELDS[*]}
#show the array content
echo ${FIELDS[*]}

will output:

————-
39
post:field 1;field 2;field 3;field 4;field 5
anno:field 2;field 3;field 4;field 5
field 1
field 1
————-
31
post:field 2;field 3;field 4;field 5
anno:field 3;field 4;field 5
field 2
field 2
————-
23
post:field 3;field 4;field 5
anno:field 4;field 5
field 3
field 3
————-
15
post:field 4;field 5
anno:field 5
field 4
field 4
————-
7
post:field 5
anno:
field 5
field 5
————-
5
field 1 field 2 field 3 field 4 field 5

 
 
the final script fragment without the comments::

#!/bin/bash
ITEMS='field 1;field 2;field 3;field 4;field 5'
declare -a FIELDS
LEN=${#ITEMS}
while [ "$LEN" -gt "0" ]; do
	THIS_FIELD=${ITEMS%%;*}
	ITEMS=${ITEMS:$((${#THIS_FIELD}+1))}
	LEN=${#ITEMS}
	FIELDS[ (${#FIELDS[*]}) ]=${THIS_FIELD}
done

COUNT=0
while [ "$COUNT" -lt ${#FIELDS[*]} ]; do
	echo ${FIELDS[$COUNT]}
	COUNT=$(( ${COUNT}+1 ))
done

obviously you would want to change the “ITEMS” variable and do something meaningful in the second loop.

this was posted in the hope that it will be of some use to someone.

GhostScript and Margins

I had a postscript document that simply did not want to come out correctly, the offsets were such that the page content was going well off the visible area.

the following vile hack is one solution:

cat <<EOF>offsetfix.ps
%!PS-Adobe-2.0
<<
	/PageOffset [ 16 -52]
	/Margins [0 0]
	/.HWMargins [0 0 0 0]
>>
setpagedevice
EOF

gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite \
	-dOptimize=true \
	-sOutputFile=output.pdf \
	offsetfix.ps input.ps

granted, one could cat the snippit in offsetfix.ps with the original document, but as I’m not sure what else may be using it, I’d rather not.

this also has the advantage of being a bit more portable, albeit rather ugly when compared to the original:

ps2pdf input.ps output.pdf

File Modifications

Change File Extention

echo “`echo TESTFILENAME.EXTENTION | perl -pe ’s/\.[^.]+$//’`.NEWEXTENTION”

User Modifications

Add a user for server/system procs

useradd -g GROUP -c “USERNAME” -s /sbin/nologin USERNAME

Directory Tree Modifications

Change the owner of just the dirs in the current tree

find . -type d -exec chown USER:GROUP {} \;

Change the owner of just the files in the current tree

find . -type f -exec chown USER:GROUP {} \;

Change the permissions of just the dirs in the current tree

find . -type d -exec chmod 0775 {} \;

Change the permissions of just the files in the current tree

find . -type f -exec chmod 0775 {} \;

Change the permissions of files and dirs in the current tree

find . -type d -exec chmod 0775 {} \; ; \
find . -type f -exec chmod 0664 {} \;

  • Tags

  • Categories

  • Need Code Written?

  • Need a Coding Job?

  • Archives