Hi All,
I have a file having one line only.
It is like
trapsess:inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|78|0037|
I want to replace the numbers in last two columns by As.
It should look like
trapsess:inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|AA|AAAA|
Please, suggest me any shell command for this.
Thanks in Advance.
clx
June 8, 2011, 9:32am
2
One way,
awk -F\| '{$(NF-1)="AAAA";$(NF-2)="AA";print}' OFS=\| file
perl -F"\|" -lane 'BEGIN{$,="|"}$F[-1]=~s/\d/A/g;$F[-2]=~s/\d/A/g;print @F'
awk command is not working, it is printing the my desired output, but not replacing the actual file.
I can redirect the output, but it may be a little more long in terms of line of code.
Thanks all for replying
clx
June 8, 2011, 9:41am
5
Okies.. I misread the requirements.
Here is the updated one.
awk -F\| '{gsub(/[0-9]/,"A",$(NF-1));gsub(/[0-9]/,"A",$(NF-2));print}' OFS=\| file
EDIT: yeah. that wont edit the file.
mukeshbaranwal:
awk command is not working, it is printing the my desired output, but not replacing the actual file.
I can redirect the output, but it may be a little more long in terms of line of code.
Thanks all for replying
If you want to edit the original file, use the below command
perl -i.bak -F"\|" -lane 'BEGIN{$,="|"}$F[-1]=~s/\d/A/g;$F[-2]=~s/\d/A/g;print @F'
input
Hi Anchal, I am still in same place. It is not replacing, just printing my desired requirement.
Thanks
try this ..
$ echo "inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|78|0037|" | sed 's,|[0-9][0-9]|[0-9][0-9][0-9][0-9]|,|AA|AAAA|,g'
inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|AA|AAAA|
clx
June 8, 2011, 10:01am
9
Awk writes to the stdout.
If you don't want to create a temp file and must want to edit the original file. use perl (posted by getmmg).
ctsgnb
June 8, 2011, 10:04am
10
jayan_jay:
try this ..
$ echo "inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|78|0037|" | sed 's,|[0-9][0-9]|[0-9][0-9][0-9][0-9]|,|AA|AAAA|,g'
inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|AA|AAAA|
or more simply
$ echo "inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|78|0037|" | sed 's:[^|]*|[^|]*|$:AA|AAAA|:'
inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|AA|AAAA|
borrowing from anchal_khare:
{ rm file; awk -F\| '{$(NF-1)="AAAA";$(NF-2)="AA";print}' OFS=\| file; } < file
I believe you meant to redirect AWK's stdout to "file". Otherwise, AWK will try to open ./file for reading, a pathname which does not exist after the rm.
As with any use of the unlink trick, should the last process holding open a file descriptor to the original "file" crash, you've lost the data (just a heads up for novices ;)).
Regards,
Alister
1 Like
alister:
I believe you meant to redirect AWK's stdout to "file". Otherwise, AWK will try to open ./file for reading, a pathname which does not exist after the rm.
As with any use of the unlink trick, should the last process holding open a file descriptor to the original "file" crash, you've lost the data (just a heads up for novices ;)).
Regards,
Alister
ooops, fat fingers - good catch, thanks!
{ rm file; awk -F\| '{$(NF-1)="AAAA";$(NF-2)="AA";print}' OFS=\| > file; } < file