Input File
AAAAAA this is some content.
This is AAAAAA some more content BBBBBB. BBBBBB BBBBBB
This is yet AAAAAA some more BBBBBB BBBBBB BBBBBB content.
I can accomplish this partially with this code:
awk '/AAAAAA/{gsub("AAAAAA", "x"++i)}1' test.txt > test1.txt
awk '{for(x=1;x<=NF;x++)if($x~/BBBBBB/){sub(/BBBBBB/,"y"++i)}}1' test1.txt
Output:
x1 this is some content.
This is x2 some more content y1. y2 y3
This is yet x3 some more y4 y5 y6 content.
Anyway to get this output?
Expected Output:
x1 this is some content.
This is x2 some more content y1. y2 y3
This is yet x3 some more y1 y2 y3 content.
another one
$ awk '{sub("AAAAAA","x"(++x)); y=0; while(sub("BBBBBB","y"(++y)));}1' file
x1 this is some content.
This is x2 some more content y1. y2 y3
This is yet x3 some more y1 y2 y3 content.
You may use this single awk:
awk '{
j=0
for (x=1; x<=NF; x++)
if ($x ~ /^A{6}/)
sub(/^A{6}/, "x" (++i), $x)
else if ($x ~ /^B{6}/)
sub(/^B{6}/, "y" (++j), $x)
} 1' file
x1 this is some content.
This is x2 some more content y1. y2 y3
This is yet x3 some more y1 y2 y3 content.
Just need to reset i=0 after each loop:
awk '/AAAAAA/{gsub("AAAAAA", "x"++i)}1' test.txt > test1.txt
awk '{for(x=1;x<=NF;x++)if($x~/BBBBBB/){sub(/BBBBBB/,"y"++i)}{i=0}}1' test1.txt
Output:
x1 this is some content.
This is x2 some more content y1. y2 y3
This is yet x3 some more y1 y2 y3 content.
Here is an alternate awk that is easily extended to add as many tags as you wish:
awk 'BEGIN{ rep["AAAAAA"]="x"; cnts["AAAAAA"]=1; reset["AAAAAA"]=0
rep["BBBBBB"]="y"; cnts["BBBBBB"]=1; reset["BBBBBB"]=1
# and so on...
}
{
for (e in rep) {
cnts[e]=(reset[e]) ? reset[e] : cnts[e]
while ( sub(e,rep[e] (cnts[e]++) ) )
; # empty statement since work is inside the while
}
} 1' file
Prints:
x1 this is some content.
This is x2 some more content y1. y2 y3
This is yet x3 some more y1 y2 y3 content.
Related
I have files that look similar to this:
LET: DimX (2660.0)
LET: DimZ (1050.0)
LET: DimS (0.8)
LET: DREHEN (20)
DIM: X DimX
Z DimZ+0.5
S DimS
REF: X1 FOD-23.24
X2 FOD-23.24
Z1 FOD-24.86
Z2 FOD-24.86
POS: CENT_FUNC 1
QSU 10 QSD 10
TURN_AROUND
And like this - note the STAINLESS in the 2nd one:
LET: DimX (2660.0)
LET: DimZ (1050.0)
LET: DimS (0.7)
LET: DREHEN (20)
DIM: X DimX
Z DimZ+0.5
S DimS
STAINLESS
REF: X1 FOD-23.24
X2 FOD-23.24
Z1 FOD-24.86
Z2 FOD-24.86
POS: CENT_FUNC 1
QSU 10 QSD 10
TURN_AROUND
I want to save the value in Brackets in the line matching
LET: DimS (XX)
and insert a new line
MAT: 'XX correction' - where XX is the save value.
after S DimS
But only if the file whole doesn't contain the string STAINLESS.
So this should be the outcome for the 1st example:
LET: DimX (2660.0)
LET: DimZ (1050.0)
LET: DimS (0.8)
LET: DREHEN (20)
DIM: X DimX
Z DimZ+0.5
S DimS
MAT: '0.8 correction'
REF: X1 FOD-23.24
X2 FOD-23.24
Z1 FOD-24.86
Z2 FOD-24.86
POS: CENT_FUNC 1
QSU 10 QSD 10
TURN_AROUND
Outcome of the 2nd example should stay as it was as it contains STAINLESS:
LET: DimX (2660.0)
LET: DimZ (1050.0)
LET: DimS (0.7)
LET: DREHEN (20)
DIM: X DimX
Z DimZ+0.5
S DimS
STAINLESS
REF: X1 FOD-23.24
X2 FOD-23.24
Z1 FOD-24.86
Z2 FOD-24.86
POS: CENT_FUNC 1
QSU 10 QSD 10
TURN_AROUND
I've tried this to add the line after S DimS pattern:
sed -i -E '/S DimS/I a \\t MAT: "'"0.8 correction"'"'
But that just gives me:
LET: DimX (2660.0)
LET: DimZ (1050.0)
LET: DimS (0.8)
LET: DREHEN (20)
DIM: X DimX
Z DimZ+0.5
S DimS
MAT: "0.8 correction"
REF: X1 FOD-23.24
X2 FOD-23.24
Z1 FOD-24.86
Z2 FOD-24.86
POS: CENT_FUNC 1
QSU 10 QSD 10
TURN_AROUND
And:
LET: DimX (2660.0)
LET: DimZ (1050.0)
LET: DimS (0.7)
LET: DREHEN (20)
DIM: X DimX
Z DimZ+0.5
S DimS
MAT: "0.8 correction"
STAINLESS
REF: X1 FOD-23.24
X2 FOD-23.24
Z1 FOD-24.86
Z2 FOD-24.86
POS: CENT_FUNC 1
QSU 10 QSD 10
TURN_AROUND
And obvioulsy won't save the value in the brackets...
Can anyone help me please?
Thank you.
If you're amenable to an awk solution, which is often easier than sed when there's logic to perform beyond simple string manipulation ...
# Capture the correction amount
/LET: DimS/ { correction = $3; gsub(/[()]/, "", correction) }
# Get ready to print
$1 == "DIM:" { f = 1 }
# Abort print!!
$1 == "STAINLESS" { f = 0 }
# Now is the time to print the extra line if the flag is still set
NF == 0 && f { printf " MAT: '%s correction'\n", correction; f = 0 }
# Output the original lines of the file
{ print }
Test first example:
$ awk -f a.awk file
LET: DimX (2660.0)
LET: DimZ (1050.0)
LET: DimS (0.8)
LET: DREHEN (20)
DIM: X DimX
Z DimZ+0.5
S DimS
MAT: '0.8 correction'
REF: X1 FOD-23.24
X2 FOD-23.24
Z1 FOD-24.86
Z2 FOD-24.86
POS: CENT_FUNC 1
QSU 10 QSD 10
TURN_AROUND
Try it with your second "STAINLESS" example and you'll see that the extra line is not printed.
This might work for you (GNU sed):
sed -E ':a;N;$!ba;/STAINLESS/b;
s/^(.*LET:\sDimS\s*\(([^)]*).*\n(\s*)S DimS[^\n]*\n)/\1\3MAT: '\''\2 correction'\''/' file
Gather up the entire file into memory and test it to see if it contains the word STAINLESS. If so, bail out otherwise: using pattern matching insert the required text.
Using the Eigen3/C++ Library, given a MatrixXd
/ x0 ... y0 \
| x1 ... y1 |
M = | ... ... ... |
| |
\ xN ... yN /
what is the fasted method to achieve a modified version as shown below?
/ x0 * y0 ... y0 \
| x1 * y1 ... y1 |
M' = | ... ... ... |
| |
\ xN * yN ... yN /
That is, one column (the one with the x-s) is replaced by itself
multiplied with another column (that with the y-s).
do you mean how to coefficient-wise assign-multiply the first and last column vectors ? there are many ways of doing it, but the easiest/fastest might be
Eigen::MatrixXd M2 = M;
M2.leftCols<1>().array() *= M2.rightCols<1>().array();
an alternative might be constructing an uninitialized matrix with a given number of rows/cols and then block-assign like
Eigen::MatrixXd M2{ M.rows(), M.cols() };
M2.rightCols( M.cols() - 1 ) = M.rightCols( M.cols() - 1 );
M2.leftCols<1>() = M.leftCols<1>().cwiseProduct( M.rightCols<1>() );
which is faster I don't know ( but your preferred profiler does ).
for future questions, here is the official Eigen quick reference ;)
I am reading two different files with for loop. First "for loop" stops after first iteration. The print output is only line1 of f1 with all lines of f2 but then exit the loop.
for line1 in f1:
line1 = line1.split('\t')
for line2 in f2:
line2 = line2.split('\t')
print line1,line2
f1:
x1
x2
x3
f2:
y1
y2
y3
output:
x1 y1
x1 y2
x1 y3
x2 y1
x2 y2
x2 y3
x3 y1
x3 y2
x3 y3
Your loops are currently nested, which means that your program will read the entire contents of f2 for every line in f1. but once the end of file 2 is reached (at the end of the first outer look there are no more lines in f2 to read. so we manually reset the cursor to the beginning.
Attempt 4:
You were not resetting the cursor on file 2 once you got to the end of the file the first time round, unless you reopen the file in each iteration you must move the cursor to the beginning manually.
If I have now understood you correctly:
def print_both(f1, f2):
f1.seek(0)
f2.seek(0)
for line1 in f1:
line1 = line1.split('\t')
for line2 in f2:
line2 = line2.split('\t')
print(line1, line2)
f2.seek(0)
print_both(open("f1.tsv", 'r'), open("f2.tsv", 'r'))
I need to extract some info from a txt file which looks like this using regexp:
##FileName = disp_20120803_064635_1
#Plane1
x1 = 10008 x2= -9991 x3= -9991
y1 = 137 y2 = 10 y3 = 158
z1= 844 z2= 779 z3 = 700
#Plane2
x1 = -16 x2= 193 x3= 320
y1 = -4472 y2 = -556 y3 = 5143
z1= 3215 z2= -1309 z3 = 370
#Plane3
x1 = -8145 x2= 5387 x3= 8070
y1 = -4808 y2 = 7643 y3 = 3051
z1= 4212 z2= 4120 z3 = -4176
##end
I want to extract the file name by the following code:
buffer = fileread('test.txt') ;
pattern = '##FileName\s=\s+(\w+?\d+)';
tokens = regexp(buffer, pattern, 'tokens');
fileName = [tokens{:}]
But the result is just disp_20120803 which is not the complete file name?
Any help?
Use this pattern instead:
pattern = '##FileName\s=\s+(\w+)';
Edit:
I don't know matlab syntax but you can use the following regex to capture the variables name and their values:
pattern = '([xyz][123])\s*=\s*(-?\d+)'
The variable name is in group 1 and its value in group 2.
I would like to add a term after an nth term with the previous term substituted in. For instance, how would I change the following in notpad++:
x1 y1 z1 a1 b1 c1
x2 y2 z2 a2 b2 c2
x3 y3 z3 a3 b3 c3
to
x1 y1 z1 ["z1"] a1 b1 c1
x2 y2 z2 ["z2"] a2 b2 c2
x3 y3 z3 ["z3"] a3 b3 c3
where x, y, z, a, b and c are strings seperated by spaces.
another example:
apples bananas pears grapes oranges lemons
to
apples bananas pears grapes fruit:(grapes) oranges lemons
and so on.
Suppose you have one group that matches your elements, for example [1-9] and there is another group, that matches the separator between your elements, for example [\,\.], then you can write the following
([1-9][\,\.]){n}([\,\.])([1-9][\,\.])*
This will match the first n elements and the separator after it.
You can then use the matched pattern to substitute the content of the second match with your values.
Is that is something you're looking for?
in the find put...
(\w\s\w\s\w\s)
in the replace put
\1["z"]
See this question for more info.
NotePad++ replace problem
I guess this would also make sense for the find...
(x[ ]y[ ]z[ ])
for your example... if it was the 3rd item it would be
find:
(\w\s\w\s)(\w)
replace
\1\2 fruit(\2)