Print all fields following 10th field and selected fields - awk/sed/perl

Print all fields following 10th field and selected fields - awk/sed/perl

am 12.11.2007 22:19:40 von dba user

Hi Group,

I have a file in the following format. The values are not in fixed
length format. I would like to print fields 1,2,3,8,9,10 and
everything following field 10. Fields 1 through 10 will not have any
spaces. However, the last field is a varaible length text field with
spaces

$ cat file-name

Key1 col2 col3 col4 .. col10 text doc
Key2 col2 col3 col4 .. col10 text doc with
Key3 col2 col3 col4 .. ccol10 text doc with spaces
Key4 col2 col3 col4 .. cccol10 text

If I do, awk 'BEGIN {printf "%-10s %-10s .. %-50s",
$1.$2,$3,$8,$9,$10,$11}' file-name, I get only the first word of the
text field. Since the length of fields from 1 to 10 is also a
variable, the substr is challenging.

How do I get the above done using sed or awk or perl.

Thank you!!!

Re: Print all fields following 10th field and selected fields - awk/sed/perl

am 12.11.2007 22:25:25 von cfajohnson

On 2007-11-12, da. Ram wrote:
>
> I have a file in the following format. The values are not in fixed
> length format. I would like to print fields 1,2,3,8,9,10 and
> everything following field 10. Fields 1 through 10 will not have any
> spaces. However, the last field is a varaible length text field with
> spaces
>
> $ cat file-name
>
> Key1 col2 col3 col4 .. col10 text doc
> Key2 col2 col3 col4 .. col10 text doc with
> Key3 col2 col3 col4 .. ccol10 text doc with spaces
> Key4 col2 col3 col4 .. cccol10 text
>
> If I do, awk 'BEGIN {printf "%-10s %-10s .. %-50s",
> $1.$2,$3,$8,$9,$10,$11}' file-name, I get only the first word of the
> text field. Since the length of fields from 1 to 10 is also a
> variable, the substr is challenging.

If there is a single space between the fields:

cut -d ' ' -f 10- file-name

--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: Print all fields following 10th field and selected fields - awk/sed/perl

am 12.11.2007 23:34:09 von krahnj

"da. Ram" wrote:
>
> I have a file in the following format. The values are not in fixed
> length format. I would like to print fields 1,2,3,8,9,10 and
> everything following field 10. Fields 1 through 10 will not have any
> spaces. However, the last field is a varaible length text field with
> spaces
>
> $ cat file-name
>
> Key1 col2 col3 col4 .. col10 text doc
> Key2 col2 col3 col4 .. col10 text doc with
> Key3 col2 col3 col4 .. ccol10 text doc with spaces
> Key4 col2 col3 col4 .. cccol10 text
>
> If I do, awk 'BEGIN {printf "%-10s %-10s .. %-50s",
> $1.$2,$3,$8,$9,$10,$11}' file-name, I get only the first word of the
> text field. Since the length of fields from 1 to 10 is also a
> variable, the substr is challenging.
>
> How do I get the above done using sed or awk or perl.

john@d66-183-63-222:~ > echo "Key1 col2 col3 col4 col5 col6 col7 col8
col9 col10 text doc
Key2 col2 col3 col4 col5 col6 col7 col8 col9 col10 text doc with
Key3 col2 col3 col4 col5 col6 col7 col8 col9 ccol10 text doc with spaces
Key4 col2 col3 col4 col5 col6 col7 col8 col9 cccol10 text" |\
perl -lne'
@x = split " ", $_, 11;
splice @x, 3, 4;
printf "%-10s" x ( @x - 1 ) . "%-15s\n", @x;
'
Key1 col2 col3 col8 col9 col10 text
doc
Key2 col2 col3 col8 col9 col10 text doc
with
Key3 col2 col3 col8 col9 ccol10 text doc
with spaces
Key4 col2 col3 col8 col9 cccol10
text



John
--
use Perl;
program
fulfillment

Re: Print all fields following 10th field and selected fields - awk/sed/perl

am 13.11.2007 00:49:21 von dba user

On Nov 12, 1:25 pm, "Chris F.A. Johnson" wrote:
> On 2007-11-12, da. Ram wrote:
>
>
>
>
>
> > I have a file in the following format. The values are not in fixed
> > length format. I would like to print fields 1,2,3,8,9,10 and
> > everything following field 10. Fields 1 through 10 will not have any
> > spaces. However, the last field is a varaible length text field with
> > spaces
>
> > $ cat file-name
>
> > Key1 col2 col3 col4 .. col10 text doc
> > Key2 col2 col3 col4 .. col10 text doc with
> > Key3 col2 col3 col4 .. ccol10 text doc with spaces
> > Key4 col2 col3 col4 .. cccol10 text
>
> > If I do, awk 'BEGIN {printf "%-10s %-10s .. %-50s",
> > $1.$2,$3,$8,$9,$10,$11}' file-name, I get only the first word of the
> > text field. Since the length of fields from 1 to 10 is also a
> > variable, the substr is challenging.
>
> If there is a single space between the fields:
>
> cut -d ' ' -f 10- file-name
>
> --
> Chris F.A. Johnson, author
> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> ===== My code in this post, if any, assumes the POSIX locale
> ===== and is released under the GNU General Public Licence

Thanks for the reponse,

There could be multiple spaces and also I want the out to be formated.
So, I am not sure this would work.

Re: Print all fields following 10th field and selected fields - awk/sed/perl

am 13.11.2007 00:50:41 von dba user

On Nov 12, 2:34 pm, "John W. Krahn" wrote:
> "da. Ram" wrote:
>
> > I have a file in the following format. The values are not in fixed
> > length format. I would like to print fields 1,2,3,8,9,10 and
> > everything following field 10. Fields 1 through 10 will not have any
> > spaces. However, the last field is a varaible length text field with
> > spaces
>
> > $ cat file-name
>
> > Key1 col2 col3 col4 .. col10 text doc
> > Key2 col2 col3 col4 .. col10 text doc with
> > Key3 col2 col3 col4 .. ccol10 text doc with spaces
> > Key4 col2 col3 col4 .. cccol10 text
>
> > If I do, awk 'BEGIN {printf "%-10s %-10s .. %-50s",
> > $1.$2,$3,$8,$9,$10,$11}' file-name, I get only the first word of the
> > text field. Since the length of fields from 1 to 10 is also a
> > variable, the substr is challenging.
>
> > How do I get the above done using sed or awk or perl.
>
> john@d66-183-63-222:~ > echo "Key1 col2 col3 col4 col5 col6 col7 col8
> col9 col10 text doc
> Key2 col2 col3 col4 col5 col6 col7 col8 col9 col10 text doc with
> Key3 col2 col3 col4 col5 col6 col7 col8 col9 ccol10 text doc with spaces
> Key4 col2 col3 col4 col5 col6 col7 col8 col9 cccol10 text" |\
> perl -lne'
> @x = split " ", $_, 11;
> splice @x, 3, 4;
> printf "%-10s" x ( @x - 1 ) . "%-15s\n", @x;
> '
> Key1 col2 col3 col8 col9 col10 text
> doc
> Key2 col2 col3 col8 col9 col10 text doc
> with
> Key3 col2 col3 col8 col9 ccol10 text doc
> with spaces
> Key4 col2 col3 col8 col9 cccol10
> text
>
> John
> --
> use Perl;
> program
> fulfillment


Thanks for the response. I will try this and see how it works.