How remove text wrap using vim text editor? - regex

I'm trying to write a vim script for remove the text wrap, I am using the following code but it's doesn't provide exact output. eg \string{this indicate newline} if "this" appears in first line, "indicate" is in second line e.t.c then how I remove text wrap. Is it possible?
:%s/\\string{\zs\(\_[^}]*\)\ze}/\1/gec
Edit based on OP's comment:
for example (i/p): \string{1 <enterkey> 2 <enterkey> 3 <enterkey>
4 <enterkey> 5 <enterkey>}. i need (o/p) \string{1 2 3 4 5}.
Before I have:
\string{1
2
3
4
5
}
After I want:
\string{1 2 3 4 5}
Before I have (new pattern):
\string{1
{2}
{3}
4
5}
After I want:
\string{1 {2} {3} 4 5}

This line does what you want:
%s/\\string{\_[^}]*/\=substitute(submatch(0),"\n",' ','g')/
it changes:
foobar
\string{1
2
3
4
5
}
foobar
into:
foobar
\string{1 2 3 4 5 }
foobar

It would be easier to understand your question if you gave a longer example of text, and what you want to do with it. If I understand correctly, you could like to remove the line wrap on lines that contain \\string{this.
You could use :%g/\\string{this/j. It executes the j command on every line matching the \\string{this pattern.
Input:
some text
\string{this
indicate}
more text
Turns into:
some text
\string{this indicate}
more text

Related

Using awk, how do I match pattern and variants?

I've been struggling with this for a while in regex testers but what came up as a correct regex pattern actually failed. I've got a large file, tab delimited, with numerous types of data. I want to print a specific column, with the characters XYZ, and it's subsequent values.
In the specific column I'm interested in I have values like:
XYZ
ABCDE
XYZ/WORDS
XYZ/ABCDE
ABFE
XYZ
regex tester that was successful was something like:
XYZ(.....)*
It obviously fails when implemented as:
awk '{if ($1=="XYZ(......)*") print$0}'
What regex character do I use to denote that I want everything after the backslash(/), including the original pattern (XYZ)?
Specifically, I want to be able to capture all instances of XYZ, and print the other columns that go along with them (hence the print$0). Specifically, capture these values:
XYZ
XYZ/WORDS
XYZ/ABCDE
Thank you
Setup: (assuming actual data file does not include blank lines)
$ cat x
XYZ 1 2 3 4
ABCDE 1 2 3 4
XYZ/WORDS 1 2 3 4
XYZ/ABCDE 1 2 3 4
ABFE 1 2 3 4
XYZ 1 2 3 4
If you merely want to print all rows where the first field starts with XYZ:
$ awk '$1 ~ /^XYZ/' x
XYZ 1 2 3 4
XYZ/WORDS 1 2 3 4
XYZ/ABCDE 1 2 3 4
XYZ 1 2 3 4
If this doesn't provide the expected results then please update the question with more details (to include a more representative set of input data and the expected output).

Find and KEEP all DUPLICATE lines (instead of unique lines) in a text file

I am aiming to identify and keep DUPLICATE, TRIPLICATE, etc. lines, i.e., all lines that occur more than once in Notepad++? In other words, how can I delete all unique lines only?
For example, here are seven (7) separate lists and the desired true duplicate lines of each lists (shown as 7 columns, regard each column as an individual list or file!). (The lists here are shown side by side only to save space, in real life, each of the 7 lists occurs alone and independently from the others and are separate files!)
list1 list2 list3 list4 list5 list6 list7
1 0 0 0 0 0 0
2 1 1 1 1 1 1
3 2 2 2 2 2 2
4 3 3 3 3 3 3
4 4 4 4 4 4 4
4 4 4 4 4 4 4
5 4 4 4 4 4 4
6 5 5 5 5 5 5
7 5 5 5 5 5 5
8 6 6 6 6 6 6
9 6 6 6 6 6 6
abc 7 7 7 7 7 7
abd 8 8 8 8 8 8
abd 9 9 9 9 9 9
abe <CR> 9 9 9 9
<CR> 99 99
<CR>
[Lines of multiple occurence of above lists:]
4 4 4 4 4 4 4
4 4 4 4 4 4 4
4 4 4 4 4 4 4
abd 5 5 5 5 5 5
abd 5 5 5 5 5 5
6 6 6 6 6 6
6 6 6 6 6 6
9 9 9 9
9 9 9 9
There are many solutions to eliminate duplicates (e.g., TextFX; notepad++ delete duplicate and original lines to keep unique lines), I can not find solutions to keep duplicates only.
((.*)\R(\2\R)+)*\K.+\R
#Lars Fischer: This script works nearly OK, except the last entry of the (presorted) list needs to be unique line followed by a <CR> empty line. One (suboptimal) workaround is to insert an artificial (helper) unique line (e.g., zzz) followed by an empty line <CR> as the last two lines.
(END OF QUESTION)
UPDATE 3: This question is reposted per stackoverflow "ask a new question" instruction. (#AdrianHHH, #B. Desai, #Paolo Forgia, #greg-449, #Erik von Asmuth draw the incorrect conclusion that this question is a duplicate of notepad++ delete duplicate and original lines to keep unique lines. This question is definitely not a duplicate of the one #AdrianHHH et al quotes.
UPDATE 2: #AdrianHHH This question is not less "broad" (in fact, one can hardly be more specific) or less researched than other Notepad++ questions, including the one https://stackoverflow.com/questions/29303148 cited (wrongly) by #AdrianHHH et al. as the same question.
UPDATE:
#AdrianHHH, #B. Desai, #Paolo Forgia, #greg-449, #Erik von Asmuth
This questions is different from:
https://stackoverflow.com/questions/29303148
beacuse Q 29303148 is (i) neither asking how to identify and keep only the lines of multiple occurrence, (ii) neither there is a solution provided in the answers for that. Q 29303148 asks "...I just need the unique lines."
Here is a solution based on regular Expressions and bookmarks, it works for a sorted file (i.e. each duplicated line is followed by its duplicates):
Open the Mark Dialog (Search -> Mark ....)
click Clear all Marks on the right
check Bookmark line
check Wrap aound
Find What: ((.*)\R(\2\R?)+)*\K.*
Check regular expression and uncheck . matches newline
Mark All
Click Close
Search -> Bookmark -> Remove Bookmarked Lines
Explanation
The regular expression is made up of three parts:
((.*)\R(\2\R?)+)* : this is an optional block of duplicates consisting of one ore more line blocks
the outher ( ... )* matches zero or more such blocks of duplicated lines (if in your example the three 4 would be followed by two 5 we will need a concept of sequences of duplicate blocks)
(.*)\R(\2\R?)+: \2 references the content of (.*): this are all duplicates of one line
the second \R is an optional ( due to the ?) linebreak. Thus it is possible to match a duplicate in the last line of the file if that line does not end with a linebreak
If there is a block of duplicated lines after the cursor position from which you start, this will match it.
now \K discards what we have matched so far (the duplicates) and "puts the cursor" before the first unique line
.* matches the next (unique) line and bookmarks it
Using Mark All we bookmark all such unique lines, so that we can remove them using the Entry from the Search -> Bookmark menu.

Bring all rows to one row

In Excel, I have rows like below:
1 2 3 4 5
6 7 8 9 0
9 8 7 6 5
...
I need to bring all of them to the first row:
1 2 3 4 5 6 7 8 9 0 9 8 7 6 5 ...
The numbers of rows and columns are fixed.
What is the fastest way I can achieve this?
Alternatively, can I solve this on a Textpad or Notepad++ using some REGEX grouping?
If you wanted to do it with an Excel formula, pasting the following, starting in column F, would produce it across the top row:
=INDIRECT("r"&CEILING(COLUMN()/5,1)&"c"&IF(MOD(COLUMN(),5)=0,5,MOD(COLUMN(),5)),FALSE)
If your table started from A2 and your row values are to be copied from A1 onwards, following should work:
=OFFSET($A$2, (COLUMN()-COLUMN($A$1))/5, MOD(COLUMN()-COLUMN($A$1), 5))
However, I think for just a small table of size 5X10, using '=' sign manually would be the fastest.
I just select and drag them up there. When there was a pattern of projects I wrote a VBA function to do the job, but for most small unique projects select and drag worked for me.
In Notepad++, for Find what : \r\n, Replace with : 'space' with Search Mode Extended, Replace All, then copy result into Excel.
With images :) here:
Replace Carriage Return and Line Feed in Notepad++.

Replace 2nd to last line -1 in vi

I am trying to replace the content of 2nd to last-1 line in sed. But I cannot get the addressing correct.
for example: in a file
1
2
3
4
5
I want to do:
1
2,
3,
4,
5
In vi it has to be something like: 2,$-1s/$/,/ but $-1 wont work. please suggest.
This might work for you:
seq 5 | sed '1b;$b;s/$/,/'
1
2,
3,
4,
5
you are close. you need a 'g' at the end of 's' commdn. in vim:
:2,$-1 s/$/,/g
will get the thing done.

Highlight columns which differer from previous lines

I have text file with numerical data written in lines; there are interspersed input lines (unknowns) and residuals lines (which are supposed to be minimized). I am investigating ways how the iterative solver handles various cases, and would like to highlight every (space-delimited) field in the residuals line which is (textually) different from the same field in the previous residuals line (2 lines above, better given by a regexp). I am free to decorate beginnings of the lines as I like, if that helps.
Is this at all possible with Vim and some regexp magic?
Example file:
input 1 2 3 4 5 6
errors .2 .2 .3 .1 0 0
input 1 2.1 2.9 4 5 6 ## here, 2.1 and 2.9 should be highlighted
errors .21 .3 .44 .3 0 0
input 1 2 3 3.9 5.2 6 ## here, 2, 3, 3.9 and 5.2 should be highlighted
errors .2 .2 .34 .9 1 0
Note: I could code script extracting differences in Python, but I want to have a look at both the actual data and the changes. When it does not work with Vim, I will process it with python and output highlighted HTML, but I will lose automatic folding capabilities.
I think that if you use arrays rather than a regex for the comparison, it might be a lot easier:
<?php
$lastRow=array()
$h=fopen('file','r');
while ($r=fgetcsv($h,0,' ')) // Retrieve a line into an array and split on spaces
{
if (count($lastRow)==0)
{
$lastRow=$r; // Store last line
}
$count++;
if ($r[0]=='input')
{
/*
* this won't find any differences the first run through
*/
$diffs=array_diff($lastRow,$r); // Anything that has changed from the last "input" line is now in $diffs
$lastRow=$r; // Only copy into $lastRow if it's an "input" line
}
/*
* Put your code to output line here with relevant CSS for highlighting
*/
}
fclose($h);
?>
I've not tested this code but I think it's shows how you could get a solution without delving into regex