search pattern on sed or grep
Hi all, I'm very sorry to bother the list with this problem but I've been searching in the web the couples hours to find an answer and still haven't find any. The problem is that I have a txt file of 3.5GB containing all the info of Human chromosome 6. I want to save into one another file all lines that have the pattern rs10946398 (occurring only ones). I know that vi cannot handle files so big. I used ed in Fedora5 but this too cannot stream it. I hope that grep or sed can do this but cannot figure how to. I tried the following for sed but doesn't work: sed '/rs10946398/p' chr6.txt Can someone help? Thank in advance, Nikos
Re: search pattern on sed or grep
A.J.Mechelynck wrote: Nikolaos A. Patsopoulos wrote: Hi all, I'm very sorry to bother the list with this problem but I've been searching in the web the couples hours to find an answer and still haven't find any. The problem is that I have a txt file of 3.5GB containing all the info of Human chromosome 6. I want to save into one another file all lines that have the pattern rs10946398 (occurring only ones). I know that vi cannot handle files so big. I used ed in Fedora5 but this too cannot stream it. I hope that grep or sed can do this but cannot figure how to. I tried the following for sed but doesn't work: sed '/rs10946398/p' chr6.txt Can someone help? Thank in advance, Nikos grep rs10946398 chr6.txt chr6.extract.txt Grep is a filter, remember? It takes its input from stdin, writes its output on stdout, and the regexp is on the command-line. The output consists of all input lines matching the pattern. Since in this case the pattern is a fixed string, we can also use fgrep (or grep -F) instead of grep. Best regards, Tony. Thanks, I found also the solution with sed: sed -n '/rs10946398/p' chr6.txt o.txt Thanks, Nikos
Re: search pattern on sed or grep
Tim Chase wrote: grep 'rs10946498' chr6.txt | grep -v 'rs10946498.*rs10946498' out.txt Sed might allow it in one pass with something like sed -e '/rs10946398/!d' -e '/rs10946398.*rs10946398/d' chr6.txt out.txt Still try to migrate from Windows to linux, but hopefully will done it someday! Since you have a fixed pattern (as Tony mentioned about using fgrep), you can do at least the first variant in native windows/dos with C:\Temp find rs10946398 chr6.txt out.txt without the need for sed/grep at all. The dos find command is a bit like grep with all the cool functionality removed. The resulting file would hopefully be small enough that vim/ed could handle the resulting out.txt file. You can learn more by issuing C:\Temp find /? No patterns other than fixed text, but sometimes that's all you need. And 640k oughta be enough for anyone ;) -tim (now only running Windows at work, but Linux, OpenBSD and Mac OS X at home) I never thought of using find under cmd. I'm not very into computers but as long as I remember cmd (and DOS) generally can handle data of 640K at one batch meaning that it would need many hours (even days) to execute the above command for a 3.5GB file. Anyway a test is always better than a hypothesis, so I started a cmd prompt and run the code. I did this as soon as I got your e-mail. It now have passed 5-6 mins and still no result (I monitor the out.txt filesize as well). sed finished iin about 70secs, so probably cmd will take alot of hours. Nikos
Re: vim | delete consecutive occurrences of a pattern
A.J.Mechelynck wrote: Nikolaos A. Patsopoulos wrote: Tim Chase wrote: I have a text that has many occurrences of a pattern . I want to delete every consecutive occurrence, e.g. Pattern Pattern other text Pattern Pattern Pattern Pattern other text Pattern Pattern Pattern should look like this: Pattern other text Pattern other text Pattern I've used: :%s/\(Pattern\s\+\)\(Pattern\)/\1/g but have to run this more than once with: %g to result the wanted text. Can I do this with one command only? If not can I write a while function?: You seem to be close. The following did it for me, :%s/\(Pattern\)\(\s\+Pattern\)\+/\1/g or, if you're lazy, :%s/\(Pattern\)\(\s\+\1\)\+/\1/g (no need to type the Pattern a 2nd time) HTH, -tim I tried Jorgen' s code (all possible ways) but I still had to run the command more than once. Tim's worked great. I forgot to mention that pattern is a regexp, so Tony's couldn't be tested. Thank you all for your time. By the way, can someone explain if I could the while function??? Thanks, Nikos Of course you could: function RemoveDuplicates(pattern) range exe a:firstline while search(a:pattern . a:pattern, 'c', a:lastline) exe 's/\(' . a:pattern . '\)' . a:pattern . '/\1' endwhile endfunction command -range=% -bar -nargs=0 ScrapDup call RemoveDuplicates(q-args) The above will remove one duplicate at each iteration and exit when there are none left. IMHO a single substitute is more elegant though. Best regards, Tony. The single command it's more easier!!! I was just curious Thanks Tony, Nikos
Re: vim | delete consecutive occurrences of a pattern
Jόrgen Krδmer wrote: Hi, Nikolaos A. Patsopoulos wrote: Tim Chase wrote: I have a text that has many occurrences of a pattern . I want to delete every consecutive occurrence, e.g. Pattern Pattern other text Pattern Pattern Pattern Pattern other text Pattern Pattern Pattern should look like this: Pattern other text Pattern other text Pattern I've used: :%s/\(Pattern\s\+\)\(Pattern\)/\1/g but have to run this more than once with: %g to result the wanted text. Can I do this with one command only? If not can I write a while function?: You seem to be close. The following did it for me, :%s/\(Pattern\)\(\s\+Pattern\)\+/\1/g or, if you're lazy, :%s/\(Pattern\)\(\s\+\1\)\+/\1/g (no need to type the Pattern a 2nd time) I tried Jorgen' s code (all possible ways) but I still had to run the command more than once. strange, the only difference between Tim's and my versions where the \+ he used after the second parenthesis and the * I used. As both \+ and * are greedy, this should not make a difference for the final result, only maybe in speed. Just out of curiosity could you show me/us the exact command you used? Regards, Jόrgen I started copy-pasting the commands and realized that I used: :%s/\(Author .\{-}\/Author)\)\(\s\+Author .\{-}\/Author\)*/\1/g ^ [one extra ( ] instead of :%s/\(Author .\{-}\/Author\)\(\s\+Author .\{-}\/Author\)*/\1/g That's why the code failed. Sorry for my mistake. Nikos
join all lines inside pattern that occurs more than once
Hi, I want to join all lines that are inside a given pattern and occurs more than once in the text, ie: PatternStart text1 text2 ...text3 ..text4 PatternEnd ... ... PatternStart text1 text2 ...text3 ..text4 ... ...textn PatternEnd ... PatternStart text1text2...text3..text4PatternEnd PatternStart text1text2...text3..text4......textnPatternEnd I tried to use: :g/PatternStart\_.\{-}PatternEnd/ J but this joins only first and second line of the pattern. How can I tell vi to join all lines inside all occurrences of this pattern with variable containing lines? Thanks in advance, Nikos
Re: delete all but first occurence of a pattern
Tobia wrote: I don't think Vim's regular expressions are the best tool for this job. I mean, XML manipulation is much easier done in XSLT: ?xml version=1.0? xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform; xsl:template match=article xsl:copy xsl:copy-of select=@*/ AuthorList CompleteYN=Y Author ValidYN=Y xsl:copy-of select=AuthorList/Author[1]/*/ /Author /AuthorList xsl:copy-of select=node()[not(self::AuthorList)]/ /xsl:copy /xsl:template /xsl:stylesheet This does what you want in your example, assuming the source is a proper XML document (among other things there must be a root tag encompassing all the articles.) Invoke with xsltproc fix-authors.xsl articles.xml or with any other XSLT tool. To get back on-topic, I find these scripts make working with XSLT a bit less painful: xslhelper.vim http://www.vim.org/scripts/script.php?script_id=1364 closetag.vim http://www.vim.org/scripts/script.php?script_id=13 This, on the other hand, is on my list of things to check, but I still haven't got around to checking it out: xml.vim http://www.vim.org/scripts/script.php?script_id=1397 Tobia I'm currently looking for sth that will work with VI (I use vim script files). As last resort I'll try your suggestion though. Thank you for your reply, Nikos
delete all but first occurence of a pattern
Hi all, I've got xml files that each have 500 entries marked as article /article .in each entry there are fields author/author. Some articles have non author fields and some have more than one. What I want to do is every article to have only one such field, e.g. if an article has 3 author.. /author fields the two last must be dropped, if an article has none an empty one author /author must be added. How can i do this but in every 500 articles that are included in the same xml file? (I use VI, not gvim) Here follows some sample entries: article AuthorList CompleteYN=Y Author ValidYN=Y LastNameHeinisch/LastName ForeNameRoberto H/ForeName InitialsRH/Initials /Author Author ValidYN=Y LastNameZanetti/LastName ForeNameCarlos R/ForeName InitialsCR/Initials /Author Author ValidYN=Y LastNameComin/LastName ForeNameFabiano/ForeName InitialsF/Initials /Author Author ValidYN=Y LastNameFernandes/LastName ForeNameJuliano L/ForeName InitialsJL/Initials /Author Author ValidYN=Y LastNameRamires/LastName ForeNameJos#233; A/ForeName InitialsJA/Initials /Author Author ValidYN=Y LastNameSerrano/LastName ForeNameCarlos V/ForeName InitialsCV/Initials SuffixJr/Suffix /Author /AuthorList /article article /article article AuthorList CompleteYN=Y Author ValidYN=Y LastNameSaint-Remy/LastName ForeNameAnnie/ForeName InitialsA/Initials /Author /AuthorList /article The above sample should be turned into this: article AuthorList CompleteYN=Y Author ValidYN=Y LastNameHeinisch/LastName ForeNameRoberto H/ForeName InitialsRH/Initials /Author /AuthorList /article article AuthorList CompleteYN=Y Author ValidYN=Y /Author /AuthorList /article article AuthorList CompleteYN=Y Author ValidYN=Y LastNameSaint-Remy/LastName ForeNameAnnie/ForeName InitialsA/Initials /Author /AuthorList /article Thanks in advance, Nikos
grouping problem
Ηι, I'm want to group a text that has the following text pattern (values and number of lines vary across instances): PMID16893921/PMID Volume164/Volume Issue7/Issue Year2006/Year ISOAbbreviationAm. J. Epidemiol./ISOAbbreviation ArticleTitleImplications of small effect sizes of individual genetic variants on the design and interpretation of genetic association studies of complex diseases./ArticleTitle MedlinePgn609-14/MedlinePgn FirstAuthorIoannidis JP Used the following code but got a pattern recognition problem.: :%s/\(PMID.*$\)\(Volume.*$\)\(Issue.*$\)\(Year.*\)\(ISO.*$\)\(ArticleTitle\_.\{-}ArticleTitle\)\(MedlinePgn.*$\)\(FirstAuthor.*$\)/\1\6\5\4\2\3\7\8\g What am I missing?? Thanks in advance, Nikos
vim | deleting end of lines inside a pattern
Hi, I would like to delete all end of lines (\n) inside a given pattern that runs through a text. The pattern is like this: PubmedArticle text1 \n text2 \n text3 \n text4 \n text5 \n text6 \n ... \n PubmedArticle Any help? Thanks
vim | editing pdf files with vim
Hi all, is there a way to edit pdf files with vim? If not pdf as is, then eps or postscript? I tried with either format but the text kept been converted to sthl ike ASCII code. Thanks, Nikos
Re: vim | editing pdf files with vim
Mike Williams wrote: Nikolaos A. Patsopoulos did utter on 10/10/2006 14:58: Hi all, is there a way to edit pdf files with vim? If not pdf as is, then eps or postscript? I tried with either format but the text kept been converted to sthl ike ASCII code. In general trying to edit PDF or PostScript files is a non-starter, unless you have a lot of knowledge about how the files were constructed. In particular, text depends on the font encoding used. It will be much easier to go back to the original application that generated the PDF/PS and edit the document there. TTFN Mike You are 100% right. But what if you cannot access the original file? Lets say you want to edit a pdf you downloaded from internet. Isn't there a way to do this with vim? I haven't tried emacs or sed but probably they will fail too. I was just wondering if there some way out there. Nikos
vim | replacement question
Hi all! Another replacement question: how can I replace all occurrence of a pattern except a given one, e.g. the first or third? the code for all occurrences I use is: :%s/a.\{-}//g Thanks in advance, Nikos
Re: vim | replacement question
Yakov Lerner wrote: On 9/20/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Hi all! Another replacement question: how can I replace all occurrence of a pattern except a given one, e.g. the first or third? the code for all occurrences I use is: :%s/a.\{-}//g /pattern/+1,$s///g Yakov Thanks! Nikos
Re: vim | replacement question
A.J.Mechelynck wrote: Nikolaos A. Patsopoulos wrote: A.J.Mechelynck wrote: Nikolaos A. Patsopoulos wrote: Hi, another two questions: 1. I want to delete all text that has a specific pattern. I use the following code with s command but I want to keep the \a character in the beginning: :%s/\a,\_.\{-}\/td\/tr/ To delete everything that matches a certain pattern :%s/pattern//g (i.e., replace by nothing). To keep something at the start, see :help /\zs :help /\@= 2. how can I join lines that have non-numerical characters? e.g. 153 Purdue Canc Ct 1256 should be 153 Purdue Canc Ct 1256 Thanks in advance, Nikos (untested) :%g/\D.*\n.*\D/join i.e. join two successive lines, adding an intervening space, if there is at least one non-digit anywhere in each of them. Best regards, Tony. For 1 I came up with this: :%s/\(\a\),\_.\{-}\/td\/tr/\1 about 2 :%g/\D.*\n.*\D/join .* captures some numeric values in between. Maybe sth like this would be better: :%g/\D.*\n.*[^\d]\D/join but this is not right. Well, it all depends on what you want to do. If there are both digits and nondigits on a single line, do you want to join it or not? Or does it depend on whether the nondigits are or aren't whitespace? I would suggest that you read the helpfile :help pattern.txt and especially the part starting at :help pattern-overview and extending over 150 lines or more. Best regards, Well that worked for me fine: :%g/\D\n\D/join You are right. Everything depends on what you want to do. Another small question: If you want to an empty line to the end of a file what does the trick? I tried :,$s/\(.*\)/\1\n but doesn't work Thanks, Nikos
vim | multiple files editing and delete question
Hi, I have a series of questions: 1. I want to edit multiple files from command line so I created a vim script with all the commands (20). I use a batch file in WinXP: |@echo off vim -s script file.txt exit however I need to run this script on multiple files. In vim's help there is this code for use in bash(?) shell ||for file in *.txt; do| | vim -e -s $file change.vim| | lpr -r tempfile| |done however it doesn't seem to work under Cygwin. 2.Can I delete after a pattern search? Sth like this: :/^html\_.{-}body: /-3d and how can I repeat this globally? 3. This is not Vim related but I wonder if anyone knows sth. I have the following structure of folders and files: .. folder1 file1 file2 folder2 file1 file2 . and want to add the folder name into the filename: || folder1 ||folder1|_|file1 ||folder1|_|||file2 folder2 ||folder2|_|||file1 ||folder2|_|||file2 Thanks in advance, Nikos |
Re: vim | multiple files editing and delete question
Yakov Lerner wrote: On 9/14/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: 2.Can I delete after a pattern search? Sth like this: :/^html\_.{-}body: /-3d and how can I repeat this globally? :g/^html\_.{-}body: /.-3d Yakov I get an E16: invalid range error -- Nikolaos A. Patsopoulos, MD Department of Hygiene and Epidemiology University of Ioannina School of Medicine University Campus Ioannina 45110 Greece Tel: (+30) 26510-97804 mobile: +30 6972882016 Fax: (+30) 26510-97867 (care of Nikolaos A. Patsopoulos) e-mail: [EMAIL PROTECTED]
Re: vim | multiple files editing and delete question
Yakov Lerner wrote: On 9/14/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Yakov Lerner wrote: On 9/14/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Yakov Lerner wrote: On 9/14/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: 2.Can I delete after a pattern search? Sth like this: :/^html\_.{-}body: /-3d and how can I repeat this globally? :g/^html\_.{-}body: /.-3d Yakov I get an E16: invalid range error Yeah, this can happen if pattern is found in line number 4. (Then .-3 results in =0 which is invalid line number). Just insert 4 dummy empty lines at beginning of file to avoid this. Yakov This means that the -3d counts from the beginning of the pattern?? In my file The first occurrence of the patten expands from line 1 to line 82. Yes, from the beginning of the pattern Yakov How can I force it to delete 3 lines from the end? -- Nikolaos A. Patsopoulos, MD Department of Hygiene and Epidemiology University of Ioannina School of Medicine University Campus Ioannina 45110 Greece Tel: (+30) 26510-97804 mobile: +30 6972882016 Fax: (+30) 26510-97867 (care of Nikolaos A. Patsopoulos) e-mail: [EMAIL PROTECTED]
Re: vim | multiple files editing and delete question (bash script)
Yakov Lerner wrote: On 9/14/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Yakov Lerner wrote: On 9/14/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: 3. This is not Vim related but I wonder if anyone knows sth. I have the following structure of folders and files: .. folder1 file1 file2 folder2 file1 file2 . and want to add the folder name into the filename: || folder1 ||folder1|_|file1 ||folder1|_|||file2 folder2 ||folder2|_|||file1 ||folder2|_|||file2 Try this shell script: for file in folder*/*; do dir=$(basename $(dirname $file) ) base=$(basename $file) mv $file $(dirname $file)/${dir}_${base} done Untested! Yakov Bash result: script started, file is typescript. Nothing happens. Just an empty file called typescript appears in parent folder. Try on bash mailing list or bash irc chan. Yakov Thanks. I'll do that. Nikos
Re: vim | multiple files editing and delete question
Steve Hall wrote: On Thu, 2006-09-14 at 13:01 +0300, Nikolaos A. Patsopoulos wrote: 1. I want to edit multiple files from command line so I created a vim script with all the commands (20). I use a batch file in WinXP: |@echo off vim -s script file.txt exit however I need to run this script on multiple files. In vim's help there is this code for use in bash(?) shell ||for file in *.txt; do| | vim -e -s $file change.vim| | lpr -r tempfile| |done however it doesn't seem to work under Cygwin. To do an operation on multiple files in a WinXP DOS batch: for %%A in (*.txt) do [command] Chain multiple commands after the do statement with : for %%A in (*.txt) do vim -e -s change.vim %%A copy %%A lpt1 or call a separate batch that takes %1 as the argument: for %%A in (*.txt) do call MyEdit.bat %%A where MyEdit.bat is: @echo OFF echo File %1... vim -e -s change.vim %1 copy %1 lpt1 Note that I usually sprinkle double quotes liberally to avoid issues with spaces in paths. That works great!!! Thanks! Nikos
Re: vim | multiple files editing and delete question
Sibin P. Thomas wrote: If u have Cygwin then creating a batch file with the following would be the simplest solution - set TARGETDIR=C:\something set SCRIPTDIR=C:\something_else find %TARGETDIR% -name *.[ch] -exec gvim -s %SCRIPTDIR%\win32_vimscript.vim {} ; Basically use find to help u (actually it's just one instruction, so u don't even need to have a batch file for it) Regards, Sibin -Original Message- From: Nikolaos A. Patsopoulos [mailto:[EMAIL PROTECTED] Sent: Thursday, September 14, 2006 3:31 PM To: vim@vim.org Subject: vim | multiple files editing and delete question Hi, I have a series of questions: 1. I want to edit multiple files from command line so I created a vim script with all the commands (20). I use a batch file in WinXP: |@echo off vim -s script file.txt exit however I need to run this script on multiple files. In vim's help there is this code for use in bash(?) shell ||for file in *.txt; do| | vim -e -s $file change.vim| | lpr -r tempfile| |done however it doesn't seem to work under Cygwin. 2.Can I delete after a pattern search? Sth like this: :/^html\_.{-}body: /-3d and how can I repeat this globally? 3. This is not Vim related but I wonder if anyone knows sth. I have the following structure of folders and files: .. folder1 file1 file2 folder2 file1 file2 . and want to add the folder name into the filename: || folder1 ||folder1|_|file1 ||folder1|_|||file2 folder2 ||folder2|_|||file1 ||folder2|_|||file2 Thanks in advance, Nikos | - Disclaimer - This message(including attachment if any)is confidential and may be privileged.Before opening attachments please check them for viruses and defects.MindTree Consulting Private Limited (MindTree)will not be responsible for any viruses or defects or any forwarded attachments emanating either from within MindTree or outside.If you have received this message by mistake please notify the sender by return e-mail and delete this message from your system. Any unauthorized use or dissemination of this message in whole or in part is strictly prohibited. Please note that e-mails are susceptible to change and MindTree shall not be liable for any improper, untimely or incomplete transmission. - No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.1.405 / Virus Database: 268.12.3/447 - Release Date: 13/9/2006 Haven't tested your suggestion, but this works just fine: for file in folder */*; do dir=$(basename $(dirname $file)) base=$(basename $file) mv $file $(dirname $file)/${dir}_${base} done
vim | replacement question
Hi, another two questions: 1. I want to delete all text that has a specific pattern. I use the following code with s command but I want to keep the \a character in the beginning: :%s/\a,\_.\{-}\/td\/tr/ 2. how can I join lines that have non-numerical characters? e.g. 153 Purdue Canc Ct 1256 should be 153 Purdue Canc Ct 1256 Thanks in advance, Nikos
Re: vim | replacement question
A.J.Mechelynck wrote: Nikolaos A. Patsopoulos wrote: Hi, another two questions: 1. I want to delete all text that has a specific pattern. I use the following code with s command but I want to keep the \a character in the beginning: :%s/\a,\_.\{-}\/td\/tr/ To delete everything that matches a certain pattern :%s/pattern//g (i.e., replace by nothing). To keep something at the start, see :help /\zs :help /\@= 2. how can I join lines that have non-numerical characters? e.g. 153 Purdue Canc Ct 1256 should be 153 Purdue Canc Ct 1256 Thanks in advance, Nikos (untested) :%g/\D.*\n.*\D/join i.e. join two successive lines, adding an intervening space, if there is at least one non-digit anywhere in each of them. Best regards, Tony. For 1 I came up with this: :%s/\(\a\),\_.\{-}\/td\/tr/\1 about 2 :%g/\D.*\n.*\D/join .* captures some numeric values in between. Maybe sth like this would be better: :%g/\D.*\n.*[^\d]\D/join but this is not right. -- Nikolaos A. Patsopoulos, MD Department of Hygiene and Epidemiology University of Ioannina School of Medicine University Campus Ioannina 45110 Greece Tel: (+30) 26510-97804 mobile: +30 6972882016 Fax: (+30) 26510-97867 (care of Nikolaos A. Patsopoulos) e-mail: [EMAIL PROTECTED]
vim | insert filename into file
Hi, how can anyone add the filename in the file in ex-mode? C-R% and %p works only in normal mode Thanks, Nikos
Re: vim | insert filename into file
Jürgen Krämer wrote: Hi, Nikolaos A. Patsopoulos wrote: how can anyone add the filename in the file in ex-mode? C-R% and %p works only in normal mode :put % Regards, Jürgen That seems to insert the filename in random place. What if I want to place it e.g. in the beginning of each line?
Re: vim | insert filename into file
Tim Chase wrote: :put % That seems to insert the filename in random place. What if I want to place it e.g. in the beginning of each line? It inserts it on the line below where the cursor currently is. You can specify a target (as in :30put % which will put it below line #30) If you want it at the beginning of each line, you can use my aforementioned suggestion of :%s/^/\=expand('%').': ' (which also adds a colon and a space for visualization purposes, but is totally omittable if you want) -tim :%s/^/\=expand('%') works great! Thank you all
Deleting question
Hi, I'm trying to delete several lines from the beginning of file till the appearance of a specific pattern, without deleting the pattern. I have used the following command: :1,/Citations: /d/e-10 but the offset doesn't work. Thanks in advance, Nikos
Re: Deleting question
Yakov Lerner wrote: On 9/13/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Hi, I'm trying to delete several lines from the beginning of file till the appearance of a specific pattern, without deleting the pattern. I have used the following command: :1,/Citations: /d/e-10 but the offset doesn't work. Try this: :1,/Citations: /-1d Caution: This works except in the case when pattern is found in the 1st line. Yakov That's work fine, thanks! Can I ask another question? How can someone substitute or delete a block of text which expands to more than one line? E.g. the text: Morning bgfn nbgfnmore text.. more text... ... end of text. Can I use a sth like this? s/Morning,text/anothertext
Re: Deleting question
Yakov Lerner wrote: On 9/13/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Yakov Lerner wrote: On 9/13/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Hi, I'm trying to delete several lines from the beginning of file till the appearance of a specific pattern, without deleting the pattern. I have used the following command: :1,/Citations: /d/e-10 but the offset doesn't work. Try this: :1,/Citations: /-1d Caution: This works except in the case when pattern is found in the 1st line. Yakov That's work fine, thanks! Can I ask another question? How can someone substitute or delete a block of text which expands to more than one line? E.g. the text: Morning bgfn nbgfnmore text.. more text... ... end of text. Can I use a sth like this? s/Morning,text/anothertext try :s/Morning\_.*text/anothertext/ or :s/Morning\(\n\|.\)*text/anothertext/ :help \_ Yakov Works great!
Re: Deleting question
Yakov Lerner wrote: On 9/13/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Yakov Lerner wrote: On 9/13/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Hi, I'm trying to delete several lines from the beginning of file till the appearance of a specific pattern, without deleting the pattern. I have used the following command: :1,/Citations: /d/e-10 but the offset doesn't work. Try this: :1,/Citations: /-1d Caution: This works except in the case when pattern is found in the 1st line. Yakov That's work fine, thanks! Can I ask another question? How can someone substitute or delete a block of text which expands to more than one line? E.g. the text: Morning bgfn nbgfnmore text.. more text... ... end of text. Can I use a sth like this? s/Morning,text/anothertext try :s/Morning\_.*text/anothertext/ or :s/Morning\(\n\|.\)*text/anothertext/ :help \_ Yakov Sorry, that works but I forgot to mention that I have many occurrences of /text/ (last word in block) and I want to substitute to the first occurrence only.
Re: Deleting question
A.J.Mechelynck wrote: Nikolaos A. Patsopoulos wrote: Hi, I'm trying to delete several lines from the beginning of file till the appearance of a specific pattern, without deleting the pattern. I have used the following command: :1,/Citations: /d/e-10 but the offset doesn't work. Thanks in advance, Nikos Well, I suppose gg/Citations: / would put the cursor on the first occurrence of the pattern from the start of the file (assuming that it isn't at the very start of the file, in which case there's nothing to do). (a) if the pattern is at the start of a line: :1,.-1d (b) otherwise hdgg will delete everything preceding it to the start of the file. Breakup of the commands: :ex-command 1,from first line .-1to current line minus 1 ddelete lines (the :d[elete] command) hleft one character dggdelete to start of file. ( d{motion} is a Normal-mode command and gg moves to start of file ) That's for doing it manually, on one file, at the command-line. Now let's assume you wanted to do it on all *.txt files in the current directory and everywhere below it. You could then first source the following scriptlet: function DeletePreamble(pattern) search from and including start-of-file normal gg let matchline = search(a:pattern, 'c') if not found, there's nothing to do if matchline == 0 return endif pattern was found in line(matchline) the cursor is now on the first char of the match if col('.') == 1 match found in column 1 if matchline == 1 match at start of file: nothing to do return else delete all preceding lines (linewise) 1,.-1d endif else match not at start of line delete from preceding character to start of file (characterwise) normal hdgg endif endfunction and then call it as: :args ./**/*.txt :argdo call DeletePreamble('Citations: ') | update Best regards, Tony. That looks great!!! I'll take some time to study it thoroughly. Thanks!!
Re: Deleting question
Yakov Lerner wrote: On 9/13/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Yakov Lerner wrote: On 9/13/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Yakov Lerner wrote: On 9/13/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Hi, I'm trying to delete several lines from the beginning of file till the appearance of a specific pattern, without deleting the pattern. I have used the following command: :1,/Citations: /d/e-10 but the offset doesn't work. Try this: :1,/Citations: /-1d Caution: This works except in the case when pattern is found in the 1st line. Yakov That's work fine, thanks! Can I ask another question? How can someone substitute or delete a block of text which expands to more than one line? E.g. the text: Morning bgfn nbgfnmore text.. more text... ... end of text. Can I use a sth like this? s/Morning,text/anothertext try :s/Morning\_.*text/anothertext/ or :s/Morning\(\n\|.\)*text/anothertext/ :help \_ Yakov Sorry, that works but I forgot to mention that I have many occurrences of /text/ (last word in block) and I want to substitute to the first occurrence only. Then you change * in the pattern for \{-} Yakov It's OK now. -- Nikolaos A. Patsopoulos, MD Department of Hygiene and Epidemiology University of Ioannina School of Medicine University Campus Ioannina 45110 Greece Tel: (+30) 26510-97804 mobile: +30 6972882016 Fax: (+30) 26510-97867 (care of Nikolaos A. Patsopoulos) e-mail: [EMAIL PROTECTED]
Re: Deleting question
Yakov Lerner wrote: On 9/13/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Yakov Lerner wrote: On 9/13/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Yakov Lerner wrote: On 9/13/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Hi, I'm trying to delete several lines from the beginning of file till the appearance of a specific pattern, without deleting the pattern. I have used the following command: :1,/Citations: /d/e-10 but the offset doesn't work. Try this: :1,/Citations: /-1d Caution: This works except in the case when pattern is found in the 1st line. Yakov That's work fine, thanks! Can I ask another question? How can someone substitute or delete a block of text which expands to more than one line? E.g. the text: Morning bgfn nbgfnmore text.. more text... ... end of text. Can I use a sth like this? s/Morning,text/anothertext try :s/Morning\_.*text/anothertext/ or :s/Morning\(\n\|.\)*text/anothertext/ :help \_ Yakov Sorry, that works but I forgot to mention that I have many occurrences of /text/ (last word in block) and I want to substitute to the first occurrence only. Then you change * in the pattern for \{-} Yakov this seems like a never-ending question: what if I have more than of the same blocks? How can I use the sub command to replace all occurrences of the same block in a file?
Re: Deleting question
A.J.Mechelynck wrote: Nikolaos A. Patsopoulos wrote: Yakov Lerner wrote: On 9/13/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Yakov Lerner wrote: On 9/13/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Yakov Lerner wrote: On 9/13/06, Nikolaos A. Patsopoulos [EMAIL PROTECTED] wrote: Hi, I'm trying to delete several lines from the beginning of file till the appearance of a specific pattern, without deleting the pattern. I have used the following command: :1,/Citations: /d/e-10 but the offset doesn't work. Try this: :1,/Citations: /-1d Caution: This works except in the case when pattern is found in the 1st line. Yakov That's work fine, thanks! Can I ask another question? How can someone substitute or delete a block of text which expands to more than one line? E.g. the text: Morning bgfn nbgfnmore text.. more text... ... end of text. Can I use a sth like this? s/Morning,text/anothertext try :s/Morning\_.*text/anothertext/ or :s/Morning\(\n\|.\)*text/anothertext/ :help \_ Yakov Sorry, that works but I forgot to mention that I have many occurrences of /text/ (last word in block) and I want to substitute to the first occurrence only. Then you change * in the pattern for \{-} Yakov this seems like a never-ending question: what if I have more than of the same blocks? How can I use the sub command to replace all occurrences of the same block in a file? To replace pattern1 by text2 everywhere in the file (including several times on a line): :1,$s/pattern1/text2/g 1,$ is a range of lines (in this case: line 1 to last line) see :help :s :help [range] Best regards, Tony. I used :%s/\(a href\_.\{-}\Source:\)/bSource:/g (a_href is the beginning of the block and Source is the end) but all the occurrences of the block are mixed up, since the code is been repeated till the block is not found, while it should stop at the first pass of the whole file.
vim | cut paste in ex mode
Hi, is there a way to yank in ex-mode? I want to find all \d\d\d\dnbsp; in my file and put them in the end of their containing line. Thanks in advance, Nikos
Re: vim | cut paste in ex mode
Tim Chase wrote: is there a way to yank in ex-mode? I want to find all \d\d\d\dnbsp; in my file and put them in the end of their containing line. 1) yes, you can yank in ex-mode, though it usually happens in line-wise fashion, unless you pull some stunts to prevent that: :10y will yank line #10 into the default register. :let @a=''|g/regexp/y A will pull all the lines matching regexp into register a To do just piecewise bits into a register, you can use :let @a=''|g/regexp1/let @A=substitute(getline('.', '^.\{-}\(regexp2\).*$', '\1', '') which is ugly and hackish, but will pull one piece matching regexp2 from lines matching regexp1 and tack them into register a (you can append .'\n' IIRC, to put each piece on its own line in the register) 2) If you just want to move that pattern to the end of the line, you can use :%s/\(\d\d\d\dnbsp;\)\(.*\)/\2\1 If you want to copy (rather than move) the contents to the end of the line, you can use :%s/\(\d\d\d\dnbsp;\).*/\1 If you want extra space between what was formerly the EOL and the added text, just add a space before the \1 Just a few ideas... -tim :%s/\(\d\d\d\dnbsp;\)\(.*\)/\2\1 works fine. Thanks!
vim | reformatting question
Hi, I'm trying to fix a file in the following format : 5,329; 1999; a Univ Washington/a; 695; 2001; NHLBI; 684; 1998; a Stanford Univ/a; 3,537; 1998; La Jolla Inst Allergy Immunol; 3,333; 1996; a BRIGHAM WOMENS HOSP/a; 2,967; 1998; a Northwestern Univ/a; 2,936; 2002; a NHLBI/a; 2,695; 1996; a ST JUDE CHILDRENS HOSP/a; 2,581; 1998; a Merck Res Labs/a; into this: 5,329;1999;a Univ Washington/a; 695;2001;NHLBI; 684;1998;a Stanford Univ/a; 3,537;1998;La Jolla Inst Allergy Immunol; 3,333;1996;a BRIGHAM WOMENS HOSP/a; etc... I came up with this but sth is missing... :%s/\(^\d,\d\d\d;$\|^\d\d\d;$\)\(^\d\d\d\d;$\)\(^\D\+;$\)/\1\2\3
Re: vim | reformatting question
Max Dyckhoff wrote: It seems like you are just trying to join groups of three lines, which you can do very easily using this following command: :g/./j!3 Hope that helps! Max -Original Message- From: Nikolaos A. Patsopoulos [mailto:[EMAIL PROTECTED] Sent: Wednesday, September 13, 2006 8:48 AM To: vim@vim.org Subject: vim | reformatting question Hi, I'm trying to fix a file in the following format : 5,329; 1999; a Univ Washington/a; 695; 2001; NHLBI; 684; 1998; a Stanford Univ/a; 3,537; 1998; La Jolla Inst Allergy Immunol; 3,333; 1996; a BRIGHAM WOMENS HOSP/a; 2,967; 1998; a Northwestern Univ/a; 2,936; 2002; a NHLBI/a; 2,695; 1996; a ST JUDE CHILDRENS HOSP/a; 2,581; 1998; a Merck Res Labs/a; into this: 5,329;1999;a Univ Washington/a; 695;2001;NHLBI; 684;1998;a Stanford Univ/a; 3,537;1998;La Jolla Inst Allergy Immunol; 3,333;1996;a BRIGHAM WOMENS HOSP/a; etc... I came up with this but sth is missing... :%s/\(^\d,\d\d\d;$\|^\d\d\d;$\)\(^\d\d\d\d;$\)\(^\D\+;$\)/\1\2\3 Your code and Tim's do exactly what I want to do. Thanks! -- Nikolaos A. Patsopoulos, MD Department of Hygiene and Epidemiology University of Ioannina School of Medicine University Campus Ioannina 45110 Greece Tel: (+30) 26510-97804 mobile: +30 6972882016 Fax: (+30) 26510-97867 (care of Nikolaos A. Patsopoulos) e-mail: [EMAIL PROTECTED]
html file to cvs
Hi all, I have the following problem: I have a huge pack of html files (1000) and I want to extract some info on cvs files. The html source looks like this: ./code / bSource:/b/code/ 338 (13): 853-860 MAR 26 1998nbsp;/code /bAddresses:/b/code/ a href=http:./code/ Northwestern Univ,/a/code /the above block is repeated =20 times. I want a cvs file that will look like this: 1998;Northwestern Univ; 1998;ETc; I tried few things but I cannot reach a working code. One of main issues is how discriminate years from 4digit pages, e.g 1987. I have attached a html source file (since it is impossible to cut paste the whole code here) but I've got a failure notice. I have published a html file in the following address: http://users.uoi.gr/npatsop/portal_002.htm Thanks in advance, Nikos -- Nikolaos A. Patsopoulos, MD Department of Hygiene and Epidemiology University of Ioannina School of Medicine University Campus Ioannina 45110 Greece Tel: (+30) 26510-97804 mobile: +30 6972882016 Fax: (+30) 26510-97867 (care of Nikolaos A. Patsopoulos) e-mail: [EMAIL PROTECTED]
Re: html file to cvs
Devin Weaver wrote: I don't fully understand what you mean by a cvs file whether that refers to a congruent visioning file or if you meant a comma separated values file. Based on the sample output I'm assuming a CSV file using semi-colons. I choose PERL at the Swiss-Army knife of scripts and was able to whip up a parser in about fifteen minutes. attached is what I came up with. I left the loading of multiple files to the student. I used mainly regular expressions so it could be ported to VIM script in theory but this type of parsing would be better suited for a scripting language not an editor. Hope this gives some inspiration. On Sep 6, 2006, at 06:14, Nikolaos A. Patsopoulos wrote: I have a huge pack of html files (1000) and I want to extract some info on cvs files. #!/usr/bin/perl # Very simple script to parse a specific styled HTML document and output a file # parsed with a delimiter. # # The folowing are the settings. Pick what you need. Using command line # arguments left for the student. $file = portal_002.htm; $output = out.csv; $csv_delim = ';'; $quiet = 0; # set this to 1 to stop debug output $months_pat = (JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC); ## sub msg { my $str = shift; my $line_no = shift; if (!$quiet) { print $str; if ($line_no ne ) { print (line: $line_no); } print \n; } } $line_no = 0; # used to track the line number. open FD, $file || die Could not open file; open OUT, $output || die Unable to open output file; while ($line = FD) { $line_no++; if ($line =~ /Source:/i) { $line =~ /$months_pat\s+[0-9]+\s+([0-9]+)/i; $year = $2; msg (Found 'Source:'; Year = $year, $line_no); } elsif ($line =~ /Addresses:/i) { $line =~ /a(\s.+?)?(.+?)\/a/i; $univ = $2; $univ =~ s/^\s+//; $univ =~ s/(\s+|[,;])$//; # pull out the HTML amp; $univ =~ s/amp;//gi; msg ( Child Found 'Addresses:'; Univ = $univ, $line_no); # Since this should be the end of the record write to file. print OUT $year$csv_delim$univ$csv_delim\n; } } close OUT; close FD; msg (Done. (Parsed $line_no lines) CSV output to $output, ); No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.1.405 / Virus Database: 268.11.7/438 - Release Date: 5/9/2006 Thanks for the time and effort. I work on WinXP machine and cannot brag for my Perl knowledge. From the very few code I can understand it seems that you are close to what I want to do but much are missing. I'm sorry but I'm unable to follow a Perl script. Thanks, Nikos
Re: VIM script replacement question
Alan G Isaac wrote: On Thu, 25 May 2006, Nikolaos A. Patsopoulos apparently wrote: In detail: 1.I want in front of the number in the first column to add # , then change line after the value 2. change line after 3rd column 3. change line after 5th column 4. repeat all three steps %s/^\(\d\+\)\s\+\([01]\)\s\+\([01]\)\s\+\([01]\)\s\+\([01]\)\s*/#\1\r\2 \3\r\4 \5 hth, Alan Isaac Thanks for the answer,but sth is not working right. My output is this: 171100 180000 191111 201000 211101 221000 230100 #24 1 1 1 0 251111 260000 270000 The replacement didn't occur to the whole file. Thanks, Nikos
Re: VIM script replacement question
Alan G Isaac wrote: On Thu, 25 May 2006, Nikolaos A. Patsopoulos apparently wrote: The replacement didn't occur to the whole file. You must have forgotten the '%'. hth, Alan Isaac No, I used %. Got them same problem with Tim's code :(
Re: VIM script replacement question
Alan G Isaac wrote: On Thu, 25 May 2006, Nikolaos A. Patsopoulos apparently wrote: No, I used %. Got them same problem with Tim's code Something is not right ... Try using :g/./s/ instead of :%s/ and see what happens. hth, Alan Isaac Works great! Million thanks! Nikos
Re: VIM script replacement question
Alan G Isaac wrote: On Thu, 25 May 2006, Nikolaos A. Patsopoulos apparently wrote: No, I used %. Got them same problem with Tim's code Something is not right ... Try using :g/./s/ instead of :%s/ and see what happens. hth, Alan Isaac One last question: I get this: #3 00 00 #4 11 10 #5 11 00 How I can put spaces between numbers in same rows? 00 must become0 0 Don't want space after #3 though. Thanks, Nikos
Re: VIM script replacement question
Alan G Isaac wrote: On Thu, 25 May 2006, Nikolaos A. Patsopoulos apparently wrote: 00 must become 0 0 The original replacement I sent had these spaces in it: :g/./s/^\(\d\+\)\s\+\([01]\)\s\+\([01]\)\s\+\([01]\)\s\+\([01]\)\s*/#\1\r\2 \3\r\4 \5 Look after \2 and after \4 hth, Alan Isaac Sth went wrong during copypaste. Added the spaces manually and everything is ok. Thanks, Nikos