LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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: 372

Rep: Reputation: 1
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: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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: 372

Original Poster
Rep: Reputation: 1
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: 288

Rep: Reputation: 155Reputation: 155
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: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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: 372

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


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 05:28 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration