Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
Here are 3 additional methods, using different data, with ed, ex, sed. As usual with sed, you need to arrange for the output to replace the original, either through a direct copy yourself, or use GNU sed option "-i". In either case be careful that your solution is working correctly before replacing the original data:
Code:
#!/usr/bin/env sh
# @(#) s3 Demonstrate match and delete with sed.
set -o nounset
echo
## Use local command version for the commands in this demonstration.
version && version bash ed ex
cat >data0 <<'EOF'
a
first follow-on
b
1b
c
1c
a
second follow-on
b
2b
EOF
echo
echo " Input file:"
cp data0 data1
cat data1
# Search for pattern "a" at beginning of line, then delete it and
# the line following it.
echo
echo " Conversation with ed:"
ed data1 <<'EOF'
g/^a/d\
d
w
q
EOF
echo
echo " Results after ed delete:"
cat data1
cp data0 data1
ex -V1 data1 <<'EOF'
g/^a/,+d
w
q
EOF
echo
echo " Results after ex delete:"
cat data1
cp data0 data1
echo
echo " Results after sed delete:"
sed -e '/^a/,+1d' data1
exit 0
Producing:
Code:
% ./s3
GNU bash 2.05b.0
GNU ed version 0.2
VIM - Vi IMproved 6.3 (2004 June 7, compiled Apr 2 2005 17:44:56)
Input file:
a
first follow-on
b
1b
c
1c
a
second follow-on
b
2b
Conversation with ed:
52
15
Results after ed delete:
b
1b
c
1c
b
2b
Results after ex delete:
b
1b
c
1c
b
2b
Results after sed delete:
b
1b
c
1c
b
2b
Working on UNIX shell scripting I found in env file the following
export H1="\n- "
echo "${H1} Waiting for dependencies of ${MONITOR_KEY} to be satisfied ..." >> ${LOG}
What is in shell
\n-
The new line character?
Thanks for contribution (2 Replies)
Dear Ladies & Gents,
I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out:
for filename in $(find /var/log/test... (2 Replies)
Hi,
I have line in input file as below:
3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL
My expected output for line in the file must be :
"1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL"
Can someone... (7 Replies)
I wrote a script to delete files which are older than "x" days, if the size of the directory is greater than "y"
#!/bin/bash
du -hs $1
while read SIZE ENTRY
do
if ;
then
find $1 -mtime +$2 -exec rm -f {} \;
echo "Files older than $2 days deleted"
else
echo "free Space available"... (4 Replies)
im a new student in programming and im stuck on this question so please please HELP ME. thanks.
the question is this:
enter a command to delete all files that have filenames starting with labtest, except labtest itself (delete all files startign with 'labtest' followed by one or more... (2 Replies)
Hi
I have installed solaris 10 on an intel machine. Logged in as root. In CDE, i open terminal session, type login alex (normal user account) and password and i get this message
No utpmx entry: you must exec "login" from lowest level "shell" :confused:
What i want is: open various... (0 Replies)