The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep Three Words murbina UNIX for Dummies Questions & Answers 12 08-14-2008 04:38 AM
Delete words in File 1 from File 2 Enobarbus37 Shell Programming and Scripting 14 02-15-2008 08:09 AM
Delete lines that contain 3 or more words? revax Shell Programming and Scripting 5 12-11-2007 06:33 PM
Get the words.. kakashi_jet Shell Programming and Scripting 10 07-31-2006 10:30 AM
sed option to delete two words within a file klannon UNIX for Dummies Questions & Answers 4 04-15-2002 08:25 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-25-2007
Orbix Orbix is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 34
Talking sed [delete everything between two words]

Hi,

I have the following codes below that aims to delete every words between two pattern word. Say I have the files
Quote:
$ cat file.txt
hello
start
WISH_LIST="
candy
money
t-shirt
"
stop
cool
Christmas
To delete every word between WISH_LIST=" and " I used the below codes (but its not working):

Code:
#!/bin/sh
sed '
/WISH_LIST=\"/ {
N
/\n.*\"/ {
s/WISH_LIST=\".*\n.*\"/WISH_LIST=\"\n\"/
}
}' file.txt
and also I have tried this code:
Code:
#!/bin/sh
sed '
/WISH_LIST=\"/ {
N
/WISH_LIST=\".*\"/ {
s/WISH_LIST=\".*\"/WISH_LIST=\"\n\"/
P
D
}
}' file.txt
Any Idea why I can't have the below output from the codes above:

Quote:
$ cat file.txt
hello
start
WISH_LIST="
"
stop
cool
Christmas
  #2 (permalink)  
Old 12-25-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,541
in shell
Code:
f=0
while read line 
do
    case $line in
    WISH_LIST* ) f=1; echo $line;;
    '"'* ) f=0;;
    esac
    if [ $f -eq 1 ];then        
        continue
    else
        echo $line
    fi

done < file
  #3 (permalink)  
Old 12-25-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 712
Hi.

With sed:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate delete lines of range exclusive.
# This solution based on code tutorial at:
# http://sed.sourceforge.net/sedfaq.html
# 2007.12.25

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash sed

echo

FILE=${1-data1}
echo " Input file $FILE:"
cat $FILE

echo
echo " Results from sed:"
sed '/WISH_LIST="/,/^"$/{
/WISH_LIST="/b
/^"$/b
d
}' $FILE

exit 0
Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
GNU sed version 4.1.2

 Input file data1:
hello
start
WISH_LIST="
candy
money
t-shirt
"
stop
cool
Christmas

 Results from sed:
hello
start
WISH_LIST="
"
stop
cool
Christmas
A tip of the hat to Eric Pement ... cheers, drl
  #4 (permalink)  
Old 12-25-2007
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,315
Or a solution with awk:

Code:
awk '/^WISH/{f=1;print} /^"$/{f=0} !f' file.txt
Regards
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0