It is currently Tue, 07 Dec 2021 03:24:58 GMT



 
Author Message
 SED - I need to insert hard returns into a file
Hi,
    I have a file that I am trying to manipulate so that I can
insert hard returns after certain fields. Can anyone help
a poor beginner who is scrtaching his head. I know there
is a N option with sed, but I cannot for the life of me
work out the correct syntax for this to work. Any other
suggestions greatfully recieved.

best reagards

novice sed programmer



 Thu, 25 Apr 2002 03:00:00 GMT   
 SED - I need to insert hard returns into a file
d  <a...@shram87.freeserve.co.uk> ha scritto:

I'm a sed novice too, but all you have to do is include the newline
in the replacement part.  A newline is made by making sure that sed
sees a '\' escape character followed by a newline character.

pattern="something that describes a field"
sed "s:$pattern:&\\
:g"

or to demonstrate a different way of quoting:

sed 's:'"$pattern"':&\
:g'

To avoid (shell) quoting, use sed -f.  Make a file that contains:

----- cut here ----
s:something that describes a field:&\
:g
----- and here ----

and use it with the -f option to sed.

Regards,

Craig



 Thu, 25 Apr 2002 03:00:00 GMT   
 SED - I need to insert hard returns into a file

                join sed-users mailing list (majordomo)

--
=-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
al aab, ex seders moderator                                   sed u soon
               it is not zat we do not see the  s o l u t i o n          
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+



 Fri, 26 Apr 2002 03:00:00 GMT   
 SED - I need to insert hard returns into a file
  "d" <a...@shram87.freeserve.co.uk> wrote:

If you're trying to do what I think you are, I got awk to do it better.

VAR=`cat $HOME/file | awk '/[a-z]/{printf ("%s\\\n",$0)}'`

The above is an example of preserving hard returns within a string
variable.  In this case, it inserts a hard return after each line in the
file.  You can, of course, add hard returns after fields in the string.
The "/a-z/" filter was something I added to ensure that a letter is in
the line of data...mostly to eliminate the trailing line feed that "cat"
creates. You can remove it if you like, but I'd recommend using the "-x"
on your shell definition to watch the differences in the output before
putting it into production.

Noal
--
"Sure, it's every American's right to post false or misleading
information on the Internet."
-The Detroit News

Sent via Deja.com http://www.deja.com/
Before you buy.



 Fri, 26 Apr 2002 03:00:00 GMT   
 SED - I need to insert hard returns into a file
how do we join the list?
thanks


 Sat, 27 Apr 2002 03:00:00 GMT   
 SED - I need to insert hard returns into a file
     [courtesy cc of this posting mailed to cited author]

:    I have a file that I am trying to manipulate so that I can
:insert hard returns after certain fields. Can anyone help
:a poor beginner who is scrtaching his head. I know there
:is a N option with sed, but I cannot for the life of me
:work out the correct syntax for this to work. Any other
:suggestions greatfully recieved.

I have no earthly idea was a "hard return" is.  Is that any different from
an "easy return" or a "slow return" or an "IRC return" or a "guaranteed
return"?  What is the ASCII number for this so-called "hard" return?
Please choose from these:

    % man ascii
    ...

       Oct   Dec   Hex   Char
       ------------------------------
       000   0     00    NUL '\0'
       001   1     01    SOH
       002   2     02    STX
       003   3     03    ETX
       004   4     04    EOT
       005   5     05    ENQ
       006   6     06    ACK
       007   7     07    BEL '\a'
       010   8     08    BS  '\b'
       011   9     09    HT  '\t'
       012   10    0A    LF  '\n'
       013   11    0B    VT  '\v'
       014   12    0C    FF  '\f'
       015   13    0D    CR  '\r'
       016   14    0E    SO
       017   15    0F    SI
       020   16    10    DLE
       021   17    11    DC1
       022   18    12    DC2
       023   19    13    DC3
       024   20    14    DC4
       025   21    15    NAK
       026   22    16    SYN
       027   23    17    ETB
       030   24    18    CAN
       031   25    19    EM
       032   26    1A    SUB
       033   27    1B    ESC

I presume you mean either a carriage return (015) or a line-feed (012).
I also don't know what a "field" is in this context.  What separates
each field?  Variable amounts of white space?  A single tab?  A colon
or a slash?

If you are using white-space separated fields, you might try this:

    % awk '{ $5 = $5 "\n"; print }' < input > output

Which converts this:

    FIRST SECOND THIRD FOURTH FIFTH SIXTH SEVENTH EIGHTH NINTH TENTH

Into this:

    FIRST SECOND THIRD FOURTH FIFTH
     SIXTH SEVENTH EIGHTH NINTH TENTH

Yes, you get an extra blank at the front.

If you have a colon-separated set of records, and you wish the fifth
field to have a newline in it.

    % awk -F: 'BEGIN { OFS = ":"} { $5 = $5 "\n"; print }' < input > output

But this takes something like this:

    FIRST:SECOND:THIRD:FOUTH:FIFTH:SIXTH:SEVENTH:EIGHTH:NINTH:TENTH

And creates something like this:

    FIRST:SECOND:THIRD:FOUTH:FIFTH
    :SIXTH:SEVENTH:EIGHTH:NINTH:TENTH

That's probabably not want you want, since now you've got a leading null
field in the new record.

I suspect that you'll find that sed is probably not your friend in this,
but that awk or perl should be fine.  I just don't really know what
you're trying to do.

For example:

    % perl -ple 's/^((\S+\s+){5})/$1\n/' < input > output

will end up producing this:

    FIRST SECOND THIRD FOURTH FIFTH
    SIXTH SEVENTH EIGHTH NINTH TENTH

You can't see it, but there's now a trailing blank at the end behind
the word FIFTH.  A more elaborate approach:

    % perl -ple 's/^((\S+\s+){4})(\S+)\s+/$1$3\n/'  < input > output

    first second third fourth fifth
    sixth seventh eighth ninth tenth

wit neither a leading nor a trailing blank.

--tom
--
"I have never understood why it should be necessary to become irrational in
 order to prove that you care.  Or indeed why it should be necessary to prove
 it at all."   - Avon to Vila, `Blake's 7'



 Sat, 27 Apr 2002 03:00:00 GMT   
 SED - I need to insert hard returns into a file

Quoting from Eric Pement's Sed FAQ
(http://www.cornerstonemag.com/sed/sedfaq.html):

+ 2.3.2. Mailing list
+
+    The informal "seders" mailing list has changed to a Majordomo
+    mailing list called "sed-users". Regular and digest versions are
+    available. Average mail volume is 12-25 messages per week. For more
+    information, address mail to "majord...@jpusa.chi.il.us" with any
+    subject line and the following in the message body:
+
+       info sed-users yourn...@your.site
+
+    To subscribe, mail to "majord...@jpusa.chi.il.us" with any subject
+    line and one of the following in the message body:
+
+       subscribe sed-users yourn...@your.site
+       subscribe sed-users-digest yourn...@your.site

                --Ken Pizzini



 Sun, 28 Apr 2002 03:00:00 GMT   
 
   [ 7 post ] 

Similar Threads

1. Need To Insert Carriage Return

2. How to insert Carriage Return in a file ?

3. Insert escape sequence in file : SED ?

4. sed insert into empty file

5. insert file in sed replace command

6. Sed, insert a file current line

7. sed to insert lines in a file.

8. use sed to insert system date to a file

9. Sed question (How to insert text into all the files)

10. grep files in dir and SED - insert


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software