comp.unix.shell FAQ - Answers to Frequently Asked Questions

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Tapani Tar » Sat, 23 Aug 2003 21:58:17



XXXX@XXXXX.COM (Joe Halpin) writes:

[...]


Written like that the "print" command is executed by the shell,
so it'll only work with shells that have it (like ksh, but not
Bourne or even bash). It'd be better to add the final ^Z
with awk:

awk '{print} END{printf "\032"}' ORS='\r\n' unixfile > dosfile

(Note printf rather than print to avoid printing extra \r\n.)

--
Tapani Tarvainen
 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Tapani Tar » Sat, 23 Aug 2003 22:26:05


XXXX@XXXXX.COM (Joe Halpin) writes:


That almost certainly doesn't do what is desired: the
variable will be set for END statement only, after the
file has been processed. Usually what is needed is

awk '{print $0,var}' var=xxx file

which sets the variable first (but after processing
BEGIN statement, unlike the -v option which sets
it even before that). This allows setting the
variables differently for several files, as in

awk '...' var=xxx file1 var=yyy file2

Another common way of passing shell variables to awk
is unquoting the awk script around it, e.g.

awk '{print $0,'"$var"'}'

though I'm not sure that's really a FAQ.

--
Tapani Tarvainen

 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Alan Conno » Sun, 24 Aug 2003 04:45:00


Joe,

For reasons unknown to me, the FAQ did not make it onto news.west.\
earthlink.net.


You can set procmail up, easily, to create a fileserver via email. The
recipe is towards the bottom of man procmailex.


Alan C
 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Alan Conno » Sun, 24 Aug 2003 04:45:00


Yeh. There's no "print" on Debian linux , at least. Joe and I ran into that
yesterday.


Alan C
 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by joe » Sun, 24 Aug 2003 05:46:41

Alan Connor < XXXX@XXXXX.COM > writes:



[...]


I doubt that the version of linux being used is the issue. At least,
I'm not aware of an implementation where print is an external
command.

Joe
 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Alan Conno » Sun, 24 Aug 2003 05:54:33


So it is the shell? Not available in sh/bash etc? Seems like you should
put the printf version in your FAQ, also.


Alan Connor
 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by joe » Sun, 24 Aug 2003 06:00:15

Alan Connor < XXXX@XXXXX.COM > writes:




Yes.


I did point that out in the FAQ.

Joe
 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by joe » Sun, 24 Aug 2003 06:15:26


XXXX@XXXXX.COM writes:



Not to mention that Tapani was talking about awk, not the shell or an
external print command.

I'm taking the rest of the day off.

Joe
 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Sean T. Ki » Mon, 22 Sep 2003 06:40:24


[...]

Nice solutions there!

More often than I would've thought, I've found myself wanting to remove
lines from the beginning _and_ the end of a file; came up with this
a while back, have used it ever since:

(I call it 'pare')
------------
#!/bin/bash
myname=$(basename $0)
if [ $# -lt 2 ]; then
cat << EOF 1>&2

$myname n m file

where 'n' is the number of lines to omit from the beginning,
and 'm' is the number of lines to omit from the end, of 'file'

or use as a filter:

command_or_pipeline | $myname n m

EOF
exit 1
fi

if [ -z $3 ]; then
tbp='/tmp/'tbp.$$
cat >$tbp
trap "rm -f $tbp" 0 1 2 15
else
tbp=$3
fi

# ln=$(wc -l $tbp | tr -s ' ' | cut -d ' ' -f2)
# to=$(expr $ln - $1)
# ho=$(expr $(expr $ln - $2) - $1)
# tail -n $to $tbp | head -n $ho

ln=$(wc -l $tbp | awk '{print $1}')
to=$(expr $1 + 1)
ho=$(expr $(expr $ln - $2) - $1)
tail -n '+'$to $tbp | head -n $ho

exit 0
------------

It's a little wasteful, I guess, but I just use a '0' when I only want
to 'slice' one end -- 'pare 0 9 file'; 'pare 7 0 file'.

Sean
 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Chris F.A. » Mon, 22 Sep 2003 09:21:55


Since you are using bash, why not save an external command:

myname=${0##*/}


No need for awk:

set -- $( wc -l $tbp )
ln=$1


It's only very old versions of expr that cannot handle multiple
operations in a single call. You could use:

ho=$(expr $ln - $2 - $1)

but even better, as it uses no external commands, would be:

to=$(( $1 + 1 ))
ho=$(( $ln - $2 - $1 ))


Here's a version I just whipped up; it uses the same calling
syntax as yours:

[ $# -lt 2 ] && { echo "USAGE: ${0##*/}: top tail [file]"; exit 1; }
tail=$2
IFS='
'
set -- `tail +$(( $1 + 1 )) ${3:+"$3"} | tee TEMPFILE`
lines=$#
head -$(( $lines - $tail )) TEMPFILE
rm TEMPFILE

If I were to use it in a serious script, I'd verify that the first
2 arguments are numbers, and use mktemp to get a temporary
filename. I'd also reset $IFS afterwards if it was to be used
in-line or as a function.

--
Chris F.A. Johnson http://www.yqcomputer.com/
===================================================================
My code (if any) in this post is copyright 2003, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Sean T. Ki » Mon, 22 Sep 2003 12:38:01


[...]

Good point -- ran across such usage (in Advanced bash scripting, I
think) about a week ago and have used it once so far to save myself
calling basename _and_ dirname AIR.... should plug it into all the
old scripts.

[...]

And no need for 'tr' or 'cut' _either_, as in my abandoned

ln=$(wc -l $tbp | tr -s ' ' | cut -d ' ' -f2)

I'm gonna have to read up on the set builtin, I can see....


Great, I hate using expr. More to investigate.


That's okay -- it works just fine.... ;-)


Hey, even your usage message is better than mine!


....and make sure they aren't 'bad' numbers wrt the number of lines....

[...]

Another thing I just got hip to not long ago -- I've plugged mktemp
into about a third of my scripts so far.

_Excellent_ article! Thanks very much!

Sean
 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Joe Halpi » Tue, 02 Dec 2003 06:12:15

The FAQ has been posted here, and is also available at
http://www.yqcomputer.com/ ~j.p.h/

Joe
 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by John W. Kr » Tue, 02 Dec 2003 08:13:43


Alright, who included this POS in the FAQ? :-)

Assuming 12 lines like the other examples:

perl -ne' print shift @x if @x == 12; push @x, $_ ' filename




John
--
use Perl;
program
fulfillment
 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Michael He » Tue, 02 Dec 2003 08:37:45


[..]




sed -n -e :a -e '1,12!{P;N;D;};N;ba' filename

Looks shorter

--
Michael Heiming

Remove +SIGNS and www. if you expect an answer, sorry for
inconvenience, but I get tons of SPAM
 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Stephane C » Tue, 02 Dec 2003 17:49:42

2003-12-1, 00:37(+01), Michael Heiming:
[...]

Should be:

sed -n -e :a -e '1,12!{P;N;D;}' -e 'N;ba' filename
or:
sed -n ':a
1,12!{P;N;D;}
N;ba' filename

See sed FAQ (section 6.7.1) and
http://www.yqcomputer.com/ #tag_04_126_13_03

--
Sthane ["Stephane.Chazelas" at "free.fr"]