7 Replies - 3586 Views - Last Post: 19 April 2009 - 08:22 AM Rate Topic: -----

#1 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Delete Specific Line Unix Turbo C Shell

Posted 17 April 2009 - 06:20 PM

Hey there!
I have a file with many lines, a loop that goes each line and a counter.
I want to delete a specific line after an if statement.So the number of the line is in the counter.But I dont know how to write it.I tried something like this :

sed "$counter" d phones



counter is the variable
d is the parameter for sed in order to delete
phones is the file


Any tip appreciated. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Delete Specific Line Unix Turbo C Shell

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Delete Specific Line Unix Turbo C Shell

Posted 18 April 2009 - 05:26 AM

I think this is closer to right.
sed -e '"$counter"d' phones

Read this:
http://www.panix.com...d/unix/sed.html
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Delete Specific Line Unix Turbo C Shell

Posted 18 April 2009 - 07:22 AM

This should work:
sed ${counter}d filename



The above is the syntax for explicitly calling a variable name with defined ends. There are a few was to do this, but that one is good to know. Particularly since quotes can have unexpected meaning on the command line.
Was This Post Helpful? 0
  • +
  • -

#4 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: Delete Specific Line Unix Turbo C Shell

Posted 18 April 2009 - 10:41 AM

Well, none of the answers work. :blink:
Any other idea?
Should I post the whole script?Maybe I m doing something else wrong?

This post has been edited by AbuJaFaR: 18 April 2009 - 10:42 AM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Delete Specific Line Unix Turbo C Shell

Posted 18 April 2009 - 06:29 PM

View PostAbuJaFaR, on 18 Apr, 2009 - 11:41 AM, said:

Well, none of the answers work. :blink:


Odd. Here's a proof of concept:
#!/bin/sh

FILENAME=foo.txt

echo "Larry" > $FILENAME
echo "Moe" >> $FILENAME
echo "Curly" >> $FILENAME

echo "-- Complete File --"
cat $FILENAME

LINE=2

echo "-- File Sans Line $LINE--"

sed ${LINE}d $FILENAME

rm $FILENAME

echo "-- Done--"



Results:
-- Complete File --
Larry
Moe
Curly
-- File Sans Line 2--
Larry
Curly
-- Done--




View PostAbuJaFaR, on 18 Apr, 2009 - 11:41 AM, said:

Should I post the whole script?Maybe I m doing something else wrong?


That would help.
Was This Post Helpful? 0
  • +
  • -

#6 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: Delete Specific Line Unix Turbo C Shell

Posted 18 April 2009 - 07:06 PM

Im giving you the juicy part of the script cause it is a bit bigger.But I focus on this part because I think there is the mistake.The concept is that the 'new' file contains data in the form:
Lastname
Name
Phone
And the 'phones' file contains the same data but in the form :
Lastname^Name^Phone
So before the 'for' loop below I have done a grep with output in 'new' from 'phones' to replace the ^ symbol with the new line(\n).So, now I want to print the results,which I do with the echo $line, I check the counter mod with 3 so I can change user and ask to be deleted.
I think I explained the whole idea.Im trying to find the error like 3 days.And I dont know why is happening.
Okay enough talk from me.Here it is:


@ counter = 1
foreach line(`cat new`)
echo $line
if ($counter % 3 == 0) then
echo ""
echo "--------------------"
echo -n "Delete ? (y/n):"
set answer=$<
if($answer == 'y' || $answer == 'Y') then
sed ${counter}d phones   # it is supposed to delete the counter line here but it doesnt
echo -n "Delete done ..."
set done=$<
else
echo -n "Delete not done ..."
set notdone=$<
echo "--------------------"
endif
endif
@ counter ++
end



:)
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Delete Specific Line Unix Turbo C Shell

Posted 19 April 2009 - 01:58 AM

What is the script written in? It's not Bourne, korn, csh, perl, or awk.

I'm sure I missed a few, but it's just not coming to me. I can't think of any language that would be happy with a variable declaration of "@ counter = 1".
Was This Post Helpful? 0
  • +
  • -

#8 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: Delete Specific Line Unix Turbo C Shell

Posted 19 April 2009 - 08:22 AM

Well it is csh according to my professor.
The @ symbol is used to give variables a numerical value.If we want string he said to use the set command.
That's what Im taught from him :angry:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1