LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-17-2015, 02:14 PM   #1
mhsahkir
LQ Newbie
 
Registered: Dec 2014
Location: U.S.A ,NEWYORK
Posts: 24

Rep: Reputation: Disabled
How to remove a line?


when i insert a line using command echo"" how can i remove that line? thanks advance.


[root@jakir ~]# echo "first inserting line" >file3
[root@jakir ~]# echo "first inserting line" >file3
[root@jakir ~]# cat file3
first inserting line
[root@jakir ~]# echo "first inserting line" >>file3
[root@jakir ~]# echo "first inserting line" >>file3
[root@jakir ~]# echo "first inserting line" >>file3
[root@jakir ~]# cat file3
first inserting line
first inserting line
first inserting line
first inserting line
[root@jakir ~]# rm -r "first inserting line"
rm: cannot remove `first inserting line': No such file or directory
[root@jakir ~]# ^C
[root@jakir ~]# vi file3
[root@jakir ~]# cat file3


first inserting line
first inserting line
 
Old 03-17-2015, 02:56 PM   #2
GNU/Linux
Member
 
Registered: Sep 2012
Distribution: Slackware-14
Posts: 118

Rep: Reputation: Disabled
First thing first, if you really want to mess around please do it using a normal user not as root.
Quote:
rm -r "first inserting line"
'rm -r' will delete directories recursively and that's very dangerous. rm takes filename or directory name as it's argument not contents of a text file. see 'man rm'


Now you need to know about redirection. Check out bash and sed, more specifically 'sed' for your line deletion task but it's really recommended that you get your hands dirty learning bash first.

Last edited by GNU/Linux; 03-17-2015 at 02:57 PM.
 
Old 03-17-2015, 05:00 PM   #3
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,541
Blog Entries: 23

Rep: Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943
Member response

Hi,

I too agree that you should use 'root' carefully. A newbie can do major damage with just one slip. Especially when you are using 'rm -r' on something. 'man rm';
Quote:
NAME

rm - remove files or directories SYNOPSIS

rm [OPTION]... FILE... DESCRIPTION

This manual page documents the GNU version of rm. rm removes each specified file. By default, it does not remove directories. If a file is unwritable, the standard input is a tty, and the -f or --force option is not given, rm prompts the user for whether to remove the file. If the response does not begin with `y' or `Y', the file is skipped.

OPTIONS


Remove (unlink) the FILE(s).
-d, --directory unlink FILE, even if it is a non-empty directory (super-user only)
-f, --force ignore nonexistent files, never prompt
-i, --interactive prompt before any removal
-r, -R, --recursive remove the contents of directories recursively
-v, --verbose explain what is being done
--help display this help and exit
--version output version information and exit
To remove a file whose name starts with a `-', for example `-foo', use one of these commands:
rm -- -foo
rm ./-foo Note that if you use rm to remove a file, it is usually possible to
recover the contents of that file. If you want more assurance that the
contents are truly unrecoverable, consider using shred.
You should get in the habit of using the 'man command'.

Quote:
Just a few links to aid you to gaining some understanding;

Hope this helps.
Have fun & enjoy!

Last edited by onebuck; 03-17-2015 at 05:31 PM. Reason: reformat quote
 
1 members found this post helpful.
Old 03-17-2015, 05:36 PM   #4
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
Something to play with and analyse in combination with man pages:

Code:
user@localhost:~/tmp$ echo line 1 >> testfile
user@localhost:~/tmp$ echo line 2 >> testfile
user@localhost:~/tmp$ echo line 3 >> testfile
user@localhost:~/tmp$ echo line 4 >> testfile
user@localhost:~/tmp$ echo line 5 >> testfile
user@localhost:~/tmp$ head -n -1 testfile 
line 1
line 2
line 3
line 4
user@localhost:~/tmp$ head -n -1 testfile > testfile_without_last_line
user@localhost:~/tmp$ cat testfile_without_last_line 
line 1
line 2
line 3
line 4
 
Old 03-18-2015, 06:27 PM   #5
mhsahkir
LQ Newbie
 
Registered: Dec 2014
Location: U.S.A ,NEWYORK
Posts: 24

Original Poster
Rep: Reputation: Disabled
ok thats cool idea,next time i will be work as regular user.
 
Old 03-19-2015, 08:02 AM   #6
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,253

Rep: Reputation: 559Reputation: 559Reputation: 559Reputation: 559Reputation: 559Reputation: 559
Use sed like so:
Code:
sed '2d' /path/to/file
Removes line 2
Or to remove a range of lines
Code:
sed '2,4d' /path/to/file
Removes lines 2-4 inclusive.
 
  


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
remove the line ust Linux - General 2 11-08-2013 10:21 PM
[SOLVED] Remove line break if next line does not start with "PX" joje47 Programming 8 05-25-2011 09:50 AM
[SOLVED] remove specified line from text ted_chou12 Linux - Newbie 7 02-12-2011 04:34 AM
script to remove line ust Linux - Server 2 07-02-2008 06:46 AM
remove the line munna_dude Programming 13 10-04-2007 04:23 AM


All times are GMT -5. The time now is 09:07 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration