adding page skip character to multiple files....

  • Follow


Hi,

I have to add a page skip(^L) character at the beginning of multiple
files(file1,file2.....filen). Presently i am opening in each file in
vim like


vi file*

and adding ^L--->save--->pickup next file.....

I want an awk solution.


0
Reply nag 10/21/2009 3:17:54 PM

nag wrote:
> Hi,
> 
> I have to add a page skip(^L) character at the beginning of multiple
> files(file1,file2.....filen). Presently i am opening in each file in
> vim like
> 
> 
> vi file*
> 
> and adding ^L--->save--->pickup next file.....
> 
> I want an awk solution.
> 
> 

The "pure awk" solution for one file is:

awk '
function printout(_str) { _out[++_nr] = _str }
function flushout(  _i) { close(FILENAME);
                           for (_i=1; _i<=_nr;_i++)
                                  print _out[_i] > FILENAME
                         }
FNR==1 { printout( "" ) }
{ printout( $0 ) }
END { flushout() }'
file

To do multiple files, change the "flushout()" from being called in only 
an END section to being called each time FNR==1 and in the END but it's 
simpler, more efficient, and more robust to use a tmp file:

awk 'FNR==1{print "^L"}1' file > tmp &&
mv tmp file

and wrap it in a shell loop if you want to do multiple files:

for file in "$@"
do
    awk 'FNR==1{print "^L"}1' "$file" > tmp &&
    mv tmp "$file"
done

but finally the best solution is not to use awk at all:

for file in "$@"
do
    ( printf "\n"; cat "$file" ) > tmp &&
    mv tmp "$file"
done

    Ed.
0
Reply Ed 10/21/2009 3:52:14 PM


Ed Morton wrote:
> nag wrote:
>> Hi,
>>
>> I have to add a page skip(^L) character at the beginning of multiple
>> files(file1,file2.....filen). Presently i am opening in each file in
>> vim like
>>
>>
>> vi file*
>>
>> and adding ^L--->save--->pickup next file.....
>>
>> I want an awk solution.
>>
>>
> 
> The "pure awk" solution for one file is:
> 
> awk '
> function printout(_str) { _out[++_nr] = _str }
> function flushout(  _i) { close(FILENAME);
>                           for (_i=1; _i<=_nr;_i++)
>                                  print _out[_i] > FILENAME
>                         }
> FNR==1 { printout( "" ) }
> { printout( $0 ) }
> END { flushout() }'
> file
> 
> To do multiple files, change the "flushout()" from being called in only 
> an END section to being called each time FNR==1 and in the END but it's 
> simpler, more efficient, and more robust to use a tmp file:
> 
> awk 'FNR==1{print "^L"}1' file > tmp &&
> mv tmp file
> 
> and wrap it in a shell loop if you want to do multiple files:
> 
> for file in "$@"
> do
>    awk 'FNR==1{print "^L"}1' "$file" > tmp &&
>    mv tmp "$file"
> done
> 
> but finally the best solution is not to use awk at all:
> 
> for file in "$@"
> do
>    ( printf "\n"; cat "$file" ) > tmp &&
>    mv tmp "$file"
> done
> 
>    Ed.

Oops, sorry for veering OT, forgot which group I was in....

	Ed.
0
Reply Ed 10/21/2009 3:52:52 PM

Ed Morton wrote:
> Ed Morton wrote:
>> nag wrote:
>>> Hi,
>>>
>>> I have to add a page skip(^L) character at the beginning of multiple
>>> files(file1,file2.....filen). [...]

>> but finally the best solution is not to use awk at all:
>>
>> for file in "$@"
>> do
>>    ( printf "\n"; cat "$file" ) > tmp &&
                ^^^^
>>    mv tmp "$file"
>> done
>>
>>    Ed.
> 
> Oops, sorry for veering OT, forgot which group I was in....

In the shell group they ask for inappropriate sed solutions where
awk would fit much better, and here they're asking for awk where a
shell solution (as yours) is more appropriate.

