LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-12-2011, 12:15 AM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431
Blog Entries: 31

Rep: Reputation: 3
remove specified line from text


hi, I wish to move a specified line from a text file:

Quote:
ltoremove=5 #remove 5th line
i=1
while read line ; do
if [ $i == $ltoremove ] ; then
...
i=$(($i+1))
done < "txt.txt"
how should this actually be done? is there a shorter faster way to do this?
Thanks,
Ted

Last edited by ted_chou12; 02-12-2011 at 12:16 AM.
 
Old 02-12-2011, 12:25 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You can do it with sed, e.g.

$ sed '5d' file

which says to delete line 5 from file. This will print the output to the terminal (rather than changing the file), so if you want to actually make the change to the file, you need to use the -i option:

$ sed -i '5d' file

This is a tutorial for sed.
 
Old 02-12-2011, 12:39 AM   #3
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431

Original Poster
Blog Entries: 31

Rep: Reputation: 3
so the code should look something like? (in bash script)
Quote:
sed -i '5d' file << file.txt
Thanks
 
Old 02-12-2011, 12:47 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
No, it should be

sed -i '5d' file.txt,

assuming that file.txt is the file you want to operate on.

Last edited by Nylex; 02-12-2011 at 12:53 AM.
 
Old 02-12-2011, 12:57 AM   #5
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431

Original Poster
Blog Entries: 31

Rep: Reputation: 3
thank you
 
Old 02-12-2011, 01:24 AM   #6
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431

Original Poster
Blog Entries: 31

Rep: Reputation: 3
Sorry again,im doing another app
How would I change a specific line completely?
I read the manual and -c is what I am looking for:
Change a line with 'c'

You can change the current line with a new line.
so line 5 in the text file would be complete replaced with a new string line.

#!/bin/sh
sed '
/WORD/ c\
Replace the current line with the line
'
where do you put the line number and the string to?
Thanks
 
Old 02-12-2011, 01:42 AM   #7
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
If you just want to replace line 5 with some other text you can do it like this:

file.txt:
1
2
3
4
5

$ sed '5c\stuff' file.txt
1
2
3
4
stuff

So, the line number goes before the 'c' and the text you want to replace that line with goes after the '\'.

Does that help?
 
Old 02-12-2011, 03:34 AM   #8
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431

Original Poster
Blog Entries: 31

Rep: Reputation: 3
Quote:
Originally Posted by Nylex View Post
If you just want to replace line 5 with some other text you can do it like this:

file.txt:
1
2
3
4
5

$ sed '5c\stuff' file.txt
1
2
3
4
stuff

So, the line number goes before the 'c' and the text you want to replace that line with goes after the '\'.

Does that help?
thanks for your help!
 
  


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] Script to remove text strings before a regexp on every other line? kmkocot Programming 10 07-12-2010 11:58 PM
[SOLVED] sed or awk help - need to remove text on each line before a regular expression kmkocot Linux - Newbie 15 10-30-2009 03:20 AM
Remove New Line or Carriage return from Text File DIMonS Programming 13 10-01-2007 10:31 AM
Remove specific text in output line dwarf007 Linux - General 8 07-04-2007 11:28 AM
How to remove line of text from file netkepala Linux - General 2 05-23-2003 11:49 AM

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

All times are GMT -5. The time now is 12:52 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