breaking up long one liners

breaking up long one liners

am 18.12.2007 19:34:18 von RickM

Some of my one liners are getting very long and hard to read, is there
a way to break or continue the line?

Thanks

Re: breaking up long one liners

am 18.12.2007 19:38:18 von Ed Morton

On 12/18/2007 12:34 PM, rickm@galaxy.nsc.com wrote:
> Some of my one liners are getting very long and hard to read, is there
> a way to break or continue the line?
>
> Thanks

Yes. If you post an example we could probably help.

Ed.

Re: breaking up long one liners

am 18.12.2007 19:42:55 von RickM

On Dec 18, 10:38 am, Ed Morton wrote:
> On 12/18/2007 12:34 PM, ri...@galaxy.nsc.com wrote:
>
> > Some of my one liners are getting very long and hard to read, is there
> > a way to break or continue the line?
>
> > Thanks
>
> Yes. If you post an example we could probably help.
>
> Ed.

Heres one:
awk '/DEFINE/ { print $1" "$3 }' cds.lib | awk -F / '/DEFINE/ { print
$0 " " $(NF -3 ) "/" $(NF -2 ) "/" $(NF -1 ) "/"$NF }' | sed '/^$/d'
| sed 's/DEFINE/cp -r /g' | sed 's/$/ . /' | sed '/^\-/d' >!
$ARCHIVE_DIR/copyLibs.csh

Im sure there are much better way to do this.

Re: breaking up long one liners

am 18.12.2007 21:06:40 von Ed Morton

On 12/18/2007 12:42 PM, rickm@galaxy.nsc.com wrote:
> On Dec 18, 10:38 am, Ed Morton wrote:
>
>>On 12/18/2007 12:34 PM, ri...@galaxy.nsc.com wrote:
>>
>>
>>>Some of my one liners are getting very long and hard to read, is there
>>>a way to break or continue the line?
>>
>>>Thanks
>>
>>Yes. If you post an example we could probably help.
>>
>> Ed.
>
>
> Heres one:
> awk '/DEFINE/ { print $1" "$3 }' cds.lib | awk -F / '/DEFINE/ { print
> $0 " " $(NF -3 ) "/" $(NF -2 ) "/" $(NF -1 ) "/"$NF }' | sed '/^$/d'
> | sed 's/DEFINE/cp -r /g' | sed 's/$/ . /' | sed '/^\-/d' >!
> $ARCHIVE_DIR/copyLibs.csh
>
> Im sure there are much better way to do this.
>

Well, if you just want to put it on separate lines, just put a line break after
each pipe symbol:

awk '/DEFINE/ { print $1" "$3 }' cds.lib |
awk -F / '/DEFINE/ { print $0 " " $(NF -3 ) "/" $(NF -2 ) "/" $(NF -1 ) "/"$NF }' |
sed '/^$/d' |
sed 's/DEFINE/cp -r /g' |
sed 's/$/ . /' |
sed '/^\-/d' > $ARCHIVE_DIR/copyLibs.csh

but a couple of immediate observations:

1) You don't need to search for "DEFINE" with the second awk scrip since your
first awk script only prints records that contain DEFINE.

2) You don't need to delete blank lines with your first sed script since your
awk scripts won't print any.

So you can immediately reduce the above to:

awk '/DEFINE/ { print $1" "$3 }' cds.lib |
awk -F / '{ print $0 " " $(NF -3 ) "/" $(NF -2 ) "/" $(NF -1 ) "/"$NF }' |
sed 's/DEFINE/cp -r /g' |
sed 's/$/ . /' |
sed '/^\-/d' > $ARCHIVE_DIR/copyLibs.csh

Now, instead of 2 piped awk scripts, you can just merge them to one:

awk '/DEFINE/ { o = $1 " " $3
n = split(o,f,"/")
print o " " f[n-3] "/" f[n-2] "/" f[n-1] "/" f[n]
}' cds.lib | ...

and you can then merge in all your sed scripts:

awk '
/DEFINE/ { o = $1 " " $3
n = split(o,f,"/")
o = o " " f[n-3] "/" f[n-2] "/" f[n-1] "/" f[n]
gsub(/DEFINE/,"cp -r ",o)
sub(/$/," . ",o)
if ( !/^\-/) {
print o
}
}' cds.lib > $ARCHIVE_DIR/copyLibs.csh

There are probably more efficient ways to code that, it depends what your real
input file looks like so all I've done is translate your awk+sed pipes as-is.
For example, doing all that work and THEN filtering out lines that start with a
minus sign seems kinda silly. It'd make more sense to drop those lines up-front,
e.g.:

awk '
/DEFINE/ && !/^-/ { o = $1 " " $3
n = split(o,f,"/")
o = o " " f[n-3] "/" f[n-2] "/" f[n-1] "/" f[n]
gsub(/DEFINE/,"cp -r ",o)
sub(/$/," . ",o)
}' cds.lib > $ARCHIVE_DIR/copyLibs.csh

Regards,

Ed.