Adding a new column in a file with other existing columns

Hi All ,
Kindly help me with this soln

awk '{printf "%s %7s \n", $1,$c}' infile

where
value of variable c I am externally giving input
But executing the above command shows all the columns of infile where as I want only 1st column of infile and 2nd column should print value c

c="something"
awk   -v c="$c"  '{printf "%s %7s \n", $1,$c}' infile

-v $varname lets you pass shell environment variables to awk. One -v per variable usually.

Hi Jim ,

Appreciate your kind support and also thanks for the explanation.:slight_smile:

---------- Post updated 09-21-10 at 06:47 AM ---------- Previous update was 09-20-10 at 11:36 AM ----------

Hi Jim ,

While running the below command

nawk -v c="230031234567420987654321"  '{printf "%s %30s \n", $1,$c}'  infile

I am getting the following error

nawk: out of space in morefld

Please help me out

A typo of Jim, the command tries to print field number 230031234567420987654321 instead of the value, try this:

nawk -v c="230031234567420987654321" '{printf "%s %30s \n", $1, c}' infile

Hi Jim,

Thanks again
but I am getting two more rows with the variable .
What I want is to print the variable except from 1st and last row .
Can we achieve this using NR .

Please let me know

---------- Post updated at 07:28 AM ---------- Previous update was at 07:15 AM ----------

Hi Jim

I come up with something

nawk -v c="230031234567420987654321" 'NR >1 && NR <8{ printf "%s %30s \n", $1, c}'  infile

I want to print variable c only for row 2 to 7
But above command printing both the columns for 6 times
How i can separate the $1 value so it can print for all 8 rows

Hi,

This is not Jim :), as always, it's better to post the input file and the desired output.

Hi Franklin ,

Here is the 1st column of input file

12344455
12
13
14
15
15
16
12344455

and the variable value i am externally passing say 14

and desired o/p should be

12344455
12                     13
13                     13
14                     13
15                     13
15                     13
16                     13
12344455

point is 13 should not be printed for 1st and last row
and sorry for the addressing :slight_smile:

---------- Post updated at 12:45 PM ---------- Previous update was at 12:43 PM ----------

Hi ,

correction
variable value is 13

Try this:

awk -v var="13" '
NR==1{print;next}
s{print s, var}
{s=$0}
END{print s}' file

Or sed:

c=13
sed '1n;$n;s/$/ '"$c/" infile

or:

sed '1,$!s/$/ '"$c/" infile