Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading