LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 09-15-2013, 02:15 AM   #1
waddles
Member
 
Registered: Sep 2012
Posts: 373

Rep: Reputation: 0
Removing line from executing script


Developing a script which leaves data in the script which runs the command to remove and write a line.
Done this to scripts b4 but am drawing a blank on removal and get an error.
Code:
#!/bin/sh
num=`echo $lin | cut -d":" -f1`
echo line number is: $num
sed '${num}d' $0
##data	234	345	456
Shell returns error: sed: -e expression #1, char 4: extra characters after command
Couldn't find any examples with my search query so needing help.
BTW have use echo statements to validate variables and output.

Last edited by waddles; 09-15-2013 at 02:17 AM.
 
Old 09-15-2013, 02:23 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
You are using single quotes instead of double quotes which prevent the shell from expanding the variable:
Code:
sed '${num}d' $0

# should be:

sed "${num}d" $0
 
Old 09-15-2013, 11:45 PM   #3
waddles
Member
 
Registered: Sep 2012
Posts: 373

Original Poster
Rep: Reputation: 0
Regrets for not listing the entire script correctly here is with echo statements:
Code:
#!/bin/sh
lin=`egrep -n "^##data" $0`
echo $lin
num=`echo $lin | cut -d":" -f1`
echo line number is: $num
sed "${num}d" $0 > $0
##data	234	345	456
Yes this works but the entire script is lost. Need to keep script with the ##data statement removed as will write a new ##data statement into the script's tail, i.e. keeping ##data info with the program that updates it.
 
Old 09-16-2013, 01:17 AM   #4
saulgoode
Member
 
Registered: May 2007
Distribution: Slackware
Posts: 243

Rep: Reputation: 112Reputation: 112
Code:
sed "${num}d" $0 > $0  #<<-- FAIL!
The stdout redirection will result in $0 being opened as a new file before the command is executed. By the time 'sed' gets executed and accesses the file, it is empty.
 
Old 09-16-2013, 01:26 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Elaborating on saulgoode's answer (which is correct):

If you use a modern sed version (4+) you can use the -i flag to edit the file in-place:
Code:
sed -i "${num}d" $0
If you're using an older sed version:
Code:
sed "${num}d" $0 > /tmp/${0}.tmp
mv /tmp/${0}.tmp $0
 
1 members found this post helpful.
Old 09-16-2013, 01:45 AM   #6
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,950

Rep: Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308
that can be solved with a single sed:
sed -i '/^##data/d' <filename>
if I understand it well
 
2 members found this post helpful.
Old 09-16-2013, 03:18 AM   #7
waddles
Member
 
Registered: Sep 2012
Posts: 373

Original Poster
Rep: Reputation: 0
druuna & pan64 U have it right! Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Removing a line from file which is having specific pattern using shell script emcykm Linux - Newbie 5 03-21-2011 05:49 AM
Removing new line characters on every line execpt first line bioinformatics_guy Linux - Newbie 4 10-21-2008 12:41 PM
Removing beginning of line with a shell script oscarlevin Programming 9 05-07-2007 01:18 AM
shell script question: removing a line according to a duplicate "field" mattie_linux Linux - Newbie 1 01-12-2006 03:57 PM
executing from command line nodger Linux - Newbie 3 01-20-2004 10:30 AM


All times are GMT -5. The time now is 11:20 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration