Re: cut command
- From: Janis <janis_papanagnou@xxxxxxxxxxx>
- Date: Mon, 09 Jul 2007 07:30:29 -0700
On 9 Jul., 15:49, ashu <ashishmoury...@xxxxxxxxx> wrote:
On Jul 9, 6:23 pm, Janis <janis_papanag...@xxxxxxxxxxx> wrote:
On 9 Jul., 15:11, ashu <ashishmoury...@xxxxxxxxx> wrote:
On Jul 9, 5:32 pm, Ed Morton <mor...@xxxxxxxxxxxxxx> wrote:
Janis wrote:
On 9 Jul., 13:16, ashu <ashishmoury...@xxxxxxxxx> wrote:
i want to cut some field from a file in a specific order but cut
command is printing them in default manner.
eg: cut -d" " -f 3,2,1 filename
wil print
field 1 2 3
while i want to print them in order 3,2,1
what is problem ?
Using awk seems better suited.
awk '{print $3,$2,$1}' filename
Note that this is not equivalent to your cut command if the fields are
separated by more than a single space; your cut treats two consecutive
spaces as a non-present field and displays that field it empty. So the
awk or the cut solution may or may not be what you need.
Or you can set the awk field separator to a single blank character:
$ echo "a b c" | awk '{for (i=1;i<=NF;i++) printf "%d:<%s>\n",i,$i}'
1:<a>
2:<b>
3:<c>
$ echo "a b c" | awk -F'[ ]' '{for (i=1;i<=NF;i++) printf
"%d:<%s>\n",i,$i}'
1:<a>
2:<>
3:<b>
4:<>
5:<c>
so the OP could use:
awk -F'[ ]' '{print $3,$2,$1}' filename
Regards,
Ed.
it means it is not possible to use cut command as i want ?
Awk is a standard tool. What's wrong with using awk?
Have you a task to solve or some homework to do which forces you to
use cut? In the latter case I'd inspect the man pages of cut, you'll
find there what's possible and what's not possible with cut.
Janis
nothing is wrong using awk but i read man page of cut and thinking
that this command should be more useful by adding some extra
code ,then we can use it in our way. and one last question if in
question they tell us to make shell script , can we use awk in it ?
I'd really ask the person who assigned the task, but my opinion on
that is; *if* you are allowed to use cut(1) you should as well be
allowed to use awk(1), both are standard Unix tools external to the
shell[*]. If, OTOH, the task is formulated to just use shell features
then you wouldn't be allowed to use cut, either; you would then resort
to shell builtins, maybe something like
while read -r f1 f2 f3
do printf "%s %s %s\n" "$f3" "$f2" "$f1"
done
Janis
[*] Though implementors of newer shells think about implementing such
basic commands into the shell for performance reasons. In this respect
cut might be a candidate while awk, a complete programming language,
might not be. (But this is quite academic.)
.
- References:
- cut command
- From: ashu
- Re: cut command
- From: Janis
- Re: cut command
- From: Ed Morton
- Re: cut command
- From: ashu
- Re: cut command
- From: Janis
- Re: cut command
- From: ashu
- cut command
- Prev by Date: Re: cut command
- Next by Date: Sorting du -h?
- Previous by thread: Re: cut command
- Next by thread: Re: cut command
- Index(es):
Relevant Pages
|