| Author |
add increment field when first field changes
|
|
| ayman_zekry@yahoo.com 2005-11-14, 6:56 pm |
| Hi
I have a file that looks like the following:
Name1 aaa bbbb
Name1 ffd hhghg
Name1 dffg ghhhh
Name2 rtrt dfff
Name2 rrtfr gtt
Name2 dsddf gfggf
Name2 ffffg gfgf
NAme3 fdffg hhgh
Is it possible to format it so that a number increment field is added
whenever the first field in a record changes. like that
1 Name1 aaa bbbb
1 Name1 ffd hhghg
1 Name1 dffg ghhhh
2 Name2 rtrt dfff
2 Name2 rrtfr tgtt
2 Name2 dsddf gfggf
2 Name2 ffffg gfgf
Thanks
| |
| Sebastian Luque 2005-11-14, 6:56 pm |
| "ayman_zekry@yahoo.com" <ayman_zekry@yahoo.com> wrote:
> Hi
> I have a file that looks like the following:
> Name1 aaa bbbb
> Name1 ffd hhghg
> Name1 dffg ghhhh
> Name2 rtrt dfff
> Name2 rrtfr gtt
> Name2 dsddf gfggf
> Name2 ffffg gfgf
> NAme3 fdffg hhgh
> Is it possible to format it so that a number increment field is added
> whenever the first field in a record changes. like that
> 1 Name1 aaa bbbb
> 1 Name1 ffd hhghg
> 1 Name1 dffg ghhhh
> 2 Name2 rtrt dfff
> 2 Name2 rrtfr tgtt
> 2 Name2 dsddf gfggf
> 2 Name2 ffffg gfgf
Does the following work for you?
{
nid = substr($1, match($1, /[0-9]+$/), length($1))
print nid, $0
}
--
Sebastian P. Luque
| |
| Ed Morton 2005-11-14, 6:56 pm |
| ayman_zekry@yahoo.com wrote:
> Hi
> I have a file that looks like the following:
>
> Name1 aaa bbbb
> Name1 ffd hhghg
> Name1 dffg ghhhh
> Name2 rtrt dfff
> Name2 rrtfr gtt
> Name2 dsddf gfggf
> Name2 ffffg gfgf
> NAme3 fdffg hhgh
>
> Is it possible to format it so that a number increment field is added
> whenever the first field in a record changes. like that
>
> 1 Name1 aaa bbbb
> 1 Name1 ffd hhghg
> 1 Name1 dffg ghhhh
> 2 Name2 rtrt dfff
> 2 Name2 rrtfr tgtt
> 2 Name2 dsddf gfggf
> 2 Name2 ffffg gfgf
>
> Thanks
>
awk '$1!=p{n++}{print n,$0;p=$1}' file
Ed.
| |
| ayman_zekry@yahoo.com 2005-11-14, 6:56 pm |
| Well actually it does work if the field names are called
Name1,Name2,....etc.
What I meant is any name for the first field without a specific pattern
e.g
white ffff hhhh
white llll kkkk
red nmn mnmnn
red jjjj jjjhjh
etc
I am trying to find a way to put a number in the first field and
increment it whenever the fields changes.
sorry for the confusion.
Thanks for your help
| |
| ayman_zekry@yahoo.com 2005-11-14, 6:56 pm |
| Thanks so much .
This works great.
appreciate your help.
| |
| Kenny McCormack 2005-11-14, 6:56 pm |
| In article <cqCdndMWM8-heuXeRVn-uA@comcast.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
....
>awk '$1!=p{n++}{print n,$0;p=$1}' file
>
> Ed.
awk '$1!=p{n++;p=$1}{print n,$0}' file
| |
| Steffen Schuler 2005-11-16, 3:55 am |
| ayman_zekry@yahoo.com wrote:
> I have a file that looks like the following:
>
> Name1 aaa bbbb
> Name1 ffd hhghg
> Name1 dffg ghhhh
> Name2 rtrt dfff
> Name2 rrtfr gtt
> Name2 dsddf gfggf
> Name2 ffffg gfgf
> NAme3 fdffg hhgh
>
> Is it possible to format it so that a number increment field is added
> whenever the first field in a record changes. like that
>
> 1 Name1 aaa bbbb
> 1 Name1 ffd hhghg
> 1 Name1 dffg ghhhh
> 2 Name2 rtrt dfff
> 2 Name2 rrtfr tgtt
> 2 Name2 dsddf gfggf
> 2 Name2 ffffg gfgf
Hi,
#!/usr/bin/awk -f
BEGIN {OFS = "\t"}
$1 != f {++i; f = $1}
{print i, $0}
Regards,
Steffen
|
|
|
|