String formatting using AWK

Hi,
I need to insert a line at a particular line number. I am using the below code:

sed $REV_LINO_NO" i\\
# $CURRENT_DATE $NAME           Changed pwd for cindy\'s id" file > file1

This code works, but the formatting is not as I expected. For example, I get lines as shown below according to the length of the variable '$NAME'

# 20100401 Sudha           Changed pwd for cindy's id
# 20100401 Sudhaaaaaa       Changed pwd for cindy's id
# 20100401 Su           Changed pwd for cindy's id

I have to format the line that I am inserting as shown below:

20100401 Sudha        Changed pwd for cindy's id
20100401 Sudhaaaaaa   Changed pwd for cindy's id
20100401 Su           Changed pwd for cindy's id
 
 

i.e I want the 4th feild, "Changed pwd for cindy's id" to start at a fixed place. I tried something with awk as shown below:

echo hi hellll hi | awk '{printf "# %s %s %30s\n", $1,$2,$3}'

but this does not work as I expect.

Your input will be of great help.

Thanks,
Sugan.

$
$
$ echo hi hellll hi | awk '{printf "# %s %s %30s\n", $1,$2,$3}'
# hi hellll                             hi
$
$

Well, what did you expect ?

tyler_durden

Not sure if awk allows you to print like that without typing all fields explicitly; maybe you could use the Bash shell:

$
$
$ cat f4
# 20100401 Sudha           Changed pwd for cindy's id
# 20100401 Sudhaaaaaa       Changed pwd for cindy's id
# 20100401 Su           Changed pwd for cindy's id
$
$ while read hsh num name txt
> do
>   printf "%s %s %-30s %-s\n" $hsh $num $name "$txt"
> done <f4
# 20100401 Sudha                          Changed pwd for cindy's id
# 20100401 Sudhaaaaaa                     Changed pwd for cindy's id
# 20100401 Su                             Changed pwd for cindy's id
$
$

tyler_durden

Great! It works. Thank you :slight_smile: