-1

I have a text file that has lines of different lengths. I need to make these uniform so that the PLSQL Developers text import function reads them correctly. Lines that are 89 characters long need to be padded with 4 spaces on the end. For some reason the -i argument to sed isn't accepted either.

The file can be found here

I have tried a number of different regex commands found from various sources through Google but none of them are working, either because the 'function cannot be parsed' or it doesn't add the spaces needed.

The code that I wrote that worked using Notepad++ was
Find: (^.{89})($)
Replace: \1 \2

I've tried a number of unix sed commands such as
sed -e "s/(^.{89})($)/\1 \2/" file.txt
sed -e "s/(^.{89})($)/\1\s\s\s\s\2/" file.txt
sed -e "s/(^.{89})($)/\1\ \ \ \ \2/" file.txt
sed -e "s/\(^.\{89\}\)\($\)/\1\ \ \ \ \2" file.txt
sed -e 's/\(^.\{89\}\)\($\)/\1[[:space:]]\2/g' file.txt
sed -e 's/\(^.\{89\}\)\($\)/\1[[:space:]]\{4\}\2/g' file.txt
sed -e 's/(^.{89})($)/\1[[:space:]]{4}\2/g' file.txt

  • Are you on Mac OS? Try `sed -i '' 's/^.\{89\}$/& /' file.txt` (with 4 spaces after `&`). In POSIX BRE patterns, `{m,n}` quantifier must have escaped braces. If `-i` still does not work, use `sed 's/^.\{89\}$/& /' file.txt > newfile.txt` – Wiktor Stribiżew Jan 14 '19 at 11:16
  • 1
    Better put some sample text ere. – Til Jan 14 '19 at 11:20
  • What do you mean that the `-i` option "isn't accepted"? Don't include end of line `$` as a capture group with `($)`. It's unnecessary and won't work. Did you try `sed -e "s/(^.{89})$/\1 /" file.txt`? – lurker Jan 14 '19 at 11:28
  • @lurker passing in -i to sed I get "sed: illegal option -- i". I've just tried your command and got the "cannot be parsed" error. – Paul Wright Jan 14 '19 at 13:04
  • Sorry, I was a bit hasty in my suggest. Try this: `sed -e "s/([^.]{89})$/\1 /" file.txt` – lurker Jan 14 '19 at 13:05
  • @lurker Some success. That command, including escapes for the ( ) { } characters added spaces, but removed line ends. I tried adding them back in with ```sed -e "s/\([^.]\{89\}\)\($\)/\1 \2/"``` but no joy. – Paul Wright Jan 14 '19 at 13:18
  • The behavior of `sed` on your system isn't the same as Linux. It shouldn't remove a line end. The `$` is a token representing the end of line but shouldn't have to be resubstituted in the replacement. If my suggestion worked but removed line ends, you can put the line end back, possibly, with `sed -e "s/([^.]{89})$/\1 \n/`. Also check the `sed` options in the `sed` manual page (`man sed`) for line end handling. – lurker Jan 14 '19 at 13:36
  • I'm finding more articles where people are having issues with sed in HP-UX. using the new line \n just adds 'n' where the end of line should be, which is ridiculously frustrating. I'll keep trying different combinations of the command. – Paul Wright Jan 14 '19 at 13:50
  • That behavior of `\n` is what I feared. You could check the `sed` options as there may be an option that causes it to see it as a line-end. I still find it odd that it gobbles up the line end if you use `$` on the source regex. I suppose another option might be `awk`. – lurker Jan 14 '19 at 13:52

3 Answers3

0

The main issue here is that you are using BRE POSIX syntax and unescaped ( / ) are treated as literal parentheses and unescaped {/} are treated as literal braces.

This should work:

sed 's/^.\{89\}$/&    /' file.txt > newfile.txt

Here:

  • ^ - matches the start of a line
  • .\{89\} - matches any 89 chars
  • $ - asserts the end of line position.

The & in the replacement refers to the whole match.

If you need to use -i option, see sed edit file in place.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

Try this:

awk '{ <br>
 diff = 89 - length($0); <br>
 for(i=1;i<=diff; i++) <br>
 { <br>
   $0 = $0  "a" <br>
 } <br>
 print $0 <br>
} <br>
' FileName.txt
Harshith Rai
  • 3,018
  • 7
  • 22
  • 35
0

A software tools kludge for HP-UX, based on its manuals:

sed 's/.*/    /' file | paste -d ' ' file - | cut -c 1-93 

How it works:

  1. Use sed to create a dummy stream of the same number of lines as file, but with four spaces on each line.
  2. Use paste to append the dummy stream to what's in file. (Because of the -d ' ' it adds five spaces, but it doesn't much matter.)
  3. Use cut to chop off anything over 93 bytes.

If HP-UX sed is even less like GNU sed than I've supposed, it could be replaced with some equivalent like:

yes '    ' | head -n $(wc -l < file) | paste -d ' ' file - | cut -c 1-93 
agc
  • 7,973
  • 2
  • 29
  • 50