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 06-25-2008, 11:40 AM   #1
teiben
LQ Newbie
 
Registered: Jun 2008
Posts: 4

Rep: Reputation: 0
Specifying line number with sed


This is my first post, I'm taking my first Linux class and I'm stuck mid way through an assignment using sed. I need to "Capitalize the first t in line 5 and 11. (there are many t's in each line). Below is line 5

"'t'3hs s2m3 v'3hs'3ht~," K'3h4g G3~g3 th3 1st mutt3r3d, "t9pp'3h4g 9t my ch9mb3r d2~-


I've tried many combinations below, as searched high and low for the answer, it must be documented somewhere.

sed '5,5 s/t/T/' blue_bird_of_paradise.txt
sed s'5,5/t/T blue_bird_of_paradise.txt

any help would be greatly appreciated.
 
Old 06-25-2008, 12:19 PM   #2
ADxD_7
Member
 
Registered: May 2007
Location: CO
Distribution: Solaris , Redhat , Debian
Posts: 152

Rep: Reputation: 23
----------

Last edited by ADxD_7; 06-25-2008 at 12:21 PM. Reason: double post
 
Old 06-25-2008, 12:21 PM   #3
ADxD_7
Member
 
Registered: May 2007
Location: CO
Distribution: Solaris , Redhat , Debian
Posts: 152

Rep: Reputation: 23
This is how I would do it -


cat <filename> | sed -e '5s/t/T/' -e '11s/t/T/'

That will replace the t with T only in the 5th and 11th lines - and only the first instance of the "t"

And just so you know why it works - sed only works on the first instance of what you want to sub(or whatever) on the line you specify (unless you tell it otherwise)- so 5s says only work on the 5th line - then I tell it when you find the first "t" replace it with T - and so on with the 11th line - the -e separates the different sed commands to run - if you wanted to replace all the ts with Ts on lines 5 and 11 you could just add "g" to make it global at the end, like so:

cat <filename> | sed -e '5s/t/T/g' -e '11s/t/T/g'

I am sure that you could do it other ways as well - this is just the easiest IMHO

Regards

Last edited by ADxD_7; 06-25-2008 at 12:28 PM.
 
Old 06-26-2008, 07:25 AM   #4
teiben
LQ Newbie
 
Registered: Jun 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Huh? I suspect this is a joke? I don't see the command sed nor 5, nor t.
for(;P("\n"),R-;P("|"))for(e=C;e-;P("_"+(*u++/8)%2))P("|"+(*u/4) %2);

ADxD_7
I tried the code but got the message: -bash: syntax error near unexpected token `|'
Thanks for the explanation, I guess I read the syntax wrong that default it only replaces the first instance of it. So with out the g (global) it only replaces the first instance;
 
Old 06-26-2008, 07:36 AM   #5
teiben
LQ Newbie
 
Registered: Jun 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Now I'm really confused. The first command I ran (below) actually works, it replaces all * with t.

sed 's/*/t/g' blue_bird_of_paradise.txt

I shorted the syntax above and when I run it (or any other command) in an effort to Capitalize the first t in lines 5 and 11, it UNDOES the first command, the t's go back to *

sed -e '5s/t/T/g' blue_bird_of_paradise.txt

help
 
Old 06-26-2008, 06:56 PM   #6
ADxD_7
Member
 
Registered: May 2007
Location: CO
Distribution: Solaris , Redhat , Debian
Posts: 152

Rep: Reputation: 23
I gave you the sed command in the post -

cat <filename> | sed -e '5s/t/T/' -e '11s/t/T/'

You do not want to use the /g in this case - forget it - I was just trying to explain it to you



the "|" is a pipe - you have to pipe the file with cat to sed (well you dont have to but I like to) - just replace the <filename> with the text in the file - I even made a file with your exact line and put it at line 5 and 11 - works perfect - only caps the first t in each line


You are on a linux box right?

O and I see your problem - sed only by default outputs to stdout - it does not save what it did in the file - you have to pipe it too a new file like so

cat blue_bird_of_paradise.txt | sed -e '5s/t/T/' -e '11s/t/T/' > blue_bird_of_paradise.txt.new (or whatever)

If you dont you will always be working on the same file -

And do not copy and paste into your terminal my commands it might not get the characters right - hence the


-bash: syntax error near unexpected token `|'

its because it made one of my single quotes a tick - yes thier is a differnce - if you put this in here by hand

cat blue_bird_of_paradise.txt | sed -e '5s/t/T/' -e '11s/t/T/' > blue_bird_of_paradise.txt.new

With only single quotes and the spaces just like I have it - your assignment will be in blue_bird_of_paradise.txt.new -

Last edited by ADxD_7; 06-26-2008 at 07:14 PM.
 
Old 06-26-2008, 07:19 PM   #7
ADxD_7
Member
 
Registered: May 2007
Location: CO
Distribution: Solaris , Redhat , Debian
Posts: 152

Rep: Reputation: 23
Quote:
Originally Posted by teiben View Post
Huh? I suspect this is a joke? I don't see the command sed nor 5, nor t.
for(;P("\n"),R-;P("|"))for(e=C;e-;P("_"+(*u++/8)%2))P("|"+(*u/4) %2);
O and no offence but this made me LOL big time
 
Old 06-27-2008, 05:34 AM   #8
teiben
LQ Newbie
 
Registered: Jun 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Wink

Now I get it, duh, why the changes aren't being saved. Guess I was thinking that it would stay in memory.

I'm using a proxy to use the school's (Putty) Unix box actually.


O and no offence but this made me LOL big time - please explain.

Thank you very much for your time & effort.
 
Old 06-27-2008, 05:54 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by ADxD_7 View Post
O and I see your problem - sed only by default outputs to stdout - it does not save what it did in the file
Unless you use the -i option to edit the file in place! Another issue (just a detail, indeed): you can run two sed commands without using two -e options. Just use the semi-colon as separator, as in
Code:
sed '5s/t/T/; 11s/t/T/' blue_bird_of_paradise.txt
 
  


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
SED - Delete line above or below as well as matching line... OldGaf Programming 7 06-26-2008 11:51 PM
Pass a variable as a line number in sed dx0r515t Programming 7 03-30-2007 04:55 PM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
sed: replace one line with >one line bbeers Programming 3 11-19-2002 05:27 PM


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