- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: shell script problem
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2003 12:45 AM
05-28-2003 12:45 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2003 01:08 AM
05-28-2003 01:08 AM
SolutionThis 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2003 01:13 AM
05-28-2003 01:13 AM
Re: shell script problem
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2003 01:28 AM
05-28-2003 01:28 AM
Re: shell script problem
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2003 01:39 AM
05-28-2003 01:39 AM
Re: shell script problem
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2003 07:51 AM
05-28-2003 07:51 AM
Re: shell script problem
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2003 04:27 PM
05-29-2003 04:27 PM
Re: shell script problem
perl -ne 'print "START\n" if $.%10==1; print; print "END\n" if $.%10==0;' < file