Just note that a typo slipped in; should be ^L instead of \n.

Janis

> 
>     Ed.
0
Reply Janis 10/21/2009 4:36:01 PM

nag wrote:
> Hi,
> 
> I have to add a page skip(^L) character at the beginning of multiple
> files(file1,file2.....filen). Presently i am opening in each file in
> vim like
> 
> 
> vi file*
> 
> and adding ^L--->save--->pickup next file.....
> 
> I want an awk solution.

Oh, what the heck, since it's not as simple as I made it sound in my 
previous post, here's the (untested!) multiple file solution using 
slightly more meaningful variable and function names:

awk '
function saveRec(rec)    {
    _File[++_Fnr] = rec
}
function printFile(   fnr) {
    if (_PrevFilename != "") {
       close(_PrevFilename)
       for (fnr=1; fnr<=_Fnr; fnr++)
           print _File[fnr] > _PrevFilename
       close(_PrevFilename)
    }
    _Fnr = 0
    _PrevFilename = FILENAME
}
FNR==1 { printFile(); saveRec( "^L" ) }
{ saveRec( $0 ) }
END { printFile() }'
file1 file2 ...

Maybe it'll be useful to someone...

     Ed
0
Reply Ed 10/21/2009 4:40:32 PM

Janis Papanagnou wrote:

> Just note that a typo slipped in; should be ^L instead of \n.

I suppose in all cases that should be "\v" (vertical tab) in awk. ^L is just 
another way to refer to ascii 11 (ie control-L). I *think* that's the OP 
wants since he mentioned "page skip", but I might as well be mistaken.


0
Reply pk 10/21/2009 4:40:49 PM

Janis Papanagnou wrote:
> Ed Morton wrote:
>> Ed Morton wrote:
>>> nag wrote:
>>>> Hi,
>>>>
>>>> I have to add a page skip(^L) character at the beginning of multiple
>>>> files(file1,file2.....filen). [...]
> 
>>> but finally the best solution is not to use awk at all:
>>>
>>> for file in "$@"
>>> do
>>>    ( printf "\n"; cat "$file" ) > tmp &&
>                ^^^^
>>>    mv tmp "$file"
>>> done
>>>
>>>    Ed.
>>
>> Oops, sorry for veering OT, forgot which group I was in....
> 
> In the shell group they ask for inappropriate sed solutions where
> awk would fit much better, and here they're asking for awk where a
> shell solution (as yours) is more appropriate.

Yeah, but maybe the OP isn't on UNIX and I could at least have added 
"<OT>" indicators...

> Just note that a typo slipped in; should be ^L instead of \n.

Thanks for catching that. I notice I used "" instead of "^L" in the 
"pure awk" version too.

	Ed.

> Janis
> 
>>
>>     Ed.
0
Reply Ed 10/21/2009 4:43:31 PM

pk wrote:
> Janis Papanagnou wrote:
> 
>> Just note that a typo slipped in; should be ^L instead of \n.
> 
> I suppose in all cases that should be "\v" (vertical tab) in awk. ^L is just 
> another way to refer to ascii 11 (ie control-L).

Errm, no, don't think so.

\v - a vertical tab - sais nothing about the actual tab-spacing, and
ASCII 11 is the equivalent code for \v.

I was refering to ^L, which is ASCII 12, abbreviated in ASCII as "FF".

> I *think* that's the OP 
> wants since he mentioned "page skip", but I might as well be mistaken.

AFAIKT, ASCII FF (Form Feed, ^L) is typically interpreted as a page skip.

Janis
0
Reply Janis 10/21/2009 4:53:07 PM

Janis Papanagnou wrote:

