1834771 Members
3195 Online
110070 Solutions
New Discussion

Re: shell script problem

 
SOLVED
Go to solution
Wiboon Tanakitpaisal_1
Occasional Advisor

shell script problem

Hi.

Anybody could be help ?

if I have text file as below

line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11
line 12
line 13
line 14
line 15
line 16
line 17
line 18
line 19
line 20
line 21
line 22

I want to insert messages between very 10 lines for example as below

start message
line 1
line 2
...
line 10
end message
start message
line 11
line 12
...
line 20
end message
start message
line 21
line 22
end message

How can I write shell script without redirect to file (read and insert in memory) ?

Many thanks
Wiboon.
6 REPLIES 6
Darren Prior
Honored Contributor
Solution

Re: shell script problem

Hi,

This shell script sends its' output to stdout not a file - is that what you mean?

#!/usr/bin/sh
i=0
while read LINE
do
echo $LINE
i=$((i+1))
if [[ i -eq 10 ]]
then
echo "end message"
echo "start message"
i=0
fi
done
regards,

Darren.
Calm down. It's only ones and zeros...
Leif Halvarsson_2
Honored Contributor

Re: shell script problem

Hi,
I think he mean editing the the file direct without using any temporary file. This is possible with perl or you have to write a C program.
H.Merijn Brand (procura
Honored Contributor

Re: shell script problem

l1:/tmp 107 > perl -pe'$.%10 or$_.="start msg\nend msg\n"' xx.txt
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
start msg
end msg
line 11
line 12
line 13
line 14
line 15
line 16
line 17
line 18
line 19
line 20
start msg
end msg
line 21

inline editing possible.

l1:/tmp 107 > perl -pi -e'$.%10 or$_.="start msg\nend msg\n"' xx.txt

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Massimo Bianchi
Honored Contributor

Re: shell script problem

sh-posix solution.

Put this in a file called test.sh
#########################

#!/sbin/sh
COUNT=1
while read TUTTO
do
if (( $COUNT == 1))
then
echo START MESSAGE
fi
echo $TUTTO
let COUNT=COUNT+1
if (( $COUNT == 11))
then
echo STOP MESSAGE
let COUNT=1
fi
done
if (( $COUNT != 1))
then
echo STOP MESSAGE
fi
#################



Once done:


cat yourfile.txt | test.sh


here you go.

Massimo
curt larson_1
Honored Contributor

Re: shell script problem

the best way would be to make your edits to a temporary file using sed, awk, perl, or the shell. Examine the changes for correctness then copy your changes to your original file.

now if you have to edit your file in place, your pretty much limited to using perl or the ex editor. Perl definitely has superior capabilities.

there already is a solution for perl, so i'll give you a couple using ex.

one using mulitiple edits

#!/usr/bin/ksh

line="end message
start message
."

x=$(wc -l textfile | awk '{print $1;exit;}')
x=$(( $x/10 ))

y=10
if (( $x > 1 )) ;then
ex -s textfile <<-EOF
${y}a
$line
wq
EOF

a=1
while (( $x > $a ))
do
y=$(( $y+12 ))
ex -s textfile <<-EOF
${y}a
$line
wq
EOF

a=$(( $a+1 ))
done
fi

or you can write the editor command to a tmp file and edit the file just once

#!/usr/bin/ksh

line="end message
start message
."

x=$(wc -l textfile | awk '{print $1;exit;}')
x=$(( $x/10 ))

if (( $x > 1 )) ;then
print "10a|$line" > tmpfile
a=1
while (( $x > $a ))
do
print ".+10a|$line" >> tmpfile
a=$(( $a+1 ))
done
ex -s textfile <<-EOF
so tmpfile
wq
EOF
fi
rm tmpfile

both do what you want, but I wouldn't call them good solutions
Jordan Bean
Honored Contributor

Re: shell script problem

With PERL?

perl -ne 'print "START\n" if $.%10==1; print; print "END\n" if $.%10==0;' < file