
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'