notepad++ textfile column question/problem

Status
Not open for further replies.

Z1ggy

Ars Legatus Legionis
15,487
Ok so its a strange problem.

I have a text file that needs to be uploaded somewhere so i can run some scripts on it.

The scripts rely on the textfile being a certain number of columns per line.

The file coming in however has multiple lines that arent(its ok its supposed to be this way).

so i can have 1 line be 100 characters long, the next line be 700 characters long, the next line be 75 characters long, then 700 characters, etc....

Right now im manually making each row be 700 characters long(the longest line).

Is there anyway in notepad++ (or something else) so that i can say each row needs to be 700 characters long(by adding spaces to the rows)?

Its a rare enough occurrence that i can do it manually, but its just annoying.
 
Several options here. You're on Windows but sed, awk and vim are all included in a standard install of Cygwin (if you're on Windows 10, you could use WSL which is a free first party add in).

This is the type of thing Linux is the best at.

https://www.google.ca/url?q=https://sta ... _o87j3TmCw

FWIW, I believe there's a Windows port of Vim.

You can also use Linux to find the longest line, and how many characters are in it, if 700 is not a hard limit. Not at a computer at the moment, but try some combo of wc and sort.
 

minnmass

Ars Tribunus Militum
2,087
Subscriptor
If you have access to a Linux-ey system (including Cygwin; haven't used WSL, do I don't know if it'll work there, but I wouldn't be surprised) and you know the required length of a line (your post suggests it's fixed at 700), you can use printf:

Code:
cat <file> | while read line ; do printf '%-700s' "$line" ; done > <outputFile>

That will concatenate <file> out to standard output which while read will read line by line, putting each line into a variable called "line". printf will then print each line out as a string (the "%" and "s" in %-700s, left-justified (the negative part of -700) in a 700-character wide field (the "700" part of -700. Each line is printed to standard out, then the > redirects that to <outputFile>.

Depending on how sensitive the script on the server is to line endings, you may need to run dos2unix and/or unix2dos on <file> and/or <outputfile> (respectively), and may need to change %-700s to %-699s (dealing with line endings across operating system conventions kinda sucks).

If you need to find the longest line in a file,

Code:
awk '{ print length($0); }' <file> | sort -n | tail -n 1

should do the trick. The awk bit will print the length of each line of the file, sort -n sorts numerically (so that 11 is greater than 2, where the default is to sort ascii-betically, so 2 comes after 11); tail -n 1 just prints out the last line (which will be the length of the longest line).

Mind, in an ideal world, much of this would be done server-side, before the existing scripts run on it, so that you don't have to dink around with it yourself.

PowerShell looks like it can do string formatting, too: https://ss64.com/ps/syntax-f-operator.html says you can do this:

Code:
Left and right align text:
PS C:\> "|{0,-10}| |{1,10}|" -f "hello", "world"
|hello     ||     world|

I'm not sure how to run that on a file, but I have to think it's relatively straightforward.
 

Drizzt321

Ars Legatus Legionis
34,930
Subscriptor++
If you have access to a Linux-ey system (including Cygwin; haven't used WSL, do I don't know if it'll work there, but I wouldn't be surprised)

I'd bet it'd work under WSL, from what's been mentioned about it officially and what I can see it's basically a Ubuntu Bash/GNU tools environment, so it'll have the awk/sed/vim/etc or you can install it using apt-get I believe. You'd have to get used to having to prefix to the correct mount point to the windows drive letter, and then from there to the actual file path, but I'd expect it to be doable and work.

OP:
When you say "rows must be X characters long", you're not talking about fixed width fields, you're saying the program requires X characters per line, regardless of the contents of the line? So if it's a blank line, nothing on it, it would still need to be, say, 700 characters (say, 700 spaces) long? I'm very confused...

Are you sure it isn't X characters per field, right or left aligned, and then having to fill in spaces to fill in the rest (or front) of the field to get the fields/columns all lined up?

A bit more information might be useful.
 

Z1ggy

Ars Legatus Legionis
15,487
If you have access to a Linux-ey system (including Cygwin; haven't used WSL, do I don't know if it'll work there, but I wouldn't be surprised)

I'd bet it'd work under WSL, from what's been mentioned about it officially and what I can see it's basically a Ubuntu Bash/GNU tools environment, so it'll have the awk/sed/vim/etc or you can install it using apt-get I believe. You'd have to get used to having to prefix to the correct mount point to the windows drive letter, and then from there to the actual file path, but I'd expect it to be doable and work.

OP:
When you say "rows must be X characters long", you're not talking about fixed width fields, you're saying the program requires X characters per line, regardless of the contents of the line? So if it's a blank line, nothing on it, it would still need to be, say, 700 characters (say, 700 spaces) long? I'm very confused...

Are you sure it isn't X characters per field, right or left aligned, and then having to fill in spaces to fill in the rest (or front) of the field to get the fields/columns all lined up?

A bit more information might be useful.
Correct.
There won't be any blank lines
Each line had to be 700 characters long, with anything shorter than that being padded out to 700 with spaces.

Like I said it's done so infrequently, I can do it by hand. I was looking for a quicker way(one I can do at work, which means I can't install anything).
I was hoping for a plug-in of some variety for notepad++.

But for now I just manually edited the file.
 
Status
Not open for further replies.