unwrap text file - Can't find string terminator ERROR - perl

I got a LDAP schema but ldifde wraps long lines so after googled I found this command to fix the file, but I'm getting the following error:
c:\Perl64\bin>perl -p -e 'BEGIN {$/ = undef} s/\n(?=[a-z])/ /g' test.ldf
Can't find string terminator "'" anywhere before EOF at -e line 1.
c:\Perl64\bin>
Even replacing the content with a single line I get the same error so I assume the sentence is wrong.
Could you give some clue about that, Im newbie on perl.
Thanks,
m0dest0.

You seem to be on windows. Windows does not recognize single quote ', you need to use double quote ":
c:\Perl64\bin>perl -pe "BEGIN {$/ = undef} s/\n(?=[a-z])/ /g" test.ldf
You should be aware that this does not change the input file, it just prints to standard output. If you want to alter the file, you can either add the in-place edit switch, e.g. -i.bak (saves backup in test.ldf.bak) or use redirection:
c:\Perl64\bin>perl -pe "BEGIN {$/ = undef} s/\n(?=[a-z])/ /g" test.ldf > out.ldf

Wild guess, but could it be that quoting is messing you up, in the command prompt? Try putting that one line in a file, and see if perl -p file.pl test.ldf would run.

Related

Execute sed command inside TCL script

I am trying to execute sed command inside TCL script . Basically i wanted to remove all empty lines from the input file before reading the file using TCL. so i tried following in my script
exec sed -i '/^\s*$/d' .tmp.PG_Ring
set fid [open ".tmp.PG_Ring" r]
But the script is dumping following Error .
sed: -e expression #1, char 1: unknown command: `''
while executing
"exec sed -i '/^\s*$/d' .tmp.PG_Ring"
(file "pg_ring.tcl" line 1)
could you please provide me work around for this & help me with best way to do this?
That won't work, as single quotes have no special meaning to Tcl at all. Tcl uses braces to mean the same sort of thing (except they nest nicely), so instead you can use this:.
exec sed -i {/^\s*$/d} .tmp.PG_Ring

Remove all lines from a file that contain a non-ascii character

I have a file with thousands of lines in it. I would like to search each line for a non-ascii character, and if found, delete that entire line.
I found this bit of code in perl:
perl -i.bak -ne 'print unless(/[^[:ascii:]]/)' file
But I get this error when I run it with my file:
Can't find string terminator "'" anywhere before EOF at -e line 1.
Does anyone have any code for an actual perl script instead of a one liner like the above?
That's a shell error, most likely because you're on a windows machine.
Use double quotes instead of single quotes:
perl -i.bak -ne "print unless(/[^[:ascii:]]/)" file

perl find and replace from command line. special characters?

So, I have looked around for an answer to this, and indeed I have found some, but none seem to work...
I have a folder full of bash scripts that I need to modify. specifically, I need to replace the line
INPUT=/data/scratch02/mpgussert/HAWC-30/${RUN}_reco
with
INPUT=/data/hawc01/hawcroot/data/hawc/reconstructed/quesadilla/${RUN}
I have tried this
perl -w -i -p -e "s'INPUT=/data/scratch02/mpgussert/HAWC-30/${RUN}_reco'INPUT=/data/hawc01/hawcroot/data/hawc/reconstructed/quesadilla/${RUN}'g" *.sh
which executes without error, but does not find and replace the desired text. From my understanding, using ' to deliminate the regex should search without special character replacement. Is that correct? If so, any ideas why it fails?
I have also tried
perl -w -i -p -e "s/INPUT=\/data\/scratch02\/mpgussert\/HAWC-30\/\$\{RUN\}_reco/INPUT=\/data\/hawc01\/hawcroot\/data\/hawc\/reconstructed\/quesadilla\/\$\{RUN\}/g" *.sh
the backslash should ignore special character replacement, but this returns the following error.
Backslash found where operator expected at -e line 1, near "RUN\"
syntax error at -e line 1, near "RUN\"
Execution of -e aborted due to compilation errors.
So it's searching for RUN\, which is not what I want... Any thoughts? I would appreciate any help you can give.
Thanks!
You want the pattern to be ...\$\{RUN\}..., but that's not what you're passing:
$ echo "...\$\{RUN\}..."
...$\{RUN\}...
You either need do more escaping, or switch to single quotes.
$ echo '...\$\{RUN\}...'
...\$\{RUN\}...
All together:
perl -i -wpe'
s{INPUT=/data/scratch02/mpgussert/HAWC-30/\$\{RUN\}_reco}
{INPUT=/data/hawc01/hawcroot/data/hawc/reconstructed/quesadilla/\${RUN}}g
' *.sh

How to delete specfic text in a file using a perl one liner?

I'm trying to find some text in an XML file and delete only a part of a line that has it.
I found this format to try: perl -p -i -e "s/$1/$2/g" $3 after some code searches.
So I'm using this code:
perl -p -i.bak -e "s/\'../../../specialText/\'//g" "C:/box/fileName.XML";
What I want to do is delete everything from the inner single quotes as in:
'../../../specialText/', but using q() or \' to escape the quote doesn't work and I'm not sure the ..'s aren't messing things up either. I'm guessing that not putting anything in as a text replacement will delete it properly, but I'm not sure.
The errors are:
Backslash found where operator expected at -e line 1, near "/specialText/\"
(Missing operator before \?)
syntax error at -e line 1, near "/specialText/\"
Can't find string terminator "'" anywhere before EOF at -e line 1.
How do rewrite this one liner to accomplish this?
This works.
C:\box>perl -p -i.bak -e s/Copyright/bar/g Test.txt
I tried it on another file, so now I just have to play with it to modify my original.
You can escape the . and / characters in the search string by putting a backslash (\) before each of them.
However, to avoid acute leaning toothpick syndrome, I'd recommend instead using alternative regexp delimiters and the \Q and \E escape sequences, like this:
perl -p -i.bak -e "s(\Q'../../../specialText/'\E)()g" "C:/box/fileName.XML"
And what's wrong with using another set of delimiters?
perl -p -i.bak -e "s{'../../../specialText/'}{}g" "C:/box/fileName.XML"

Using perl in windows oddities

I've got windows bat file, (using activeperl)
#echo off
perl -p -e 's/DIV\[/div\[/g' orginal.txt > output.txt
perl -p -e 'rename("output.txt", "orginal.txt")';
...
Running a .bat file, and I just cant get it to run properly..
Can't open ;: No such file or directory, <> line 12248.
Can't find string terminator "'" anywhere before EOF at -e line 1.
Not sure what I'm doing wrong..
You can't use single-quotes to enclose the Perl code in Windows. As a result, you need to escape the double-quotes or find other alternatives, such as qq(...).
perl -pe "s/DIV\[/div\[/g" original.txt > output.txt
perl -pe "rename(qq(output.txt), qq/original.txt/)"
Note that in this case, the arguments to rename can simply be rename('a.txt', 'b.txt') since they are literals and no variable interpolation is required.
You ought to use double quotes to quote the program text under Windows cmd. In your example, you can just swiztch double and single quotes. In cases where you really need double quotes in the perl text, use qq{ .... } instead.
The other posters are correct: windows requires double quotes for -e scripts to perl, which often screws things up. There is one more thing you can do, though: Use the -i switch, like this:
#echo off
perl -pi.bak -we "s/DIV\[/div\[/g" original.txt
The -i.bak switch will edit the file in place - no rename required - AND it will store a backup of the file in "original.txt.bak". If you do not want a backup, remove the ".bak" part and just use -pi.