> pk wrote:
>> Janis Papanagnou wrote:
>> 
>>> Just note that a typo slipped in; should be ^L instead of \n.
>> 
>> I suppose in all cases that should be "\v" (vertical tab) in awk. ^L is
>> just another way to refer to ascii 11 (ie control-L).
> 
> Errm, no, don't think so.
> 
> \v - a vertical tab - sais nothing about the actual tab-spacing, and
> ASCII 11 is the equivalent code for \v.
> 
> I was refering to ^L, which is ASCII 12, abbreviated in ASCII as "FF".
> 
>> I *think* that's the OP
>> wants since he mentioned "page skip", but I might as well be mistaken.
> 
> AFAIKT, ASCII FF (Form Feed, ^L) is typically interpreted as a page skip.

Right, thanks for spotting that. I was looking at the wrong char. It should 
be "\f" then (I think).

0
Reply pk 10/21/2009 5:08:04 PM

On Wed, 21 Oct 2009 11:40:32 -0500, Ed Morton <mortonspam@gmail.com> wrote:

>nag wrote:
>> Hi,
>> 
>> I have to add a page skip(^L) character at the beginning of multiple
>> files(file1,file2.....filen). Presently i am opening in each file in
>> vim like
>> 
>> 
>> vi file*
>> 
>> and adding ^L--->save--->pickup next file.....
>> 
>> I want an awk solution.
>
>Oh, what the heck, since it's not as simple as I made it sound in my 
>previous post, here's the (untested!) multiple file solution using 
>slightly more meaningful variable and function names:
>
>awk '
>function saveRec(rec)    {
>    _File[++_Fnr] = rec
>}
>function printFile(   fnr) {
>    if (_PrevFilename != "") {
>       close(_PrevFilename)
>       for (fnr=1; fnr<=_Fnr; fnr++)
>           print _File[fnr] > _PrevFilename
>       close(_PrevFilename)
>    }
>    _Fnr = 0
>    _PrevFilename = FILENAME
>}
>FNR==1 { printFile(); saveRec( "^L" ) }
>{ saveRec( $0 ) }
>END { printFile() }'
>file1 file2 ...
>
>Maybe it'll be useful to someone...

What's wrong with:

$ cat a b c
line 1a
line 2a
line 3a

line 1b
line 2b
line 3b

line 1c
line 2c
line 3c

$ awk 'FNR == 1 {printf"%c","\f"};{print}' a b c > d
$ xxd d
0000000: 0c6c 696e 6520 3161 0a6c 696e 6520 3261  .line 1a.line 2a
0000010: 0a6c 696e 6520 3361 0a0a 0c6c 696e 6520  .line 3a...line
0000020: 3162 0a6c 696e 6520 3262 0a6c 696e 6520  1b.line 2b.line
0000030: 3362 0a0a 0c6c 696e 6520 3163 0a6c 696e  3b...line 1c.lin
0000040: 6520 3263 0a6c 696e 6520 3363 0a0a       e 2c.line 3c..

If OP doesn't want a formfeed before first file, change one-liner to:
  awk 'FNR != NR && FNR == 1 {printf"%c","\f"};{print}' a b c > d

This one adds a trailing FF too:
  awk 'FNR == 1 {printf"%c","\f"};{print};END{printf"%c","\f"}' a b c > d

Grant.
-- 
http://bugsplatter.id.au
0
Reply Grant 10/23/2009 3:21:20 AM

Grant wrote:
> On Wed, 21 Oct 2009 11:40:32 -0500, Ed Morton <mortonspam@gmail.com> wrote:
> 
>> nag wrote:
>>> Hi,
>>>
>>> I have to add a page skip(^L) character at the beginning of multiple
>>> files(file1,file2.....filen). Presently i am opening in each file in
>>> vim like
>>>
>>>
>>> vi file*
>>>
>>> and adding ^L--->save--->pickup next file.....
>>>
>>> I want an awk solution.
>> Oh, what the heck, since it's not as simple as I made it sound in my 
>> previous post, here's the (untested!) multiple file solution using 
>> slightly more meaningful variable and function names:
>>
>> awk '
>> function saveRec(rec)    {
>>    _File[++_Fnr] = rec
>> }
>> function printFile(   fnr) {
>>    if (_PrevFilename != "") {
>>       close(_PrevFilename)
>>       for (fnr=1; fnr<=_Fnr; fnr++)
>>           print _File[fnr] > _PrevFilename
>>       close(_PrevFilename)
>>    }
>>    _Fnr = 0
>>    _PrevFilename = FILENAME
>> }
>> FNR==1 { printFile(); saveRec( "^L" ) }
>> { saveRec( $0 ) }
>> END { printFile() }'
>> file1 file2 ...
>>
>> Maybe it'll be useful to someone...
> 
> What's wrong with:
> 
> $ cat a b c
> line 1a
> line 2a
> line 3a
> 
> line 1b
> line 2b
> line 3b
> 
> line 1c
> line 2c
> line 3c
> 
> $ awk 'FNR == 1 {printf"%c","\f"};{print}' a b c > d
> $ xxd d
> 0000000: 0c6c 696e 6520 3161 0a6c 696e 6520 3261  .line 1a.line 2a
> 0000010: 0a6c 696e 6520 3361 0a0a 0c6c 696e 6520  .line 3a...line
> 0000020: 3162 0a6c 696e 6520 3262 0a6c 696e 6520  1b.line 2b.line
> 0000030: 3362 0a0a 0c6c 696e 6520 3163 0a6c 696e  3b...line 1c.lin
> 0000040: 6520 3263 0a6c 696e 6520 3363 0a0a       e 2c.line 3c..
> 
> If OP doesn't want a formfeed before first file, change one-liner to:
>   awk 'FNR != NR && FNR == 1 {printf"%c","\f"};{print}' a b c > d
> 
> This one adds a trailing FF too:
>   awk 'FNR == 1 {printf"%c","\f"};{print};END{printf"%c","\f"}' a b c > d
> 
> Grant.

AFAIK the OP wasn't asking how to concatenate files into a single file 
using a formfeed between each, he was asking how to edit files in place 
adding a formfeed before the first line of each.

	Ed.
0
Reply Ed 10/23/2009 4:46:54 AM

On Thu, 22 Oct 2009 23:46:54 -0500, Ed Morton <mortonspam@gmail.com> wrote:

>Grant wrote:
....
>>   awk 'FNR == 1 {printf"%c","\f"};{print};END{printf"%c","\f"}' a b c > d
>> 
>> Grant.
>
>AFAIK the OP wasn't asking how to concatenate files into a single file 
>using a formfeed between each, he was asking how to edit files in place 
>adding a formfeed before the first line of each.

Hadda be a catch :)  Too easy.

Grant.
-- 
http://bugsplatter.id.au
0
Reply Grant 10/23/2009 6:26:19 AM

On Oct 21, 10:08=A0pm, pk <p...@pk.invalid> wrote:
> Janis Papanagnou wrote:
> > pk wrote:
> >> Janis Papanagnou wrote:
>
> >>> Just note that a typo slipped in; should be ^L instead of \n.
>
> >> I suppose in all cases that should be "\v" (vertical tab) in awk. ^L i=
s
> >> just another way to refer to ascii 11 (ie control-L).
>
> > Errm, no, don't think so.
>
> > \v - a vertical tab - sais nothing about the actual tab-spacing, and
> > ASCII 11 is the equivalent code for \v.
>
> > I was refering to ^L, which is ASCII 12, abbreviated in ASCII as "FF".
>
> >> I *think* that's the OP
> >> wants since he mentioned "page skip", but I might as well be mistaken.
>
> > AFAIKT, ASCII FF (Form Feed, ^L) is typically interpreted as a page ski=
p.
>
> Right, thanks for spotting that. I was looking at the wrong char. It shou=
ld
> be "\f" then (I think).

I am working on RHEL 4 ES. yes it is \f. generally i incorporate it in
my awk reports.
0
Reply nag 10/23/2009 2:22:20 PM

12 Replies
103 Views

(page loaded in 0.568 seconds)

5/21/2013 10:52:10 PM


Reply: