Reputation: 11
Need to check in a file for 4th field with length 9 if the length is 9 I need to pad a space in that 4th field and make it as 10
Unix server
I used the below logic
#!bin/bash
fil_n="/tmp/vishal/09082019/237_20190801143653.creq"
while IFS= read line
do
var=`echo $line|cut -f4 -d "," | grep -v '\#'`
echo $var
len=${#var}
echo $len
if [ $len -eq 9 ]; then
var2=" " var3="$var2$var"
echo "var3 = $var3"
echo $line | sed -i "s/${var}/${var3}/g" "$fil_n"
fi
done < "$fil_n"
but the sed is not padding the replaced value to the file
Note: Search and Replace in same file
File :
0008627921,2,A,0FE3B20F,CLW,01,07/29/2019,12:54:04,CLW,01,07/29/2019,12:59:56,00700,00680,00,02,07799871,1,*,000,0000, , ,0000000000,0008627906,00,0
expected 2 spaces In front of 0FE3B20F
INPUT:
#HEADER,CREQ,014194,07/29/2019,237,AT,08/01/2019,02:36:52
0008627921,2,A,<space>0FE3B20F,CLW,01,07/29/2019,12:54:04,CLW,01,07/29/2019,12:59:56,00700,00680,00,02,07799871,1,,000,0000, , ,0000000000,0008627906,00,0
0008627920,2,A,065000075A,CLW,01,07/29/2019,12:41:04,CLW,01,07/29/2019,12:46:56,00700,00680,00,02,07799870,1,,000,0000, , ,0000000000,0008627905,00,1
#TRAILER,014194,08/01/2019,00000002,0000001360
OUTPUT:
#HEADER,CREQ,014194,07/29/2019,237,AT,08/01/2019,02:36:52
0008627921,2,A,<space><space>0FE3B20F,CLW,01,07/29/2019,12:54:04,CLW,01,07/29/2019,12:59:56,00700,00680,00,02,07799871,1,,000,0000, , ,0000000000,0008627906,00,0
0008627920,2,A,065000075A,CLW,01,07/29/2019,12:41:04,CLW,01,07/29/2019,12:46:56,00700,00680,00,02,07799870,1,,000,0000, , ,0000000000,0008627905,00,1
#TRAILER,014194,08/01/2019,00000002,0000001360
Upvotes: 0
Views: 528
Reputation: 12448
You can use the following awk
command:
awk 'BEGIN{FS=OFS=","}{while(length($4)<10){$4=" "$4};print}' test.creq
0008627921,2,A, 0FE3B20F,CLW,01,07/29/2019,12:54:04,CLW,01,07/29/2019,12:59:56,00700,00680,00,02,07799871,1,*,000,0000, , ,0000000000,0008627906,00,0
Explanations:
BEGIN{FS=OFS=","}
set the field separator as well as the output field separator to ,
while(length($4)<10){$4=" "$4}
pad space in front of the 4th field until it reaches a length of 10 print
the lineCombined in one command:
(cp test.creq backup.creq && awk 'BEGIN{FS=OFS=","}{while(length($4)<10){$4=" "$4};print}' test.creq > tmp.creq && mv tmp.creq test.creq)
You can also use directly the following simplified command using sprintf
similar to the c++ one:
awk 'BEGIN{FS=OFS=","} {$4 = sprintf("%10s", $4)}1' test.creq
Input:
cat test.creq
#HEADER,CREQ,014194,07/29/2019,237,AT,08/01/2019,02:36:52
0008627921,2,A, 0FE3B20F,CLW,01,07/29/2019,12:54:04,CLW,01,07/29/2019,12:59:56,00700,00680,00,02,07799871,1,,000,0000, , ,0000000000,0008627906,00,0
0008627920,2,A,065000075A,CLW,01,07/29/2019,12:41:04,CLW,01,07/29/2019,12:46:56,00700,00680,00,02,07799870,1,,000,0000, , ,0000000000,0008627905,00,1
#TRAILER,014194,08/01/2019,00000002,0000001360
Output:
awk 'BEGIN{FS=OFS=","}!/^#/{while(length($4)<10){$4=" "$4};}1' test.creq
#HEADER,CREQ,014194,07/29/2019,237,AT,08/01/2019,02:36:52
0008627921,2,A, 0FE3B20F,CLW,01,07/29/2019,12:54:04,CLW,01,07/29/2019,12:59:56,00700,00680,00,02,07799871,1,,000,0000, , ,0000000000,0008627906,00,0
0008627920,2,A,065000075A,CLW,01,07/29/2019,12:41:04,CLW,01,07/29/2019,12:46:56,00700,00680,00,02,07799870,1,,000,0000, , ,0000000000,0008627905,00,1
#TRAILER,014194,08/01/2019,00000002,0000001360
Upvotes: 0
Reputation: 52449
Assuming you meant you want to pad an 8-character long 4th field to 10, not a 9 to 10...
awk 'BEGIN { FS=OFS="," } length($4) == 8 { $4 = sprintf("%10s", $4) } 1' input.csv
Upvotes: 3