Problem to add the string(without sed & awk) into the middle of file

Hi,
I have tried many times to add the string into the first line of the file or the middle of the file but could not find the solution.
I first tried by

$echo "paki" >> file

This code only append paki string at the end of file "file" but how can i add this "paki" into the first line or the midd of the file.

Thanks
kind regards,

Hi,

I have tried many times to add the string into the first line of the file f1 or the middle of the file f1. This file f1 already consists of 10 numbers of lines but could not find the solution.
I first tried by

$echo "paki" >> file

This code only append paki string at the end of file "file" but how can i add this "paki" into the first line or the midd of the file.

Thanks
kind regards,

To add in the beginning

awk '{ if ( NR == 1 ) { printf "%s\n%s\n", "somestring", $0 } else { print } }' file

To add in the middle,
based on the required line number,
change the code equating to NR

Add string to the first line

echo "paki" > tmp
cat f1 >> tmp
mv tmp f1

or

perl -i -ne 'if( $. == 1 ) { print "paki\n",$_; } else { print; } ' f1

Add string to the middle of the file

lines=$( wc -l <f1 )
half_lines=$( expr $lines / 2 )
head -$half_lines f1 > tmp
echo "paki" >> tmp
(( half_lines=half_lines+1 ))
tail -$half_lines f1 >> tmp
mv tmp f1

or

lines=$( wc -l <f1 )
half_lines=$( expr $lines / 2 )
perl -i -ne 'if( $. == '$half_lines' ) { print "paki\n"."$_";} else { print; } ' f1

Come on guys, we aren't supposed to be helping with homework. ali hussain, please stop posting homework questions.

ali hussain, this is a warning. Please stop posting homework questions and do not create multiple threads for the same question. Please follow the rules, or you risk getting banned from the site.