Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an if block in awk

Tags:

linux

awk

I'm processing a file in awk.

I want to pass along the rows in the file that have blanks in column positions 25 through 34 and I want to do work on the rows that have blanks in column positions 10 through 19. Specifically I want to replace the blanks in columns positions 10 through 19 with 0s. That way the output file will have the original rows with blanks in 25-34 untouched. and the rows with blanks in 10-19 with have been replaced with '0's. So the output file will be the same as the input file only with zeros in the relevant rows in positions 10-19. The file looks like this:

###########################################
#########          ########################
###########################################
###########################################
###########################################
###########################################
###########################################
###########################################
#########          #####          #########
###########################################
###########################################
###########################################

I know I have to use an if block but I've never used one before in awk. The syntax below is what I think I need but please help me with the details. Specifically what I'm using to specify 'blanks' in the if statements.

I apologize ahead of time for the bad syntax. This is my first time using an If block in awk. I know the syntax doesn't work, which is one of the reasons I'm posting this.

cat scr2 | awk 'BEGIN {
    pos1=substr($0,25,10); 
    pos2=substr($0,10,10);

      if (pos1 = ^[[:blank:]]$) 
         printf $0 
      else if (pos2 == ^[[:blank:]]$)
         {val=substr($0,25,10)} 
         gsub(/ /,0,val){$0=substr($0,1,24) val substr($0,35)} 1}'`

The sample output would be :

###########################################
#########0000000000########################
###########################################
###########################################
###########################################
###########################################
###########################################
###########################################
#########          #####          #########
###########################################
###########################################
###########################################

So the row with blanks only at positions 10-19 gets changed and the row with blanks at both 10-19 and 25-34 get left alone.

like image 940
Carbon Avatar asked Apr 24 '26 03:04

Carbon


2 Answers

With your shown samples, please try following awk code, written and tested in GNU awk, should work in any awk.

awk '
substr($0,10,10) ~ /^ +$/ && substr($0,20) !~ / / {
  $0=substr($0,1,9) "0000000000" substr($0,20)
}
1
' Input_file

Explanation: Simple explanation would be, checking 2 conditions in main program of awk. 1st to make sure position 10th to 20th contains only space AND 2nd rest of the line's values are NOT having spaces in it, if this is the case then enter zeroes in place of spaces and print edited/non-edited lines.

like image 79
RavinderSingh13 Avatar answered Apr 27 '26 04:04

RavinderSingh13


I'd use sed here:

sed -E 's/^(.{9}) {10}(.{5}[^ ]{10})/\10000000000\2/' file
like image 29
glenn jackman Avatar answered Apr 27 '26 03:04

glenn jackman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!