From: Ed Morton on
On 6/20/2010 6:38 AM, Dominic Fandrey wrote:
> On 20/06/2010 12:48, Ed Morton wrote:
>> On 6/20/2010 12:20 AM, Lao Ming wrote:
>>> I have a collection of files of all ascii data in which certain lines
>>> contain an @ char. In the lines that don't have the @ char, I want to
>>> substitute a space. However, since the @ is not at the beginning or
>>> end of the line, I am having difficulty determining how to accomplish
>>> this.
>>>
>>> The data below is not the real data but it is a simple accurate
>>> example of what I want to do.
>>>
>>> 1988 G. H. W. Bush more data
>>> 1992 @Bill Clinton more data
>>> 1996 @Bill Clinton more data
>>> 2000 G. W. Bush more data
>>> 2004 G. W. Bush more data
>>> 2008 @Barack Obama more data
>>>
>>> What I want the output to look like is where the names actually line
>>> up in columns so that a space occurs in front of those names without
>>> the @.
>>>
>>> Is there anyway that this can be done? Thanks.
>>
>> Sounds like this might be what you want:
>>
>> awk '!/@/{sub(/ /," ")}1' file
>
> Note that "more data" may not contain the @ character for this to work.
>

If you want to make sure the @ is only at the start of the 2nd field, change it to:

awk '$2 !~ /^@/{sub(/ /," ")}1' file

Ed.


From: Ben Bacarisse on
Lao Ming <laomingliu(a)gmail.com> writes:
<snip>
> The data below is not the real data but it is a simple accurate
> example of what I want to do.
>
> 1988 G. H. W. Bush more data
> 1992 @Bill Clinton more data
> 1996 @Bill Clinton more data
> 2000 G. W. Bush more data
> 2004 G. W. Bush more data
> 2008 @Barack Obama more data
>
> What I want the output to look like is where the names actually line
> up in columns so that a space occurs in front of those names without
> the @.

Another sed option. This one works for you data but you don't say
enough about have variable your data really is to be sure.

sed -e '/@/!s/ / /'

--
Ben.
From: Lao Ming on
On Jun 20, 12:56 am, Janis Papanagnou <janis_papanag...(a)hotmail.com>
wrote:
> Lao Ming wrote:
> > I have a collection of files of all ascii data in which certain lines
> > contain an @ char.  In the lines that don't have the @ char, I want to
> > substitute a space.  However, since the @ is not at the beginning or
> > end of the line, I am having difficulty determining how to accomplish
> > this.
>
> > The data below is not the real data but it is a simple accurate
> > example of what I want to do.
>
> > 1988 G. H. W. Bush more data
> > 1992 @Bill Clinton more data
> > 1996 @Bill Clinton more data
> > 2000 G. W. Bush more data
> > 2004 G. W. Bush more data
> > 2008 @Barack Obama  more data
>
> > What I want the output to look like is where the names actually line
> > up in columns so that a space occurs in front of those names without
> > the @.
>
> > Is there anyway that this can be done?  Thanks.
>
> This is how I understand your requirement on the data you provided...
>
>   awk '!/@/ { $2 = " "$2 } 1'
>
> If that's not what you want, please provide an appropriate sample and
> also a sample of how the output should look like.
>
> Janis


Sorry for the confusion. The "more data" is actually more columns of
numeric data that I have already successfully formatted into columns.
The desired output is below (where an underbar ( _ ) indicates the
space that I need to be inserted). As you can imagine, the inserted
char needs to be accomplished before the "more data" is formatted or
the insert will just push the more data out of order.

1988 _G. H. W. Bush more data
1992 @Bill Clinton more data
1996 @Bill Clinton more data
2000 _G. W. Bush more data
2004 _G. W. Bush more data
2008 @Barack Obama  more data


