Home > other >  Check if there is space in specific range of indexes in a line in a file and replace the space with
Check if there is space in specific range of indexes in a line in a file and replace the space with

Time:02-02

I have the following issue which I am facing. I have several lines in a file and I have an example shown below. The values between position 4-10 can contain any combinations of values and spaces and it has be replaced with 0 only if it is an empty space.

#input.txt

014       789000
234455    899800    
1213839489404040

In the example line above, we can see that we have empty spaces between position 4-10. This position to check is fixed. I want to be able to check if every line in my file has empty spaces between position 4-10, and if there is empty space, I want to be able to replace it with 0. I have provided below the desired output in the file.

#desired output in the input.txt

0140000000789000    # note that 0 has been added in the position 4-10
2344550000899800    # # note that 0 has been added in the position 7-10
1213839489404040

I was able to do the following in my code below. The code below is only able to add values at specified position using the sed command and I want to be able to modify this code such that it can do the task mentioned above.

May someone please help me ?

My Code:

#append script
function append_vals_to_line {
    insert_pos=$(($1 - 1))
    sed -i 's/\(.\{'$insert_pos'\}\)/\1'$2'/' "$3"
}

column_num_to_insert="$1"
append_value ="$2"
input_file_to_append_vals_in ="$3"

append_vals_to_line "$column_num_to_insert" "$append_value" "$input_file_to_append_vals_in"

CodePudding user response:

I suggest an awk solution for this:

awk '{s = substr($0, 4, 7); gsub(/ /, 0, s); print substr($0, 1, 3) s substr($0, 11)}' file

#input.txt

0140000000789000
2344550000899800
1213839489404040

A more readable version:

awk '{
   s = substr($0, 4, 7)
   gsub(/ /, 0, s)
   print substr($0, 1, 3) s substr($0, 11)
}' file

This command first gets a substring from 4th to 10th position in variable s. Then using gsub we replace each space with a 0 and finally we recompose and print whole line.


Consider this awk command that takes start and end position from arguments:

awk -v p1=4 -v p2=10 '{
   s = substr($0, p1, p2-p1 1)
   gsub(/ /, 0, s)
   print substr($0, 1, p1-1) s substr($0, p2 1)
}' file

CodePudding user response:

You can do:

awk '
/[[:blank:]]/{
    sub(/[[:blank:]]*$/,"")   # remove trailing spaces if any
    gsub(/[[:blank:]]/,"0")   # replace each /[[:blank:]]/ with "0"
} 1' file

Alternatively, you can do:

awk '
length($2) {
    $0=sprintf("%s%0*d",$1,16-length($1),$2)
} 1' file   

Either prints:

0140000000789000
2344550000899800
1213839489404040
  •  Tags:  
  • Related