Copy and pasted file names from finder on mac and vim showing ^M after each filename, how to fix? - linux

I copy and pasted files from finder, into a text file to get the names of the files in a certain directory to a text file. When I open the file the files from each directory are all the same line...
For example: I copy and pasted from the "Hats Directory":
Line1 is:
hats1.jpg^Mhats2.jpg^Mhats3.jpg^M...
In that same text file I copy and pasted files from the "Shoes" directory
Line 2 is:
shoes1.jpg^Mshoes2.jpg^Mshoes3.jpg^M...
I have tried
:%s/^M//
dos2unix
CTRL-V CTRL-M
Neither of them work, am I going about this process the wrong one? Is there a more efficient way to do what I'm trying to do?

A more simple way is using bash truncate tr command.
Just do, tr -d '\r' < inputFile > outputFile to remove all the carriage return characters.

Try this command:
:%s/^M//g
The ^M that you are looking for is ctrl+V Enter

These are old style mac line breaks (a single \r).
Try mac2unix

You could get that list without involving the Finder and without fishy formatting:
:r!ls path/to/dir
See :help :r and :help :!.

Related

how to remove new lines in vi editor linux?

I am using Linux (centos flavor) and created a file with the following text:
" hello
world
"
Question:
Now, I opened the file in vi editor mode and am able to remove all non blank characters(backspace or delete keys do nothing).
But newline characters persist and I get error saying "no previous regular expression".
What should I do to remove all the new lines so that my file is just empty?? I have tried backspace key many times but no effect and I do not want to use cat > filename to just overwrite the file to make it empty!
You can use dd to delete any lines in vi editor.
Example:
You have a file having 6 lines and you want to delete all 6 lines:
Open the file using 'vi` editor
Go to first line
use 6dd
:g (for global) could help you here.
:g/^$/d basically says that "globally find any pattern matching ^$ and delete those".
If you think that you might have blanks in those lines, you could say ^\ *$
open txt with vi
:1 << move cursor to first of file
d << enter delete mode
G << move cursor to end of file
It will remove all from cursor( in this case, at first of file ) to end of file
or
open txt with vi
d
N (Number, As many as you want to delete lines)
Enter

Linux replace ^M$ with $ in csv

I have received a csv file from a ftp server which I am ingesting into a table.
While ingesting the file I am receiving the error "File was a truncated file"
The actual reason is the data in a file contains $ and ^M$ in end of the line.
e.g :
ACT_RUN_TM, PROG_RUN_TM, US_HE_DT*^M$*
"CONFIRMED","","3600"$
How can I remove these $ and ^M$ from end of the line using linux command.
The ultimately correct solution is to transfer the file from the FTP server in text mode rather than binary mode, which does the appropriate end-of-line conversion for you. Change your download scripts or FTP application configuration to enable text transfers to fix this in future.
Assuming this is a one-shot transfer and you have already downloaded the file and just want to fix it, you can use tr(1) to translate characters. So to remove all control-M characters from a file, you can pipe through tr -d '\r'. Or if you want to replace them with control-J instead – for example you would do this if the file came from a pre-OSX Mac system — do tr '\r' '\n'.
It's odd to see ^M as not-the-last character, but:
sed -e 's/^M*\$$//g' <badfile >goodfile
Or use "sed -i" to update in-place.
(Note that "^M" is entered on the command line by pressing CTRL-V CTRL_M).
Update: It's been established that the question is wrong as the "^M$" are not in the file but displayed with VI. He actually wants to change CRLF pairs to just LF.
sed -e 's/^M$//g' <badfile >goodfile

multiple end of file $'s in a single file

I copy pasted some enum values from my IntelliJ IDE in windows to notepad, saved the file in a shared drive, then opened it up in a linux box. When I did cat -A on the file it showed something like:
A,B,C,^M$
D,E,F,^M$
G,H,I,^M$
After searching around I figured that ^M is the carriage return and $ means the last line of the file. I'm just puzzled at how this file is able to have multiple $'s.
From man cat on my GNU box:
-A, --show-all
equivalent to -vET
(snip)
-E, --show-ends
display $ at end of each line
Thus, there are multiple $s because there are multiple lines, each with an end.
$ is the end of line marker with cat -A, not end of file.
This is indicating the file has Windows-style line endings (carriage return followed by line feed) and not Unix-style (only line feed).
(You can convert text files from one format to the other using the programs dos2unix or unix2dos.)

vi, find line and output/output to separate file

I'm looking for a command in VI/VIM to search for particular text in the file and grab the whole line and output in either on the screen or separate file. e.g.
This is some line with this _word_ and some other text.
This is some line with this some other text and some other text.
so this would output only the first line..
:redir > output.txt
:g/_word_/p
:redir END
The line will be output to the screen and to output.txt. See :h :redir.
EDIT: I agree with others who suggest using plain old *nix grep if you can. Situations where you might not be able to use grep:
You're searching buffer text that doesn't exist in a file on the filesystem.
You're using Vim-specific regex extensions, like cursor location, column number, marks, etc.
You want do this in a cross-platform way, and grep might not exist on the system you're using.
redir can be useful in these situations.
use the g (global) command:
:g/_word_/y
will yank all lines containing _word_
having mentioned the DOS find command, you probably want to use grep:
grep -h '_word_' * > results
in vi, from command mode
search for _word_
/_word_
yank the line
yy
paste the line
p

Vim - how to start inserting at the end of the file in one step

Is there a single shortcut to start inserting in the new line at end of the file?
I'm aware of G + o combo.
There's also the command line option "+":
vim + myfile.txt
Will open myfile.txt and do an automatic G for you.
Not that I know of - G+o is what I would have suggested too, but that is 2 steps :)
You could always create a macro which does G+o, and then you can invoke the macro which will be 1 step.
Adding the following into ~/.vimrc will create one for you:
:nmap ^A Go
To type the "^A" first press Ctrl-V, then press Ctrl-A. You can then use Ctrl-A to append at the end of the file when not in insert or visual mode.
echo >> myfile.txt && vim -c 'startinsert' + myfile.txt
You can also save the above command in a script and then use $1 instead of myfile.txt, name your script myvim ( or whatever you like ) and always open your files and start writing away instantly.
myvim myfile.txt
You could stick the map definition in your .vimrc and then invoke it when the you open the file.
Or, if you only want to do this for a particular file, you could create an autocmd for that file type that does it automatically. See autocommand in the vim doc's.

Resources