Field Separator in printf (awk)

I can not figure out how to set the Output filed separator in awk when using printf.

Example:

cat file
some data
here_is_more information

Requested output

some------------data
her_is_more-----information

Here are some that does not work:

awk '{printf "%-15s %s\n",$1,$2}' OFS="-" file
some            data
here_is_more    information
awk '{printf "%-15s-%s\n",$1,$2}' file
some           -data
here_is_more   -information

printf doesn't use OFS, just whatever you specify in the format string. You can explicitly supply OFS as a %s argument if you want though:

awk '{printf "%-15s%s%s\n",$1,OFS,$2}' OFS="-" file

Supplementing CarloM's reply, here is something that can be found in the GNU awk's manual.

The print statement separates the fields using the char determined by OFS, whereas, in printf the format is specified as a format string.

@CarloM
This will just print one "-" and not fill the gaps.

Here is one solution that gives me correct output:

awk '{for (i=1;i<=(15-length($1));i++) s=s "-";printf "%s%s%s\n",$1,s,$2;s=""}' file
some-----------data
here_is_more---information

In addition to what has already been said, you're not looking for a field separator, you're looking for a field fill character. There are several ways to do this, here are two:

awk '{printf("%s%s%s\n", $1, substr("---------------", length($1)), $2)}' file
awk '{printf("%16.16s%s\n", $1 "---------------", $2)}' file

Both of these assume that 1 <= length($1) <= 15.

Both produce the output:

some------------data
here_is_more----information

when given your sample input.

Hello,

Simply one more approach for same, in spite of OFS used one variable.

awk -vs1="-------------------" -F" " '{printf"%-15s%s%15s\n",$1,s1,$2}' file_name

output will be as follows.

ome            -------------------           data
here_is_more   -------------------    information

Thanks,
R. Singh

@RavinderSingh13 I do not want the blank spaces.
@Don Cargun Thanks for explication and for the good solution

Here is another way

awk '{v=sprintf("%-15s%s",$1,$2);gsub(/ /,"-",v);print v}' t
some-----------data
here_is_more---information

this may help too.

awk -vs1="-------------------" -F" " '{print $1s1$2}' file_name

Output will be as follows.

ome-------------------data
here_is_more-------------------information

thanks,
R. Singh

@RavinderSingh13
Not to very much help, since format of column are lost.
information should be just below data

Hello Jotne,

Could you please try the following, not sure if it is good, but I tried my best to do this. Please take a look and see if this helps. Let us say input file is check_OFS_and_variable.

if [[ -f file_temp1 ]]
then
rm file_temp1
fi
 
if [[ -f file_temp ]]
then
rm file_temp
fi
 
echo "enter the number of dash you want"
read v
nine=9
if [[ $v -ge $nine ]]
then

b=`awk -F" " 'NR==1 {print$1}' check_OFS_and_variable`
a=`awk -F" " 'NR==2 {print$1}' check_OFS_and_variable`
e=`echo $b | wc -c`
f=`echo $a | wc -c`
##echo $e
let "t = v + e"
let "p = t - f"
 
##echo $p
OFS=`echo "-"`
i=0
value1_1=`awk -F" " 'NR==1 {print$1}' check_OFS_and_variable`
value1_2=`awk -F" " 'NR==1 {print$2}' check_OFS_and_variable`
echo $value1_1
value2_1=`awk -F" " 'NR==2 {print$1}' check_OFS_and_variable`
value2_2=`awk -F" " 'NR==2 {print$2}' check_OFS_and_variable`
echo $value1_1 >> file_temp
while [[ $i -le $v ]]
do
echo $OFS >> file_temp
let "i = i +1"
done
echo $value1_2 >> file_temp
i=0
echo $value2_1
echo $value2_1 >> file_temp1
while [[ $i -le $p ]]
do
echo $OFS >> file_temp1
let "i = i + 1"
done
echo $value2_2 >> file_temp1
 
awk '{print $0}' ORS="" file_temp
echo "\n"
awk '{print $0}' ORS="" file_temp1
 
else
 
echo "print enter more than 9 times."
fi

Output will be as follows.

ome----------------data
here_is_more-------information

Not sure it is not coming properly as it comes in box. Please try and let us know.

EDITED Text: sorry just now realized that script will work if user have entered number more than 8 so changed accordingly. :frowning:

Thanks,
R. Singh