How do I join by new line - ruby

I am reading a file which has source code. I need to append 2 spaces before each line. This is what I am doing.
data = read_file
data.split(/\n/).collect {|l| ' ' + l}.join('\n')
However after joining when I do puts then it prints \n literally and it is not a line break. How do I fix that?

You need to use a double quote (") instead of a single quote. So replace this:
'\n'
with this:
"\n"
Read more about it here.
You might want to use \r\n instead if you want your line-endings to be CRLF instead of LF (some Windows editors such as Notepad won't see a LF linebreak).

I was able to finally get this to work for my application by using
"<br>"

Related

Remove carriage return end of variable

I'm getting really strange output for this program. What is the "Carriage Return" doing, and how to remove it - missing single quote in the end? Why is the letter "T" missing? How to write code to correct this?
code i'm using
#!/bin/bash
export DATABASE_LIST="/opt/halogen/crontab/etc/db_stat_list.cfg"
export v3=""
while read -r USERID ORACLE_SID2
do
v3="This is '${ORACLE_SID2}' "
echo $v3
done < <(tac $DATABASE_LIST)
output
'his is 'OT1SL80
'his is 'OT1SL010
The file I'm reading from is not corrupt and is small one with two lines
[oracle#ot1sldbm001v test2]$ cat /opt/halogen/crontab/etc/db_stat_list.cfg
asp_dba/dba OT1SL010
asp_dba/dba OT1SL80
Thank you
Your DATABASE_LIST file is in DOS/Windows format, with carriage return + linefeed at the end of each line. Unix uses just linefeed as a line terminator, so unix tools treat the carriage return as part of the content of the line. You can keep this from being a problem by telling the read command to treat the carriage return as whitespace (like spaces, tabs, etc), since read automatically removes whitespace from the beginning and end of lines:
...
while IFS="$IFS"$'\r' read -r USERID ORACLE_SID2
...
Note that since this assignment to IFS (which basically lists the whitespace characters) is a prefix to the read command, it only applies to that one command and doesn't have to be set back to normal afterward.

How to remove newline breaks in fields in Unix file without using perl

I have a file which has newline breaks in one of the fields.
eg:
See third line :
"A"|"USD"|"123"|"AIRPROMOTION"|"EXPIRE"
"B"|"USD"|"456"|"AIRPROMOTION"|"EXPIRE"
"C"|"USD"|"789
"|"AIRPROMOTION"|"EXPIRE"
I tried the command perl -p00e 's/\n"|//g' which worked just fine for a small file.But my file is huge (~100MB) and it gives 'Segmentation fault' error.
What are the other options?
The reason of segmentation fault is your are enable the slurp mode. Don't do that. Instead read the file line by line.
Try this
perl -lne 'my $nxt_line = <>;($nxt_line=~m/^"\|"/)?print "$_$nxt_line":print "$_"' file.txt
In above script $nxt_line will store the next line of the file.. Then make the pattern match for to do it.
Try this! Should work like a charm!
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n"/"/g' input_file > output_file
I would use Notepad++ Replace function (\r\n\r\n and replace it with \r\n).
If you haven't it, you can download Notepad++ for free and is very useful application and has many uses.
At the View menu select Show Symbol and check on Show All Characters.
Press Cntrl+H or click on Search Menu and select Replace... option
Type in \r\n\r\n at Find what:
Type in \r\n at Replace it with
Click on Replace All button.
PS: The Text you have supplied is not just LF, it is CRLF which is \r\n. You can try your method. Remember you want to just replace CRLFCRLF with one CRLF, otherwise you will loose all your CRLF and all your text will appear in one line.

How to replace newline in csv with fart.exe at Windows command-line?

In FART.exe find/replace tool, i’m having some trouble with newline char:
> set lf=^& echo.
> fart myfile.csv lf "],["
Replaced 0 occurence(s) in 0 file(s).
Nothing gets replaced. What's the correct (and simplest) way to do this?
The line-terminators in the file might be CarriageReturn + LineFeed, not sure. How to check for both?
(PS, fart is the fastest replace tool i've tested on Windows. Tested much faster than repl.bat, jrepl.bat, findrepl.bat, sfk.ext, and powershell.)
edited to adapt to comments
fart includes a switch (-C or --c-style) to indicate that input/output strings contain C-style extended characters. In this case quotes are not needed around the search or replacement strings as there are no special characters in the command line:
fart -C myfile.csv \r\n ],[

How to remove the extra double quote?

In a malformed .csv file, there is a row of data with extra double quotes, e.g. the last line:
Name,Comment
"Peter","Nice singer"
"Paul","Love "folk" songs"
How can I remove the double quotes around folk and replace the string as:
Name,Comment
"Peter","Nice singer"
"Paul","Love _folk_ songs"
In Ruby 1.9, the following works:
result = subject.gsub(/(?<!^|,)"(?!,|$)/, '_')
Previous versions don't have lookbehind assertions.
Explanation:
(?<!^|,) # Assert that we're not at the start of the line or right after a comma
" # Match a quote
(?!,|$) # Assert that we're not at the end of the line or right before a comma
Of course this assumes that we won't run into pathological cases like
"Mary",""Oh," she said"
If you're not on Ruby 1.9, or just get tired of regexes sometimes, split the string on ,, strip the first/last quotes, replace remaining "s with _s, re-quote, and join with ,.
(We don't always have to worry about efficiency!)
$str = '"folk"';
$new = str_replace('"', '', $str);
/* now $new is only folk, without " */
Meta-strategy:
It's likely the case that the data was manually entered inconsistently, CSV's get messy when people manually enter either field terminators (double quote) or separators (comma) into the field itself. If you can have the file regenerated, ask them to use an extremely unlikely field begin/end marker, like 5 tilde's (~~~~~), and then you can split on "~~~~~,~~~~~" and get the correct number of fields every time.
Unless you have no other choice, get the file regenerated with correct escaping. Any other approach is asking for trouble, because the insertion of unescaped quotes is lossy, and thus cannot be reliably reversed.
If you can't get the file fixed from the source, then Tim Pietzcker's regex is better than nothing, but I strongly recommend that you have your script print all "fixed" lines and check them for errors manually.

Ruby gsub issues

I have a piece of text that resembled the following:
==EXCLUDE
#lots of lines of text
==EXCLUDE
#this is what I actually want
And so I was trying to remove the unwanted bit by doing:
str.gsub!(/==EX.*?==EXCLUDE/, '')
However, its not working. When I tried to remove the \n chars first, it worked like a dream. The issue is that I can't actually remove the \n characters. How can I do a substitution like this while leaving newlines in place?
By default, the . does not match line break chars. If you enable the m modifier in Ruby (in other languages, this is the s modifier) it should work:
str.gsub!(/==EX.*?==EXCLUDE/m, '')
Here's a live demo on Rubular: http://rubular.com/r/YxLSB1Iq95
Try str.gsub!(/==EX.*?==EXCLUDE/m, '')
That should make it span new lines.

Resources