0

I am trying to insert a pipe symbol in between nth line of a file. Like the one present in the output of diff command.

Do not want to use VI editor.

For example,desired line is 2nd line of the file:

cat filename

Hi            Hi
Hello         Hello
This          This
is            Is
it            it

desired output:

cat filename

Hi            Hi
Hello    |     Hello
This          This
is            Is
it            it

3 Answers3

1

You basically cannot modify in place some textual file by inserting a character inside a line. You need to read all its content, modify that content in memory, then write the new content.

You might be interested in GNU ed, which can edit a file programmatically (inside some script).

You could use awk (or any other filter) and redirect its output to some temporary file, then rename that temporary file as the original one.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

For your own sanity, just use awk:

$ awk 'NR==2{mid=length($0)/2; $0=substr($0,1,mid) "|" substr($0,mid+1)} 1' file
Hi            Hi
Hello    |     Hello
This          This
is            Is
it            it

To modify the original file you can add > tmp && mv tmp file or use -i inplace if you have GNU awk.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0
sed '
# select 2nd line
/2/ {
# keep original in memory
   G;h
:divide
# cycle by taking the 2 char at the egde of string (still string end by the \n here) and copy only first to other part of the separator (\n)
   s/\(.\)\(.*\)\(.\)\(\n.*\)/\2\4\1/
# still possible, do it again
   t divide
# add last single char if any and remove separator
   s/\(.*\)\n\(.*\)/\2\1/
# add original string (with a new line between)
   G
# take first half string and end of second string, and place a pipe in the middle in place of other char
   s/\(.*\)\n\1\(.*\)/\1|\2/
   }' YourFile
  • posix sed, so --POSIXfor GNU sed
  • Sel explain
NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43
  • For me, this just duplicated the input to the output until I changed `/2/` to `2` - then it inserted the `|` but it also added a blank line after the line it modified. Seems a tad complicated for such a conceptually simple task! – Ed Morton Jan 13 '15 at 19:53
  • I remember one of your post saying sed is a bit to(o) (h)old for lot of action missing lot of feature that other tools have (like awk), once again you got your point ;-). – NeronLeVelu Jan 14 '15 at 06:42
  • It's not that seds too old exactly, it's that before awk was invented if you had to do operations that spanned multiple lines, arithmetic, etc all you had was sed (or ed) so sed needed to have language constructs to facilitate that and people were happy to have them even though they were horrifically complicated and fragile. Once awk was invented in the mid-1970s, though, with the ability to handle records instead of single lines, and all of the other common very clear, natural constructs all of those crazy sed ones became obsolete so why some people still use them 40 years later is a mystery! – Ed Morton Jan 14 '15 at 13:09
  • 1
    To be clear though - I use sed almost every day, but just for what it's best at which is simple substitutions on single lines. – Ed Morton Jan 14 '15 at 13:11