Thanks for all the suggestions but, so far, nothing has quite worked
out. I much appreciate all the help.
From: Lao Ming on
On Jun 21, 12:29 am, Lao Ming <laoming...(a)gmail.com> wrote:
> On Jun 20, 12:56 am, Janis Papanagnou <janis_papanag...(a)hotmail.com>
> wrote:
>
>
>
>
>
> > Lao Ming wrote:
> > > I have a collection of files of all ascii data in which certain lines
> > > contain an @ char.  In the lines that don't have the @ char, I want to
> > > substitute a space.  However, since the @ is not at the beginning or
> > > end of the line, I am having difficulty determining how to accomplish
> > > this.
>
> > > The data below is not the real data but it is a simple accurate
> > > example of what I want to do.
>
> > > 1988 G. H. W. Bush more data
> > > 1992 @Bill Clinton more data
> > > 1996 @Bill Clinton more data
> > > 2000 G. W. Bush more data
> > > 2004 G. W. Bush more data
> > > 2008 @Barack Obama  more data
>
> > > What I want the output to look like is where the names actually line
> > > up in columns so that a space occurs in front of those names without
> > > the @.
>
> > > Is there anyway that this can be done?  Thanks.
>
> > This is how I understand your requirement on the data you provided...
>
> >   awk '!/@/ { $2 = " "$2 } 1'
>
> > If that's not what you want, please provide an appropriate sample and
> > also a sample of how the output should look like.
>
> > Janis
>
> Sorry for the confusion.  The "more data" is actually more columns of
> numeric data that I have already successfully formatted into columns.
> The desired output is below (where an underbar ( _ ) indicates the
> space that I need to be inserted).  As you can imagine, the inserted
> char needs to be accomplished before the "more data" is formatted or
> the insert will just push the more data out of order.
>
> 1988 _G. H. W. Bush    more data
> 1992 @Bill Clinton        more data
> 1996 @Bill Clinton        more data
> 2000 _G. W. Bush        more data
> 2004 _G. W. Bush        more data
> 2008 @Barack Obama  more data
>
> Thanks for all the suggestions but, so far, nothing has quite worked
> out.  I much appreciate all the help.

I should also mention that the names of the President are not actually
column 2. Having a space between their first name and last name
required me to concatenate them with awk in the following way (where
columns 6 and 7 are the concatenated name). This makes it difficult
to add a space prior to the first name where some lines have the @
char.

|awk '{a=$6" "$7; printf \
"%4s %3s %2s %-25s %4s %2s %2s %2s %2s %2s\n", \
$5, $3, $4, a, $8, $9, $10, $11,$12,$13}'

From: Lao Ming on
On Jun 20, 12:56 am, Janis Papanagnou <janis_papanag...(a)hotmail.com>
wrote:
> Lao Ming wrote:
> > I have a collection of files of all ascii data in which certain lines
> > contain an @ char.  In the lines that don't have the @ char, I want to
> > substitute a space.  However, since the @ is not at the beginning or
> > end of the line, I am having difficulty determining how to accomplish
> > this.
>
> > The data below is not the real data but it is a simple accurate
> > example of what I want to do.
>
> > 1988 G. H. W. Bush more data
> > 1992 @Bill Clinton more data
> > 1996 @Bill Clinton more data
> > 2000 G. W. Bush more data
> > 2004 G. W. Bush more data
> > 2008 @Barack Obama  more data
>
> > What I want the output to look like is where the names actually line
> > up in columns so that a space occurs in front of those names without
> > the @.
>
> > Is there anyway that this can be done?  Thanks.
>
> This is how I understand your requirement on the data you provided...
>
>   awk '!/@/ { $2 = " "$2 } 1'
>
> If that's not what you want, please provide an appropriate sample and
> also a sample of how the output should look like.
>
> Janis

Please ignore my two earlier posts. I just got the script to work by
massaging Janis' code above. Many thanks. It looked to me like all
of the suggestions (given the correct massage by me) would have
worked. I'm sorry about not providing the exact data input. But the
data required so much massaging that I thought the original would be
way too imposing to post so, thus, I tried to simplify it. Thanks
again!