Ask your questions here.
Post a reply
nadir

- nadir
- Posts: 1160
- Joined: Wed Mar 09, 2011 4:18 am
- Location: here

## sed

Tue Feb 19, 2013 9:59 am

I can't find the results i am looking for.
If i only want to print one line, by telling which line number i want,
how do i do that?
I found:
sed 16q filename.c
to print the first 16 lines, but i only want to see line 16, not 1-15.
If i want to replace a pattern:
sed -i 's/pattern/replace/' filename.c
how do replace only the occurrence in a certain line?
-
Any other sed tricks are welcome too.
fsmithred

- fsmithred
- Posts: 2101
- Joined: Wed Mar 09, 2011 9:13 pm

## Re: sed

Tue Feb 19, 2013 2:14 pm

Code:
  `# print section of file based on line numbers (lines 8-12, inclusive)
  sed -n '8,12p' # method 1
  sed '8,12!d' # method 2
  # print line number 52
  sed -n '52p' # method 1
  sed '52!d' # method 2
  sed '52q;d' # method 3, efficient on large files
  # beginning at line 3, print every 7th line
  gsed -n '3~7p' # GNU sed only
  sed -n '3,${p;n;n;n;n;n;n;}' # other seds`

I think that came from here - http://www.linuxhowtos.org/System/sedoneliner.htm
Can't find what you're looking for. If you can identify the line with a regex, then you could use

Code:
  `sed -i '/regex/ s/pattern/replace/'`

nadir

- nadir
- Posts: 1160
- Joined: Wed Mar 09, 2011 4:18 am
- Location: here

## Re: sed

Tue Feb 19, 2013 2:45 pm

sed -n '7p'
is perfect for me (-n sounds and seems familar).
The second answer (sed -i '/regex/ s/pattern/replace' ) i don't understand out of box, but it's been a while that i slept. Let me look and try later.
thanks.
and thanks for the link too. I don't plan to use a tutorial or how-to, only to get used to it by using it now and then
(on raspberry pi there is a difference if i use vim to open a file and replace the typo or if i use sed to do it. Else, on a PC or laptop, there has been no need to do so).
fsmithred

- fsmithred
- Posts: 2101
- Joined: Wed Mar 09, 2011 9:13 pm

## Re: sed

Tue Feb 19, 2013 3:01 pm

I did a test on a package list:

Code:
  `sed -i '/^gcc/ s/4.7/fourpointseven/' packagelist`

(Search for lines beginning with 'gcc' and replace '4.7' with 'fourpointseven')
And it edited one line, changing 'gcc-4.7-base' to 'gcc-fourpointseven-base'.
nadir

- nadir
- Posts: 1160
- Joined: Wed Mar 09, 2011 4:18 am
- Location: here

## Re: sed

Tue Feb 19, 2013 11:12 pm

Ok. Thanks.
the ^gcc means: gcc at beginning of line? Or does it mean something different for sed, and here?
Let me try to explain:
I am on low resources. That means that i can't easily copy and paste examples from the web, but have to type them. Doing so i produce quite a few typos.
I started to train gtk gui programming.
Most of the given variables i don't know, and most sound quite similar to me. Some are all upper case, some got the first word upper, the second upper, and the rest lower case, some are all lower, some come with underscore: GtkWidget, GTK_WINDOW, GdkEvent, mind the d, gtk_widget_show, gtk_window_new, etc. Showing it here they look very different, in a file it gets pretty confusing).
I compile and get an error including the line number.
If i go for a pattern i may well change more than i want, or even the wrong patter, and all over the file.
I think adding a line number would help to avoid the worst errors.
Did that make sense?
In short that doesn't make your answer useless, au contraire. Only that i think that i really would want to say: change only that line with number 9 (at least until i am more comfortable with sed).
If you got some sed tips you often do, and find useful, just shoot. I will look at the refracta scripts asap (and at the sed oneliners link you gave above)
fsmithred

