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

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!!!
dba user [ Mo, 12 November 2007 22:19 ] [ ID #1868205 ]

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

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 <http://cfaj.freeshell.org/shell/>
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
cfajohnson [ Mo, 12 November 2007 22:25 ] [ ID #1868206 ]

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

"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 [at] 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'
[at] x = split " ", $_, 11;
splice [at] x, 3, 4;
printf "%-10s" x ( [at] x - 1 ) . "%-15s\n", [at] 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
krahnj [ Mo, 12 November 2007 23:34 ] [ ID #1868207 ]

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

On Nov 12, 1:25 pm, "Chris F.A. Johnson" <cfajohn... [at] gmail.com> 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 <http://cfaj.freeshell.org/shell/>
> 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.
dba user [ Di, 13 November 2007 00:49 ] [ ID #1868997 ]

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

On Nov 12, 2:34 pm, "John W. Krahn" <kra... [at] telus.net> 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 [at] 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'
> [at] x = split " ", $_, 11;
> splice [at] x, 3, 4;
> printf "%-10s" x ( [at] x - 1 ) . "%-15s\n", [at] 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.
dba user [ Di, 13 November 2007 00:50 ] [ ID #1868998 ]
Linux » comp.unix.shell » Print all fields following 10th field and selected fields - awk/sed/perl

Vorheriges Thema: Sed reqular expression
Nächstes Thema: Some optimal xterm config