LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 01:14 PM   #1
mhsahkir
LQ Newbie
 
Registered: Dec 2014
Location: U.S.A ,NEWYORK
Posts: 26

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, 01:56 PM   #2
GNU/Linux
Member
 
Registered: Sep 2012
Distribution: Slackware-14
Posts: 120

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 01:57 PM.
 
Old 03-17-2015, 04:00 PM   #3
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
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 04:31 PM. Reason: reformat quote
 
1 members found this post helpful.
Old 03-17-2015, 04:36 PM   #4
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
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, 05:27 PM   #5
mhsahkir
LQ Newbie
 
Registered: Dec 2014
Location: U.S.A ,NEWYORK
Posts: 26

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

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:29 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
Open Source Consulting | Domain Registration