I have a file with below data:
5773|2015-11-19|2016418|O|2015-11-06|C|AB1CD|826|826|CDE|2015-11-19|22222222222222222222222222222|ABCD|AB|30
5773|2015-11-19|2016418|O|2015-11-06|C|AB1CD|826|826|CDE|2015-11-19|88888888888888888888888888888|ABCD|AB|30
5773|2015-11-19|2016418|O|2015-11-06|C|AB1CD|826|826|CDE|2015-11-19|22222222222222222222222222222|ABCD|AB|30
5773|2015-11-19|2016418|O|2015-11-06|C|AB1CD|826|826|CDE|2015-11-19|55555555555555555555555555555|ABCD|AB|30
The data is separated with control-A character but I have replaced here with |
as I was unable to post data with control-A.
I want to trim 22222222222222222222222222222
value and want first 5 value so our final output data should be like this:
5773|2015-11-19|2016418|O|2015-11-06|C|AB1CD|826|826|CDE|2015-11-19|22222|ABCD|AB|30
5773|2015-11-19|2016418|O|2015-11-06|C|AB1CD|826|826|CDE|2015-11-19|88888|ABCD|AB|30
5773|2015-11-19|2016418|O|2015-11-06|C|AB1CD|826|826|CDE|2015-11-19|22222|ABCD|AB|30
5773|2015-11-19|2016418|O|2015-11-06|C|AB1CD|826|826|CDE|2015-11-19|55555|ABCD|AB|30
Looking some help in this.
You could do it with awk
like so:
awk -F\| 'BEGIN {OFS="|"};{$12=substr($12,1,5)};1'
-F
sets the field separator to |
, then before starting to process text we use the BEGIN
block to set the output field separator (OFS
) to |
as well. Then we replace the value in the 12th field with the first 5 characters of that field for each record.
You can give the filename to process as another argument on the command line, which is what you probably want, or use this in a pipeline if you have a process that is generating the file you want to update.
Thanks to Stéphane Chazelas for fixing some issues I had and making it more POSIX-y. In particular, for fixing my start index from 0 to 1. GNU's documentation includes the following line that let me get away with the bug:
If start is less than one, substr() treats it as if it was one.
but 1
is the index of the first character, and so it should be used, especially for people not using gawk
. For instance with mawk
:
$ mawk 'BEGIN{print substr("1234567", 0, 5)}'
1234
$ mawk 'BEGIN{print substr("1234567", 1, 5)}'
12345
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments