user3290639
user3290639

Reputation: 19

I'm having problems creating a sed script

My teacher didn't really go over sed scripts so they're very confusing. I need to finish this to get an A though. It's due very soon so I doubt I have time to fully understand it because I don't understand the syntax at all.

The instructions are: Create a sed script named script3 that will print a file with “The Raven” at the top, replace every occurrence of multiple spaces with a single space, and print a line of 30 dashes below each line.

This is what I have so far:

sed script

echo "The Raven"
s/[ ]\{2,\}/ /g
/\,\./ s/^/------------------------------ /

Upvotes: 0

Views: 99

Answers (2)

NeronLeVelu
NeronLeVelu

Reputation: 10039

enter code hereCreate a sed script named script3

so create a text file calles script3 taht is called with sed -f script3 YourSoureFile content is

1 i\
The raven
s/ \{2,\}/ /g
a\
------------------------------

that will print a file with

“The Raven” at the top

1 i\
The raven

on 1st (1) line, insert (i) the next line (each line following until last cahr is NO NORE \

replace every occurrence of multiple spaces with a single space

s/ \{2,\}/ /g

substitute (s///) any pattern (part of text between the separator /composed of 2 or more \{2,\} following space jsute before the occurance specification, by a singel space the second , any occurence present ( option g). This happend at each line (no number or filter pattern in the head of the line like the 1 on precedent line)

and print a line of 30 dashes below each line

a\
------------------------------

(a) work like the (i) but append in this case. No filter pattern nor number indicate line to act, so on each line also.

i and a add text to ouptut but not on working buffer, so this cannot be manipulate inside this sed action, this is not the case of s/// that work ON the current working buffer (that is print at end of treatment of the line, before starting with a new line) This is what I have so far:

About your script

echo "The Raven"

that is not a sed action but a shell action so, it cannot work in a sed like this (could using shell substitution but not the goal normaly)

s/[ ]\{2,\}/ /g

it's fine, class [] for this space is not necessary here but litterally ok. You could use [[:space:]] or [[:blank:]] or [[:space:][:blank:]] to be exhaustif using meta class of type [:xxxxx:] inside a class

/\,\./ s/^/------------------------------ /

this will mean *on each occurence of ,. (both and in this order), replace (add thus) the start of the line with 30 - and a space. So it's not occuring on each line and it add at the start not following the line. A way using s/// could be:

s/.*/&\
------------------------------/

Replace on each line every char (all at once) by herself (&) followed by a new line and 30 dash

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246807

Using the online GNU sed manual:

  • use the i command with an address to insert the title
  • use the a command with no address to append the separator after every line
  • use s/[[:blank:]]\+/ /g to replace any horizontal whitespace characters with a single space: sed regular expressions

Upvotes: 3

Related Questions