Need help in shell scripting "delete"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in shell scripting "delete"
# 1  
Old 10-24-2007
Need help in shell scripting "delete"

I would like to delete a complete line which have particular word., and along with next line

For Eg:

disk 491 5/0/1/1/0.116.23.19.0.1.7 sdisk CLAIMED DEVICE
/dev/dsk/c107t1d7 /dev/rdsk/c107t1d7

I am able to delete complete line by matching that word "CLAIMED" by using this following command

(echo "g/${CLAIMED}/d"; echo 'wq') | ex -s hello
but i am not able to delete immediate next line

can somebody let me know how i can delete next line ???
# 2  
Old 10-24-2007
Is it possible to redirect the content to tmp file and mv the tmp file orig file

suggestion greatly appreciated ? Smilie
# 3  
Old 10-25-2007
sed

hi,
This one should be ok for you. Just try it. I tested it on solaris.
input:

Code:
disk 491 5/0/1/1/0.116.23.19.0.1.7 sdisk CLAIMED DEVICE
/dev/dsk/c107t1d7 /dev/rdsk/c107t1d7
12321321321
asdfsadklfj
disk 491 5/0/1/1/0.116.23.19.0.1.7 sdisk CLAIMED DEVICE
/dev/dsk/c107t1d7 /dev/rdsk/c107t1d7
disk 491 5/0/1/1/0.116.23.19.0.1.7 sdisk CLAIMED DEVICE
/dev/dsk/c107t1d7 /dev/rdsk/c107t1d7
asdfsdahj
2347891789
asdfjk

output:
Code:
12321321321
asdfsadklfj
asdfsdahj
2347891789
asdfjk

code:

Code:
sed '/CLAIMED/{
N
d
}' filename

# 4  
Old 10-25-2007
Thanks ,

it is working fine, thanks for your help Smilie
# 5  
Old 10-25-2007
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

See man pages for details ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In shell scripting found "\n-" and don't know what is it for

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)
Discussion started by: digioleg54
2 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

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)
Discussion started by: Hiroshi
2 Replies

3. Shell Programming and Scripting

Find "*.c" and "Makefile" and then delete them with one line

find "*.c" and "Makefile" and then delete them with one line (3 Replies)
Discussion started by: yanglei_fage
3 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

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)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Delete files older than "x" if directory size is greater than "y"

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)
Discussion started by: JamesCarter
4 Replies

6. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. Shell Programming and Scripting

Unix commands delete all files starting with "X" except "X" itself. HELP!!!!?

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)
Discussion started by: soccerball
2 Replies

10. UNIX for Dummies Questions & Answers

No utpmx entry: you must exec "login" from lowest level "shell"

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)
Discussion started by: peterpan
0 Replies
Login or Register to Ask a Question