sed to delete selected line from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to delete selected line from file
# 1  
Old 02-14-2013
sed to delete selected line from file

Ultimate goal is to delete lines from before and after a block of lines in a given file. First attempt was something like this:
Code:
sed -e '1,/STARTUP/ d' inputfile.txt     > outputfile.txt

but that deleted everything down to and including the line with STARTUP. I need to delete everything before that line.

next attempt was
Code:
 
lines=`grep -m 1 -n "STARTUP" inputfile.txt  |  awk -F: '{print $1}'`
fline=`expr $lines + 1`
sed -e '1,${fline} d'   inputfile.txt  > outputfile.txt

But that appears to create 'fline' with spaces before and after the returned line number, which then causes sed to choke:
Code:
estevens:work$ cat fixit
set -x
lines=`grep -m 1 -n "STARTUP" inputfile.txt | awk -F: '{print $1}'`
fline=`expr $lines + 1`
echo ":" $fline ":"
sed -e '1,${fline} d' inputfile.txt > outputfile.txt

estevens:work$ ./fixit
+++ grep -m 1 -n STARTUP inputfile.txt
+++ awk -F: '{print $1}'
++ lines=51
+++ expr 51 + 1
++ fline=52
++ echo : 52 :
: 52 :
++ sed -e '1,${fline} d' inputfile.txt
sed: -e expression #1, char 5: unknown command: `f'

estevens:work$

# 2  
Old 02-14-2013
Here is an example how you can delete everything before pattern: STARTUP
Code:
awk '/STARTUP/{i++}i' inputfile.txt

# 3  
Old 02-14-2013
Quote:
Originally Posted by bipinajith
Here is an example how you can delete everything before pattern: STARTUP
Code:
awk '/STARTUP/{i++}i' inputfile.txt

That works for the specific case (still trying to get my head around HOW Smilie) but I also need to delete everything *following* another string. I only presented the once case because with the approach I was taking, when I had a fix I could use the same technique on the other end.

So I guess I have two goals:
1) how to delete all lines prior to a given line, and all lines following a given line
2) understand why my own code isn't working, so that I can increase my generalize knowledge.
# 4  
Old 02-14-2013
Quote:
Originally Posted by edstevens
1) how to delete all lines prior to a given line, and all lines following a given line
Code:
awk '/STARTUP/{i++}/END/{print;i=0}i' file

Quote:
Originally Posted by edstevens
2) understand why my own code isn't working, so that I can increase my generalize knowledge.
Please see the corrections:
Code:
lines=`grep -m 1 -n "STARTUP" inputfile.txt | awk -F: '{print $1}'`
fline=`expr $lines - 1`
echo ":" $fline ":"
sed -e "1,${fline}d" inputfile.txt

This User Gave Thanks to Yoda For This Post:
# 5  
Old 02-14-2013
Quote:
Originally Posted by bipinajith
Code:
awk '/STARTUP/{i++}/END/{print;i=0}i' file

Please see the corrections:
Code:
lines=`grep -m 1 -n "STARTUP" inputfile.txt | awk -F: '{print $1}'`
fline=`expr $lines - 1`
echo ":" $fline ":"
sed -e "1,${fline}d" inputfile.txt


A little more tweaking, but that did it. Can you explain why the change from single- to double-quote to enclose the sed commands? ( I had to add back the space separating the 'd' (delete) from the range, but that was no big deal).
# 6  
Old 02-14-2013
Quote:
Originally Posted by edstevens
Can you explain why the change from single- to double-quote to enclose the sed commands?
Since you are using the variable: ${fline} inside sed you have to use double-quotes to expand this variable.

Single-quotes prevents variable expansion.
# 7  
Old 02-14-2013
Code:
sed -i '/STARTUP/,$!d' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete line from file using sed command?

hello Team, I want to delete below linux using sed command but I am getting error.sed -i '/url=/status.cgi?hostgroup=/d' 3 error:sed: -e expression #1, char 32: unknown option to `s' Could you please help me with correct syntax. My line contain / character because of that I am getting... (4 Replies)
Discussion started by: ghpradeep
4 Replies

2. Shell Programming and Scripting

How to grep/sed selected data from a command or file?

Below is the output of a DB2 command. Now I have 2 requirements... Database Partition 0 -- Database TESTDB1 -- Active Standby -- Up 213 days 02:33:07 -- Date 02/22/2016 17:04:50 HADR Information: Role State SyncMode HeartBeatsMissed LogGapRunAvg (bytes) Standby ... (2 Replies)
Discussion started by: rlokesh27
2 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. UNIX for Dummies Questions & Answers

using sed delete a line from csv file based on specific data in two separate fields

Hello, :wall: I have a 12 column csv file. I wish to delete the entire line if column 7 = hello and column 12 = goodbye. I have tried everything that I can find in all of my ref books. I know this does not work /^*,*,*,*,*,*,"hello",*,*,*,*,"goodbye"/d Any ideas? Thanks Please... (2 Replies)
Discussion started by: Chris Eagleson
2 Replies

5. Shell Programming and Scripting

delete the line with a particular string in a file using sed command.

Hello, I want to delete all lines with given string in file1 and the string val is dynamic. Can this be done using sed command. Sample: vars="test twinning yellow" for i in $vars do grep $i file1 if then echo "Do Nothing" else sed `/$i/d` file1 fi done Using the above... (5 Replies)
Discussion started by: PrasadAruna
5 Replies

6. Shell Programming and Scripting

Delete a line between selected lines using sed or any other command

I want to delete a line between selected lines using sed: e.g. : Between "bus" to "pins", delete lines conaining "signal" word. Input : bus direction signal new signal old pins signal ok end Desired Output: bus direction pins signal end (4 Replies)
Discussion started by: nehashine
4 Replies

7. Shell Programming and Scripting

How to delete selected string from a file?

awk '!(/^$/||/--/||/selected/||/^ *$/){print "A." $1 " <> B." $1 " or"}' infile my AWK out put is : A.KZ <> B.KZ or A.KZT <> B.KZT or A.KZ_Z <> B.KZ_Z or A.LH <> B.LH or A.MAN<> B.MAN or A.OBJEKT <> B.OBJECT or A.PAK <> B.PAK ; is there any way to controle AWK to not print the... (1 Reply)
Discussion started by: kanakaraju
1 Replies

8. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

9. Shell Programming and Scripting

SED: Place char at starting and replace selected line

Hello Experts, I am working on a small file editing script. Since all experts here are very generous to give me the complete code, I would take up the problem in steps so that I ensure my opportunity to learn. AIM: The script has some commented and some uncommented lines. I need to : ... (2 Replies)
Discussion started by: hkansal
2 Replies

10. Shell Programming and Scripting

delete line in file with sed

sed "/$titlesearch/d" movielist will delete any line in the file movielist that matches $titlesearch, but this is only a screen print correct ? how can I have sed to actually delete the line from the file so that it doesnt appear next time I open the file ? thanks Steffen (8 Replies)
Discussion started by: forever_49ers
8 Replies
Login or Register to Ask a Question