relle
1
Can anyone help me pls? I want to add a text into the middle of file.
I've writtenthe following script
text to add="$1"
file="$2"
lines=$(wc -l $2)
half_lines=$(expr $lines / 2)
head -$half_lines $2 > temp
echo "text to add" >> temp
((half_lines=$half_lines + 1))
tail -$half_lines $2 >> temp
mv temp $2
it gives me `expr : syntax error
Can someone tell me what i did wrong. Was i not to use expr command?
Try the wc command manually, it gives the number of lines and as second field the filename.
That's why you get an error with the expr command.
Regards
Try this script
[[ -z $1 ]]&& echo "Argument needed "&& exit
[[ ! -s $1 ]]&& echo "File not found "&& exit
rm -f $1New
var=`wc -l <$1`
echo "Enter line no :\c"
read lineno
echo "Enter Text:\c"
read text
head -$lineno $1 >$1New
echo "$text" >>$1New
var1=$((var-lineno))
tail -$var1 $1 >>$1New
relle
4
Hi Franklin
I have tried this but then i get an error command not found in line 5
Do you react on my response or on sanjaypraj reponse?
Regards
input:
first
second
forth
fifth
output:
first
second
third
forth
fifth
line=`cat a | wc -l`
half=`expr $line / 2`
nawk -v s="$half" '{
if(NR<=s)
print
}' a
echo "third"
nawk -v s="$half" '{
if(NR>s)
print
}' filename
How abt this
$cat test
first
second
fourth
fifth
$cat test | awk '{print $0}(NR==2){print "third"}'
first
second
third
fourth
fifth