Function to get space works wrong

This is a function created in awk in bash.

get_space() {
    local prohledavany_blok="$1"
    local space

    space=$(echo "$prohledavany_blok" | awk '
NR==1 {
        sub(/[ \t]*$/, "");
        if (match($0, /array\($/)) {
            sub(/array\($/, "", $0);
        }
    }
    {
        if (match($0, /\$sql_array/)) {
            sub(/\$sql_array.*/, "");
        }
        print $0
    }
    ')
    echo "$space"
}

It should return space on a line before sql_array. It returns whole multiline block with code, which is wrong.

It is called like that:

space=$(get_space "$prohledavany_block")

Original code which works fine for me is not in a function:

        # Získání textu před definicí pole $sql_array
        space=$( echo "$prohledavany_block" | awk 'NR==1' | awk '
          { 
            sub(/[ \t]*$/, ""); 
            if (match($0, /array\($/)) {
              sub(/array\($/, "", $0);
            } 
          } 
          { 
            if (match($0, /\$sql_array/)) {
              sub(/\$sql_array.*/, "");
            } 
            print $0 
          }'
        )

If you are interested how I am getting the prohledavany_block

for file in $(find . -type f -name "*.php"); do

   # seek for definition of $sql_array
    start_lines=$(grep -n "\$sql_array" "$file" | cut -d ":" -f 1)
    for start_line in $start_lines; do
        echo "START LINE:$start_line*\n"

        # read block after start_line
        end_line=$((start_line + MAX_SQL_BLOCK_LENGTH))
        prohledavany_block=$(sed -n "${start_line},${end_line}p" "$file")

A piece of php code as a source string - input...

	if ($forum_id !== false)
	{
		if (!is_array($forum_id))
		{
			$forum_id = array($forum_id);
		}

		// Exchange key/value pair to be able to faster check for the forum id existence
		$forum_id_ary = array_flip($forum_id);
	}

	$sql_array = array(
		'SELECT'	=> 'm.*, u.user_colour, g.group_colour, g.group_type',

		'FROM'		=> array(
			MODERATOR_CACHE_TABLE	=> 'm',
		),

		'LEFT_JOIN'	=> array(
			array(
				'FROM'	=> array(USERS_TABLE => 'u'),
				'ON'	=> 'm.user_id = u.user_id',
			),
			array(
				'FROM'	=> array(GROUPS_TABLE => 'g'),
				'ON'	=> 'm.group_id = g.group_id',
			),
		),

		'WHERE'		=> 'm.display_on_index = 1',
	);

	/** @var \phpbb\group\helper $group_helper */
	$group_helper = $phpbb_container->get('group_helper');

	// We query every forum here because for caching we should not have any parameter.
	$sql = $db->sql_build_query('SELECT', $sql_array);
	$result = $db->sql_query($sql, 3600);

My question is simple. What am I doing wrong in the process of moveing the code from

space=$( echo "$prohledavany_block" | awk 'NR==1' | awk '

to the function get_space?

The original code has a separate awk 'NR==1' process which only passes on one record.

In the code inside the function, there are two blocks of code inside { }. But the NR == 1 test only applies to the first code block.The second code block is executed for the whole file, not just the first record.

1 Like

Thank you, so I have found how to cut the first line. The awk was OK. I just needed to add the head -n 1 after awk.

get_space() {
    local input_block="$1"
    local space
    space=$(echo "$input_block" | head -n 1 | awk 'NR==1 {
        sub(/[ \t]*$/, "");
        if (match($0, /array\(.*$/)) {
            sub(/array\($/, "", $0);
        }
    }
    NR==1 {
        if (match($0, /\$sql_array/)) {
            sub(/\$sql_array.*/, "");
        }
        print $0
    }
    ' )
    echo "$space"
}