Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have an access log that was written with nginx and lua code.
It is url encoded and those some characters are written in the format of \xHexCode (for example, double quotes are written as \x22).
I would like to run awk or perl or other fast script to replace it back.
You can use gnu-awk like this:
str='\x22 \x41 written as \x22).'
awk -v RS='\\\\x[0-9]+' 'RT{ORS=sprintf("%c", strtonum("0" substr(RT, 2)))} 1' <<< "$str"
" A written as ").
This is how it is working:
Using RS='\\\\x[0-9]+' we're separating custom record separator for each of those \xNN numbers.
substr(RT, 2) takes x41 from \x41
strtonum("0" substr(RT, 2)) adds 0 to make it 0x41 and returns ascii code 65.
printf "%c" prints equivalent ascii character A from 65.
ORS=... sets output record separator same as the return value of sprintf.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I need to remove portion of text from a very big text file.
The text file is something like that:
Abcdefghijk
LOCK TABLES `core_log` WRITE; <----
(DATA - over 1 Gb of text data)
UNLOCK TABLES; <----
lmnopqrstuvxyz
I need to create a script (Windows or Unix) that remove all content from "LOCK TABLES" to "UNLOCK TABLES;" and preserve rest of file. After the script will run I need to have
Abcdefghijk
lmnopqrstuvxyz
I can save the extracted data in another file or I can overwrite the same file.
Thanks for help.
With GNU sed:
sed -i '/^LOCK TABLES/,/^UNLOCK TABLES/d' file
Output to file:
Abcdefghijk
lmnopqrstuvxyz
This is best done with awk:
$ awk '/^LOCK TABLE/{f=1} /^UNLOCK TABLE/{f=0} f' file
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How can I change a number on a line in a file using a unix tool like awk or sed?
I want to change the line 3 in my example file to the number 1-10 using a shell script. I think I need to use regex to recognize the digit but I'm not sure how to do this, or to allow multiple digits (like 10).
Example file:
/examples/are/hard so/hard/1
Shell script so far:
for i in {1..3};
do
sed 's|/examples/are/hard so/hard/7 | /examples/are/hard so/hard/'"$i" ex_file
cat ex_file
done
Desired output:
/examples/are/hard so/hard/1
/examples/are/hard so/hard/2
/examples/are/hard so/hard/3
What you've run isn't a valid sed command. If you're trying to do a substitution, that's s/search/replace/flags.
I imagine you meant:
sed 's/here\/is the number\/to\/change 3/here\/is the number\/to\/change '"$i"'/' ex_file
Note that we temporarily break out of single quote. Inside of single quotes, variable aren't interpolated. We swap the double quotes, bring in $i, then return to single quotes to finish the command.
P.S. You also don't have to use / as your delimiter.
sed 's|here/is the number/to/change 3|here/is the number/to/change '"$i"'|' ex_file
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My Perl script just takes whatever you give as an input, and I want make it more robust by checking the pattern of the input string. My input string has to be in the format xxxxx-xxxx-xxxx. How can I check that?
$foo =~ /^.{5}-.{4}-.{4}\z/s
For example, this will repeatedly ask for the value until it gets a valid one.
my $foo;
while (1) {
print("Please provide foo (xxxxx-xxxx-xxxx): ");
my $foo = <STDIN>;
die("EOF\n") if !defined($foo);
chomp($foo);
last if $foo =~ /^.{5}-.{4}-.{4}\z/s;
print("Invalid input\n");
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a file containing lines that look like this:
GTTCAGAGTTCTACAGTCCGACGATCGGATGAGNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCTCCGAGTNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCCTTATATNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCGAAGTGCNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCAAGTTTTNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCCGACGAANNNNNN
I want to remove the first 26 and final 6 characters from each line. I haven't been able to write a good regular expression to accomplish that using vi, but I'm not sure what else to do.
Any suggestions?
Thanks!
Try with grep.
This will keep the last 13 characters and then the first 7, returning only the matching characters (-o) with the Perl-compliant -P flag:
grep -oP ".{13}$" foo.txt | grep -oP ".{7}"
If your file name is foo you can use cut to grab out the range of chars you want:
$ cut -c27-33 foo
This produces:
GGATGAG
TCCGAGT
CTTATAT
GAAGTGC
AAGTTTT
CGACGAA
cut can take a character range, if the lines are a fixed size (they appear to each be 39 characters)
cut -c27-33 file.txt
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have below piece of code in Perl
my $file2 = "devil’s-claw-extract.html";
$file2 =~ s/’//ig;
print "$file2";
This code is working fine(means replacing ’ with ) when running in konsole but not working in browser.
Please help me out.
Of course it “works”, and I will believe so until you produce a self-contained example that indicates otherwise.
Your first problem is that you are reinventing the wheel, there already is a module on CPAN to do such escaping for you:
use utf8; # because this source file contains special chars
use HTML::Entities;
my $file2 = "devil’s-claw-extract.html";
print encode_entities $file2;
Output:
devil’s-claw-extract.html
or with encode_entities_numeric:
use utf8;
use HTML::Entities 'encode_entities_numeric';
my $file2 = "devil’s-claw-extract.html";
print encode_entities_numeric $file2;
Output:
devil’s-claw-extract.html
Secondly, it is worth noting that your input string contains ’, a single right quote. This has the codepoint U+2019 (not U+0092, which is in a private use area. Conveniently, it decodes to ’ in the Windows-1252-encoding, but the actual encoding should always be explicitly set).
The apostrophe ', which you likely wanted, is U+0027 or '.