|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi
I am extracting info from files that are held in $container , this string will always contain 4 values, I now need to generate a report thru these extractions. I use the following to print out $container values : for items in $container do echo "$items" done This is all OK, but now I need to assign a description to each item, these will always be in order of: "container stub weight type" How do I assoicated the description with the values in a for loop ? Or how do I do it another way , so I get something like: container LPSEC stub 3YB44 weight 432 type DOM Thanks Pete |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
peter sands wrote:
> Hi > I am extracting info from files that are held in $container , this > string will always contain 4 values, I now need to generate a report > thru these extractions. > > I use the following to print out $container values : > > for items in $container > do > echo "$items" > done > > This is all OK, but now I need to assign a description to each item, > these will always be in order of: > "container stub weight type" > > How do I assoicated the description with the values in a for loop ? > Or how do I do it another way , so I get something like: > > container LPSEC > stub 3YB44 > weight 432 > type DOM > > > Thanks > Pete > awk '{printf "container\t$1\nstub\t$2\nweight\t$3\ntype\t$4\n"} ' file would be the right way to get the output you want from a file, or this if you want to use an intermediate variable for some reason: echo "$container" | awk '{printf "container\t$1\nstub\t$2\nweight\t$3\ntype\t$4\n"} ' There's various alternatives. The right one to use depends what the rest of your script is doing. Ed. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
peter sands <peter_sands@techemail.com> wrote:
> This is all OK, but now I need to assign a description to each item, > these will always be in order of: > "container stub weight type" If I interpret your situation right, and your data is always contained in the same four fields in $container, in the same order, you can do this: $ set -- $container $ echo -e "container $1\nstub $2\nweight $3\ntype $4" The "set --" syntax sets the positional parameters $1 and up. If the value of $container is changing, you just have to wrap this code in a loop, like while read container ; do set -- $container echo -e "container $1\nstub $2\nweight $3\ntype $4" done -- Oh to have a lodge in some vast wilderness. Where rumors of oppression and deceit, of unsuccessful and successful wars may never reach me anymore. -- William Cowper |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On 2007-08-30, Jeremiah DeWitt Weiner wrote:
> peter sands <peter_sands@techemail.com> wrote: >> This is all OK, but now I need to assign a description to each item, >> these will always be in order of: >> "container stub weight type" > > If I interpret your situation right, and your data is always > contained in the same four fields in $container, in the same order, you > can do this: > > $ set -- $container > $ echo -e "container $1\nstub $2\nweight $3\ntype $4" > > The "set --" syntax sets the positional parameters $1 and up. If > the value of $container is changing, you just have to wrap this code in > a loop, like > > while read container ; do > set -- $container > echo -e "container $1\nstub $2\nweight $3\ntype $4" > done Even simpler: while read container stub weight type ; do printf "%s\n" "$container" "$stub" "$weight" "$type" done -- 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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Chris F.A. Johnson wrote:
> On 2007-08-30, Jeremiah DeWitt Weiner wrote: >> peter sands <peter_sands@techemail.com> wrote: >>> This is all OK, but now I need to assign a description to each item, >>> these will always be in order of: >>> "container stub weight type" >> If I interpret your situation right, and your data is always >> contained in the same four fields in $container, in the same order, you >> can do this: >> >> $ set -- $container >> $ echo -e "container $1\nstub $2\nweight $3\ntype $4" >> >> The "set --" syntax sets the positional parameters $1 and up. If >> the value of $container is changing, you just have to wrap this code in >> a loop, like >> >> while read container ; do >> set -- $container >> echo -e "container $1\nstub $2\nweight $3\ntype $4" >> done > > Even simpler: > > while read container stub weight type ; do > printf "%s\n" "$container" "$stub" "$weight" "$type" In the above format and with a portable echo command: echo " container $container stub $stub weight $weight type $type" > done > > > > -- Michael Tosch @ hp : com |
|
![]() |
| Outils de la discussion | |
|
|