I'm writing a script to check for dos line breaks, in a unix environment. however, I dont have a sample to check my test cases. How do I create one?
Load Vim, write a few lines of text, then:
:set fileformat=dos
:w
Use hexdump -C if you want to verify that the resulting file does contain CRLF.
fprintf(fp,"something\r\n");
or
data[n++]=0x0D;
data[n++]=0x0A;
fwrite(data,1,n,fp);
or have a program like this
rb=fread(datain,1,sizeof(datain),fpin);
if(rb==0) break;
rc=0;
for(ra=0;ra<rb;ra++)
{
if(datain[ra]==0x0A)
{
dataout[rc++]=0x0D;
}
dataout[rc++]=datain[ra];
}
fwrite(dataout,1,rc,fpout);
dataout has to be twice as big as datain just in case.
it is far easier just to tell your text editor to do it, open the file then save or save as after changing the format for that file. The scintilla based editors, scite, geany, notepad++, have no problem with it (geany makes it very easy on a document by document basis). looks like vi and probably emacs will work. Textpad no problem.
Related
I need to write a shell script that will do the following: will allow to create a text file from the keyboard and would find out the file type.
Could you help me? Thank you
to create a text file from keyboard input, you can use
cat > yourfile
which will send everything you type to yourfile, until you type ctrl+D on an empty line. or else, you can use any text editor. In all cases, the user will need to know how to terminate the input.
Then, if you want to identify the content of this file, you might suppose that the first line is a shebang (typically :
#!/bin/bash
and analyse it.
I'm using Ubuntu Bash within Windows 10 and I have a text document with:
{u'osidjfoij23': 3894798, u'oisjdao':234567, u'oaijsdofj': 984759}
using tr, in terminal I change my output to
'osidjfoij23': 3894798,
'oisjdao':234567,
'oaijsdofj': 984759}
when opening the same document via notepad.exe, the newline "\n" added from tr doesn't register and all the data gets presented as a paragraph.
I know this is because bash and notepad have different encodings for their documents, is there a way to make these work together or an alternative I can use for notepad?
You can use unix2dos to convert a file to Windows line endings. Linux programs handle Windows line endings fairly well, so this shouldn't break anything (especially if that's JSON as it appears to be).
I don't have a huge amount of experience using VI. I am running it on Mac OSX.
I've copied and pasted text before in the editor using (when I say gui in the following I mean the Mac OSX gui)
Cursor to highlight and copy i using command C or the gui or the yy command in VI.
Entering insert mode where I want to paste the text and then pasting using command V or the gui
My problem is that a very long line that is split over multiple lines in the terminal becomes multiple lines as shown on the terminal when copied and pasted by any of the methods.
How do I get it to copy and paste excatly as is?
Move the cursor to the line from where you want to copy and paste contents at another place.
Hold the key v in press mode and press upper or lower arrow key according to requirements or up to lines that will be copied. you can press key V to select whole lines.
Press d to cut or y to copy.
Move the cursor to the place where you want to paste.
Press p to paste contents after the cursor or P to paste before the cursor.
You have
:set paste
Put Vim in Paste mode. This is useful if you want to cut or copy
some text from one window and paste it in Vim. This will avoid
unexpected effects.
Setting this option is useful when using Vim in a terminal, where Vim
cannot distinguish between typed text and pasted text.
Assuming your vi is actually vim, before pasting, do:
:set paste
That disables word wrapping and auto-indent and all similar things that modify typed text. After pasting, turn it off again with
:set nopaste
The reason is that while gvim can tell pasting from typing (so you don't need this when using gvim), the terminal version can't, because it's the terminal doing copy and paste and vim simply sees the text as typed. And therefore applies the transformation like it does for any other text.
Someone showed me a neat trick. In the vi editor, set to insert mode ("i"). Then middle-button click at the location where you would like to insert.
Well, if it is actually one long line the easiest way to do this is with a 'Y' in command mode. Just move to the line and do Y and then move to where you want to put the line and do a p (for paste).
After trying everything suggested here and in other answers including trying to format the file afterwards with vi(m) commands and seds, I got smart and just heredoc'd what I wanted to paste with redirection to the file. i.e.
cat << EOF > yourfile.txt
paste what you are trying to paste
another line of pasted text
yet another
foo > bar?
foo = bar??
end of the file(yay)
EOF
everything between the first line and the last line will be pasted to your file without those pesky newlines interpreted as many spaces/tabs. Just beware that what you are pasting might have its own heredoc(like mine did coincidently LOL). In that case would need to manually paste those lines in your editor which shouldn't be an issue.
I have a .txt (Mac OS X Snow Leopard) file that has a lot of text. At the end of a paragraph, there is a hard return that moves the next paragraph onto another line. This is causing some issues with what I am wanting to do to get the content into my db, so I am wondering if there is anyway I can remove the hard returns? Is there some sort of script I can run? I am really hoping I don't have to go through and manually take the hard returns out.
To recap, here is what it looks like now:
This is some text. Text is what this is.
And then this is the next paragraph that is on a different line.
And this is what I would like to get to:
This is some text. Text is what this is. And then this is the next paragraph that is on a different line.
For all several thousand lines in my .txt file.
Thanks!
EDIT:
The text I am dealing with in my txt file is actually HTML:
<span class="text">1 </span> THis is where my text is<br/>
And when I run the cat command in terminal like mentioned below, only the first is there. Everything else is missing...
In a terminal:
cat myfile.txt | tr -d '\r' > file2.txt
There's probably a more efficient way to do this, since the "tr -d '\r'" is the active ingredient, but that's the idea.
I normally just use an editor with good Regular Expression support. TextWrangler is great.
An end of line in TextWrangler is \r, so to remove it, just search for \r and replace it with a space. TBH, I always wondered how it handles CRLF-encoded files, but somehow it works.
I believe you can do this with Applescript. Unfortunately I'm not familiar with it however the following should help you to acomplish this (it's for a different problem but it will lead you in the direction you need to go): http://macscripter.net/viewtopic.php?id=18762
Alternatively if you didn't want to do this with Applescript and have Excel installed (or access to a machine with it) then the following should help: http://www.mrexcel.com/forum/showthread.php?t=474054
In Linux terminal cat file.txt | tr -d "\r\n" | > new file.txt will do. Modify \r\n part to remove desired charters.
The following is the contents of the Windows System Clipboard
:function CurrentLineLength
: len = strlen(getline("."))
: return len
:endfunction
I hit the colon and then control r
I then hit shift 8 to paste the contents of the system clipboard.
I hit return and vim comes back with
E488: Trailing Characters
I see some ^M characters in there and removing them does not help. I do know that I can paste the functions into a .vim file and read them that way so its not crippling but as I work through some examples of vim script this would be nice to have.
Is there something special about how functions are entered in or is it possible to paste them from the system clipboard?
Thanks!
I'm not sure about pasting multiple lines to command mode, but you can achieve the same thing by simply putting the function in a register and executing the register (same as a macro).
Also, Vim doesn't seem to like that function as you've pasted it, I've made a couple of changes below. If you copy the below to the system clipboard and then press #* from normal mode, it works.
:function CurrentLineLength()
: let len = strlen(getline("."))
: return len
:endfunction
Vim should not have any problems with carriage returns in command mode (that's what the ^M characters are). I would guess that there are some other characters in the code you're pasting - this is quite possibly the problem if you're pasting from a web page. Try putting the contents of your clipboard into a file and see if it's really what you expect it to be (including all whitespace characters).