Wrapping each line inside quotation marks with AutoHotkey - autohotkey

I have two thousand lines of text. I need each line to be wrapped inside quotation marks like "example".
Before
line 1
line 2
line 3
After
"line 1"
"line 2"
"line 3"
How could I take care of this with AutoHotkey?

Like this?
text=
(
bla bla bla
blah blah blah blah
blablah blablah blablah
)
MsgBox % RegExReplace(RegExReplace(text,"`am)^.","""$0"),"`am).$","$0""")

Related

Sed - substitute only within the line containing braces

I have been struggling with this all day. Trying to make variables in sections of a line only contained within braces.
Lines look like this:
blah blah [ae b c] blah [zv y] blah
I need to make this:
blah blah [$ae $b $c] blah [$zv $y] blah
There must be an easy way to do this. However, whenever I try
$ echo "blah blah [ae b c] blah [zv y] blah" | sed 's/\[\(\b.*\b\)\]/$\1/g'
I get greedy matching and just one variable:
blah blah $ae b c] blah [zv y blah
Is there something better?
Thanks,
$ echo "blah blah [ae b c] blah [zv y] blah" | sed -r ':b; s/([[][^]$]* )([[:alnum:]]+)/\1$\2/g; t b; s/[[]([[:alnum:]])/[$\1/g'
blah blah [$ae $b $c] blah [$zv $y] blah
How it works
-r
This turns on extended regex.
:b
This creates a label b.
s/([[][^]$]* )([[:alnum:]]+)/\1$\2/g
This looks for [, followed by anything except ] or $, followed by a space, followed by any alphanumeric characters. It puts a $ in front of the alphanumeric characters.
Note that awk convention that makes [[] match [ while [^]$] matches anything except ] and $. This is more portable than attempting to escape these characters with backslashes.
t b
If the command above resulted in a substitution, this branches back to label b so that the substitution is attempted again.
s/[[]([[:alnum:]])/[$\1/g
The last step is to look for [ followed by an alphanumeric character and put a $ between them.
Because [[:alnum:]] is used, this code is unicode-safe.
Mac OSX (BSD) Version
On BSD sed (OSX) limits the ability to combine statements with semicolons. Try this instead:
sed -E -e ':b' -e 's/([[][^]$]* )([[:alnum:]]+)/\1$\2/g' -e 't b' -e 's/[[]([[:alnum:]])/[$\1/g'
To disable it being greedy, instead of matching any character, match any character except closing bracket:
sed 's/\[\(\b[^]]*\b\)\]/$\1/g'
The task you want to do cannot be done with sed because context-sensitive matching cannot be described with regular grammar.
It's difficult to solve it using sed. As alternative, you can use perl with the help of the Text::Balanced module, that extracts text between balanced delimiters, like square brackets. Each call returns an array with the content between delimiters, the text before them and the text after them, so you can apply the regex that insert $ sign to the significative part of the string.
perl -MText::Balanced=extract_bracketed -lne '
BEGIN { $result = q||; }
do {
#result = extract_bracketed($_, q{[]}, q{[^[]*});
if (! defined $result[0]) {
$result .= $result[1];
last;
}
$result[0] =~ s/(\[|\s+)/$1\$/g;
$result .= $result[2] . $result[0];
$_ = $result[1];
} while (1);
END { printf qq|%s\n|, $result; }
' infile
It yields:
blah blah [$ae $b $c] blah [$zv $y] blah
sed 's/\[\([^]]*\)\]/[ \1]/g
:loop
s/\(\(\[[^]$]*\)\([[:blank:]]\)\)\([^][:blank:]$][^]]*\]\)/\1\$\4/g
t loop
s/\[ \([^]]*\)\]/[\1]/g' YourFile
posix version
assuming there is no bracket inside bracket like [a b[c] d ]
algo:
add a space char after opening bracket (needed to use blank as starting word separator an often no space for first one)
label anchor for a loop
add a $ in front of last word between bracket that does not have one (not starting by $). Do it for each bracket group in line, but 1 add per group only
if occuring, retry another time going to label loop
remove the first space added in first operation
This might work for you (GNU sed):
sed -r 'h;s/\</$/g;T;G;s/^/\n/;:a;s/\n[^[]*(\[[^]]*\])(.*\n)([^[]*)[^]]*\]/\3\1\n\2/;ta;s/\n(.*)\n(.*)/\2/' file
Make a copy of the current line. Insert $ infront of all start-of-word boundaries. If nothing is substituted print the current line and bale out. Otherwise append the copy of the unadulterated line and insert a newline at the start of the adulterated current line. Using substitution and pattern matching replace the parts of the line between [...] with the original matching parts using the newline to move the match forwards through the line. When all matches have been made replace the end of the original line and remove the newlines.

Perl conditional extraction of lines from file

Could anyone suggest me a method where I could extract a few lines of text while reading it.
file sample structure:
A blah blah string1
B blah blah
C blah string2
D blah string3 blah
E blah blah
F blah string2
G blah string3 blah
H blah blah string1
I blah blah
J blah string2
Here I want to extract lines starting with string "string1" followed/ended by "string2"
In effect I want lines A-C and H-J in the above example.
My experiments are failing with the presence of line F which I would want to ignore.
Perl one liner and the Flip-flop Operator ..:
$ perl -ne 'print if /\bstring1\b/ .. /\bstring2\b/' file
A blah blah string1
B blah blah
C blah string2
H blah blah string1
I blah blah
J blah string2
\b in the above regex is called word boundary. It matches between a word characetr and a non word character.
From Perl --help
-n assume "while (<>) { ... }" loop around program
-e program one line of program (several -e's allowed, omit programfile)
This can be done as well in awk and sed by indicating the patterns between which you want to print the lines:
sed -n '/string1/,/string2/p' file
awk '/string1/,/string2/' file
In Perl you can say:
perl -e 'while (<>){print if (/string1/../string2/);}' file
Which is equivalent to
perl -ne '{print if (/string1/../string2/)}' file
^
All of them return:
A blah blah string1
B blah blah
C blah string2
H blah blah string1
I blah blah
J blah string2

SED: Combine text between two pattern in same line

I m trying to use sed /awk command to parse my text file.
I want to print text between two pattern in same line with some character added in it.
ex:
If my line is
uint8_t aucRepresentationName[NR_UNIT_REPRESENTATIONS][CHARS_P_REPRESENTATION];
OR
uint8_t aucRepresentationName [NR_UNIT_REPRESENTATIONS] [CHARS_P_REPRESENTATION];
I want to print NR_UNIT_REPRESENTATIONS*CHARS_P_REPRESENTATION
So , I have stared with sed command to insert line deliminter \n after '[' , deviding line in suitable species and then tried to parse it again.
echo "bla bla bla [AK] bla bla bla bla [A_K] bla bla bla" | sed 's/\[/\n&/g;' | awk '{sub(/.*\[ /,"");sub(/\].*/,"");print;}'
echo "bla bla bla [AK] bla bla bla bla [A_K] bla bla bla" | sed 's/\[/\n&/g;' | sed -e 's/^.*\[ //g;s/ \].*$//g'
And it is not always two diamensional array, it could be none or single/double dimentional array, I need to first how many instance of [], if its 0 then write 1, if its single diamensional array then ex A[SIZE] then write SIZE its two diamensional array ex:A[RAW][COL] write RAW*COL..
I would like to know what is wrong in my command? or any other option to do it as it will help me to further study sed.
Amruta
Try:
awk -F '[][]' '{print $2,$4}' OFS=\* file
OK, if it is a single occurrence of 0,1 or two-dimensional in one page you could try:
awk -F '[][]' '{if(NF==3)print $2; else if(NF>4)print $2,$4; else print 1}' OFS=\* file
or less comprehensible ;) :
awk -F '[][]' '{$0=NF==3?$2:NF>4?$2 OFS $4:1}1' OFS=\* file
use this:
perl -lne 'push #a,/\[([^\]]*)\]/g;END{print join "*",#a}'
modified from here
tested below:
> echo "bla [AK] bla [A_K] 10" | perl -lne 'push #a,/\[([^\]]*)\]/g;END{print join "*",#a}'
AK*A_K
You can try the following:
echo "bla bla bla [AK] bla bla bla bla [A_K] bla bla bla" | sed 's/.*\[\(.*\)\].*\[\(.*\)\].*/\1*\2/g'
AK*A_K
With sed:
sed -n 's/[^]]*\[\([^]]*\)\]\s*\[\([^]]*\)\].*$/\1*\2/p' input

sed/awk + line manipulation

I have two different questions(on both questions need to ignore spaces)
How to print all lines until "=" separator for example
echo " bla bla girl man dog = black white color bla 123 4" | sed/awk .....
Will print:
bla bla girl man dog
the second question
How to print all lines from "=" until end of line
echo " bla bla girl man dof = black white color bla 123 4" | sed/awk .....
Will print
black white color bla 123 4
THX for help
Lidia
You can try this:
echo " bla bla girl man dog = black white color bla 123 4" | awk -F '=' '{print $1}'
cut can help you.
cut cuts a line using a custom delimiter, and gives you any parts around it.
echo " bla bla girl man dof = black white color bla 123 4" | cut -d= -f1 gives you the first part before a =
echo " bla bla girl man dof = black white color bla 123 4" | cut -d= -f2 gives you the second part after a =
if you have more than one = one the line -f2- will give you everything after the first = (ignoring the second =).
Answer to the first question:
sed 's/^ *\([^=]*\) *= *\(.*\) *$/\1/'
Answer to the second question:
sed 's/^ *\([^=]*\) *= *\(.*\) *$/\2/'
Explanation:
^ *\([^=]*\) *= *\(.*\) *$ matches the whole line. It contains two capturing groups: first group captures everything up to the first = except boundary spaces, second group captures everything after the first = except for boundary spaces. \1 and \2 are references to the capturing groups.
EDIT The above answer to the first question is incorrect, as it leaves trailing spaces. A correct version should read:
sed 's/^ *\([^=]*\) *= *\(.*\) *$/\1/' | sed 's/ *$//'
There seems to be no way to make the [^=]* reluctant, hence two sed commands.
For the first, use
cut -d= -f1
For the second, use
cut -d= -f2-
But these solutions preserve spaces.
With sed:
First question:
echo " bla bla girl man dog = black white color bla 123 4" | sed -n '{s/\([^=]*\)=.*/\1/p}'
Second question:
echo " bla bla girl man dog = black white color bla 123 4" | sed -n '{s/[^=]*=\(.*\)/\1/p}'
With awk:
First question:
echo " bla bla girl man dog = black white color bla 123 4" | awk -F= '{print $1}'
Second quetion:
if you have exactly one = in each line:
echo " bla bla girl man dog = black white color bla 123 4" | awk -F= '{print $2}'
Using cut
echo " bla bla girl man dog = black white color bla 123 4" | cut -d= -f1
With Atmark its much more easy:
first
# split = head trim_
second
# split = tail trim_

echo nested quotation marks in tcsh

I have a tcsh script that generates a text file. One of the lines in the text file is:
bla bla bla 'foo foo foo "bar bar bar"': etc etc;
Note the nested ' and " and also the : and ; that must be there.
The : and ; require the whole string to be surrounded by quotation marks. However, if I do that, I have trouble escaping the quotation marks.
The command is:
echo "bla bla bla 'foo foo foo "bar bar bar"': etc etc;" >> outfile
How can I escape the quotation marks around bar bar bar so that they get printed correctly?
echo "bla bla bla 'foo foo foo "\""bar bar bar"\""': etc etc;"
or this:
echo "bla bla bla 'foo foo foo "\"bar bar bar\""': etc etc;"
These should work for the simple example you gave, but may not help for what you're actually trying to do... Quoting in tcsh always annoyed me, especially when trying to define aliases with a mix of back-ticks, quotes, and double-qutes.
Be warned that the second form works for echo, but it actually creates three separate arguments on the command line, which are (after interpreting the escape sequences):
bla bla bla 'foo foo foo "bar
bar
bar"': etc etc;
The first form is the one you should use.