- fsmithred
- Posts: 2101
- Joined: Wed Mar 09, 2011 9:13 pm

## Re: sed

Tue Feb 19, 2013 11:41 pm

Yes, ^gcc means that the line begins with gcc.
So, you're going to make these edits manually? And the compiler tells you what line to fix?
I wouldn't feel comfortable using sed in that situation. I want to see the file to make sure I'm not screwing up. I'll make edits with sed sometimes if I have a bunch of occurrances of the same thing to change.
nano +N
will open the file in nano, with the cursor on line N.
Oh, the same works in vim, apparently.
If you want to add line numbers to the file:
nl filename > filename.numbered
Removing them is not as simple. (Or maybe it is, and I just don't know the command.)
nadir

- nadir
- Posts: 1160
- Joined: Wed Mar 09, 2011 4:18 am
- Location: here

## Re: sed

Wed Feb 20, 2013 12:06 am

> I wouldn't feel comfortable using sed in that situation. I want to see the file to make sure I'm not screwing up.

That's why i stressed that i am on low resources.
vim can sometimes freeze when starting, for 2-3 seconds, same when closing and saving.
Altogether that can be five seconds more than doing it with sed.
It's not that i do it for the fun of it.
Look here:

Code:
  `$ gcc with-delete_event.c $(pkg-config gtk+-2.0 --cflags) $(pkg-config gtk+-2.0 --libs)
  with-delete_event.c:3:49: error: unknown type name ‘GtkEvent’
  `

So i now can check with sed what i got in line 3

Code:
  `$ sed -n '3p' with-delete_event.c
  static gboolean delete_event(GtkWidget *widget, GtkEvent *event, gpointer data);
  `

Aha, is see that there is Gtk twice, so i can't use:

Code:
  `sed -i 's/Gtk/Gdk/' filename.c`

(g doesn't help or hurt here)
but have to use

Code:
  `sed -i 's/GtkEvent/GdkEvent/' filename.c`

If i now could tell sed, in addition to the above: do it only in line 3,
then i avoid to have errors all over the file, but only in one line
(can now open vim and clean up).
Assuming i have messed it up.
Oh, and in vim you can do:
:set nu
:set nonu
or
:set number
:set nonumber
(you can also do that. But i barely do it. I just do " 4g" to go to line 4).
Ups: but you didn't mean that. You meant: vim +4. That is a good one, been looking for it for long (obviously not very close looking). Cool, but it doesn't help with the speed problem.
fsmithred

- fsmithred
- Posts: 2101
- Joined: Wed Mar 09, 2011 9:13 pm

## Re: sed

Wed Feb 20, 2013 1:20 pm

I'm visualizing a discarded computer on the curb near your home. Whatever part is broken in it is something you have lying around, or maybe the hardware is fine, and mswindows is broken. I think it's a core2 duo. Keep your eyes open for it.
nadir

- nadir
- Posts: 1160
- Joined: Wed Mar 09, 2011 4:18 am
- Location: here

## Re: sed

Thu Feb 21, 2013 2:34 pm

I support the idea. And i for one pick everything i can get. Hence i got 8 computers, assuming i am able to count.
But i really want to use the raspberry for such things (training to code). Mainly due to electricity costs. If that makes me learn about sed, then i call it a success (coding itself? I got doubts ...). As far it is me they can save the planet, but i don't fully understand why i have to pay for it.
nadir

- nadir
- Posts: 1160
- Joined: Wed Mar 09, 2011 4:18 am
- Location: here

## Re: sed

Fri Mar 08, 2013 12:21 am

To edit only one line number, say linenumber 3, one can use:

Code:
  `sed -i '3s/foo/bar/' test.txt
  `

I found
=
say:
sed = test.txt
to show the line numbers. That didn't go anywhere, and finally i found the solution from above (via miracle, not science).
sed is weird.
Post a reply
Jump to:

Powered by phpBB © phpBB Group.

phpBB Mobile / SEO by Artodia.