Trim a specific field and replace in Unix Shell Script

Sandeep Singh

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.

Eric Renouf

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to replace string in unix shell script

From Dev

UNIX Shell Script Backup Directory on Specific day

From Dev

Run a shell script in background in Unix at specific time daily

From Dev

How to cut a specific character in a file in unix shell script

From Dev

Unix shell script to remove new lines preceded with specific characters

From Dev

Shell Script to trim if else condition

From Dev

Arithmetic in Unix shell Script

From Dev

Unix Shell Script

From Dev

Shell Script - Unix

From Dev

Shell script, linux, unix, shell

From Dev

replace specific char of a string using sed using shell script

From Dev

SSIS: Trim field for specific part of string

From Dev

shell script replace , with ','

From Dev

shell script replace , with ','

From Dev

shell script sed replace

From Dev

Unix Shell Script: While loop

From Dev

Unix shell script - purpose of colon

From Dev

Beginner Unix shell script issues

From Dev

unix shell script to check for EOF

From Dev

Beginner Unix shell script issues

From Dev

missing `]' error in unix shell script

From Dev

Nested expression in a UNIX shell script

From Dev

Linux Shell Script: How to compare a specific field in a text document with specific text in an if statement

From Dev

Run Perl Script From Unix Shell Script

From Dev

unix shell replace string twice (in one line)

From Dev

Unix Shell Loop through files and replace texts

From Dev

shell linux unix xargs -i replace string

From Dev

How to replace the top shell script?

From Dev

Replace special characters in specific field