LinuxQuestions.org
Help answer threads with 0 replies.
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, 01:15 AM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 430
Blog Entries: 28

Rep: Reputation: 2
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 01:16 AM.
 
Old 02-12-2011, 01: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, 01:39 AM   #3
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 430
Blog Entries: 28

Original Poster
Rep: Reputation: 2
so the code should look something like? (in bash script)
Quote:
sed -i '5d' file << file.txt
Thanks
 
Old 02-12-2011, 01: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 01:53 AM.
 
Old 02-12-2011, 01:57 AM   #5
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 430
Blog Entries: 28

Original Poster
Rep: Reputation: 2
thank you
 
Old 02-12-2011, 02:24 AM   #6
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 430
Blog Entries: 28

Original Poster
Rep: Reputation: 2
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, 02: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, 04:34 AM   #8
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 430
Blog Entries: 28

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

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

All times are GMT -5. The time now is 01: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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration