remove portion of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove portion of file
# 8  
Old 04-06-2002
Quote:
I believe there is even a way to write all of these vi commands in a file and execute them while you are vi'ing the file.
I know this is straying from the original topic a little, but I wanted to say that the functionality mentioned by Kelam_Magnus is really cool Smilie and powerful. The command is ! and like many vi commands, is followed by a movement to indicate how much text to operate on. In this case, the defined amount of text is passed to the OS for processing, and all the passed text is replaced by the output of the processing.

Say you have a paragraph of plain text comments, and some lines are too long, some too short. Put your cursor at start of paragraph and type !} which says process all text thru EOP, and when prompted at the colon prompt, type adjust or adjust -m66, and the text will be replaced by the output of /bin/adjust.

Or create a script called addhori.sh:
awk '{printf "%9d%9d%9d%9d\n",$1,$2,$3,$1+$2+$3}'

In vi, place your cursor on the first of the four lines:

5 5 5
3 4 5
22 22 22
9 9 9

and this time let's process the current line plus next 3 lines: !3j
and at the colon prompt, type: addhori.sh

The four lines will be replaced with the awk output, in this case it will be the same 4 lines but formatted and with a total column added. It does not have to be line-per-line replacement. All lines could be replaced with a single line, or the 4 lines above could become the same 4 lines plus a total line below them (addvert.sh), each line could become two lines, whatever.

And of course, just type "u" to undo.

And for added functionality, some of the scripts I write for vi external processing utilize passed parameters.

Last edited by Jimbo; 04-07-2002 at 10:22 AM..
Jimbo
# 9  
Old 04-06-2002
Thanks for all the help Smilie it really works slick!
# 10  
Old 04-08-2002
I always take compliments!

Thanks for the kudos!

Yes, I am a firm believer that vi is greatly underused and vastly misunderstood as a tool for Admins.

Everyone should have a required course in vi. You can run scripts from within a file that you are editing, go and vi another file and copy in the changed file and then :wq! the one you modified! then save the changes that you copied into the original file.

VI is awesome!
# 11  
Old 08-15-2002
Question

Thanks to everyone for all the help. I do have a few questions. I am using the csplit command such as the following

csplit -f small.head -n 4 MOORDERS '%^COHEAD%' '/^COHEAD/' '{4}'

My problem is that I don't know how many "COHEAD" there will be in the file, sometime just one, other times many more. How can I get around having to specifiy how many time it should repeat? (i.e. "4")

Also, the file I am testing has six "COHEAD" in it and in order to get them all into separate files I have to set the count to 4, why is this and not 6?

Thanks, Methos
# 12  
Old 08-15-2002
Hammer & Screwdriver

The -n option on csplit is for the following:

-n number
Use number decimal digits to form filenames for the
file pieces. The default is 2.


RTM - it works.
# 13  
Old 08-15-2002
Thanks, but I don't quite understand what you mean. The "4" I am referring is the one at the end of the statement. When I change this value, it outputs more or less files, that is unless I am completely off the mark.

Is there some wild card I can use to make it more flexible?

Thanks
# 14  
Old 08-16-2002
I got it to where I can pass in a variable, still don't know why it is less than the number of occurances though. thanks everyone.

csplit -f small.head -n 4 MOORDERS '%^COHEAD%' '/^COHEAD/' {$1}


C-Ya, Methos
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to append portion of a file content to another file when a certain pattern is matching?

Hi ladies and gentleman.. I have two text file with me. I need to replace one of the file content to another file if one both files have a matching pattern. Example: text1.txt: ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF text2.txt: XXXX,ABCD... (25 Replies)
Discussion started by: bananamen
25 Replies

2. Shell Programming and Scripting

Unix Scripting : Sort a Portion of a File and not the complete file

Need to sort a portion of a file in a Alphabetical Order. Example : The user adam is not sorted and the user should get sorted. I don't want the complete file to get sorted. Currently All_users.txt contains the following lines. ############## # ARS USERS ############## mike, Mike... (6 Replies)
Discussion started by: evrurs
6 Replies

3. UNIX for Advanced & Expert Users

Removing portion of file name

Hi , I am getting file name like ABC_DATA_CUSTIOMERS_20120617.dat ABC_DATA_PRODUCTS_20120617.dat Need to convert CUSTIOMERS.dat PRODUCTS.dat Help me how to do this. (7 Replies)
Discussion started by: reach_malu
7 Replies

4. Shell Programming and Scripting

remove large portion of web page code between two tags

Hi everybody, I am trying to remove bunch of lines from web pages between two tags: one is <h1> and the other is <table it looks like <h1>Anniversary cards roses</h1> many lines here <table summary="Free anniversary greeting cards." cellspacing="8" cellpadding="8" width="70%">my goal... (5 Replies)
Discussion started by: georgi58
5 Replies

5. UNIX for Dummies Questions & Answers

Remove the date portion from file name

hi, I am trying to remove the last field before the period (.) from a list of file names in a directory in a shell script. Below is the list of file names. ENVID_archival_20120214092258.log ENVID_Get_Source_Files_20120214091828.log ENVID_Get_Source_Files_20120214092523.log... (6 Replies)
Discussion started by: Vijay81
6 Replies

6. Shell Programming and Scripting

Remove first portion of string

I have a script which currently uses a file containing a list of directories as an argument. The file is read in to an array, and then the array is iterated in a for loop. What I would like to do is cut off the first few directories of the directory path (they won't exist on the server where the... (5 Replies)
Discussion started by: msarro
5 Replies

7. UNIX for Dummies Questions & Answers

Portion of a file in a new files

Hi, I need to devide one file into 3 files based on column numbers and put a string (FILE1, FILE2, FILE3) in the last..... Input file: Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8,Column9,Column10 Output1: Column1,Column3,Column6,Column4,Column5,FILE1 Output2:... (6 Replies)
Discussion started by: yale_work
6 Replies

8. Shell Programming and Scripting

Grep certain portion from the file

Dear Friends, Here I am with another difficulty. I have a flat file from which I wanna grep following pattern. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Statement Date : Blah blah Blah blah Blah blah Blah blah... (1 Reply)
Discussion started by: anushree.a
1 Replies

9. UNIX for Dummies Questions & Answers

Print a portion of file

Hi, I have a little problem. I am having a file with pattern like : asdf;ffgg;dfjfj;djdfjf;nnjj;djd;ssj; I just want to print the portion from last ";" upto the immediate previous ";". There are several ";" in my line. Please help me out... Thnx in advance (8 Replies)
Discussion started by: vanand420
8 Replies

10. Programming

Delete Portion of a file

hi i would like to know whether i can delete a part of a file in C for eg. if my file contained 1234567890 and i want to delete 456 so that it becomes 1237890 is there a way i can do this. well, one way i can achieve this is by creating a new file, copy whatever i want, then delete the... (2 Replies)
Discussion started by: sameersbn
2 Replies
Login or Register to Ask a Question