OT: Calling unix experts, Need "vi" assistance |
|
Porsche, and the Porsche crest are registered trademarks of Dr. Ing. h.c. F. Porsche AG.
This site is not affiliated with Porsche in any way. Its only purpose is to provide an online forum for car enthusiasts. All other trademarks are property of their respective owners. |
|
OT: Calling unix experts, Need "vi" assistance |
Rusty |
Aug 9 2004, 09:18 AM
Post
#1
|
Wanted: Engine case GA003709 Group: Admin Posts: 7,952 Joined: 24-December 02 From: North Alabama Member No.: 6 Region Association: Galt's Gulch |
Sorry for the OT post.
I have a text file that I need to edit. I need to edit every record on that file (record positions 34-37) to change whatever is in those positions (only) to a four digit number. What is currently in those positions varies dramatically. My only text editor I have on the system is vi. Anyone got a command I can use? thanks, Lawrence |
fiid |
Aug 9 2004, 09:46 AM
Post
#2
|
Turbo Megasquirted Subaru Member Group: Members Posts: 2,827 Joined: 7-April 03 From: San Francisco, CA Member No.: 530 Region Association: Northern California |
hey,
It sounds like you need to make this edit on a lot of lines? You might be better off using a small SED or AWK (or perl if you have it) script to do the job. Another option is simply download the file, and use something on a box with more tools on it. Post some more details and I can help you get a script together., Fiid. |
thomasotten |
Aug 9 2004, 10:04 AM
Post
#3
|
Senior Member Group: Members Posts: 1,552 Joined: 16-November 03 From: San Antonio, Texas Member No.: 1,349 |
upload the text into a database like mysql, if you have it. Then use sql to change it. it would be quicker than trying to figure out awk and sed. I hate those tools.
|
sgomes |
Aug 9 2004, 10:05 AM
Post
#4
|
Electric Member Group: Members Posts: 815 Joined: 6-May 04 From: Campbell, CA Member No.: 2,029 |
Wow! Two things:
1) vi is still getting discussion! (IMG:style_emoticons/default/smilie_pokal.gif) 2) It's getting discussion on a 914 website! (IMG:style_emoticons/default/blink.gif) |
mikester |
Aug 9 2004, 10:07 AM
Post
#5
|
Member Group: Members Posts: 326 Joined: 18-June 03 From: CA Member No.: 837 |
If the records were all the same it would be easy in vi but since they aren't...that's another story.
Can you give me a sample line from the file? Fake data is fine - just show me the format. |
vortrex |
Aug 9 2004, 10:09 AM
Post
#6
|
Senior Member Group: Members Posts: 1,687 Joined: 24-December 02 From: SF, CA Member No.: 4 Region Association: None |
are you using real vi, as in on a unix station of some sorts?
|
Rusty |
Aug 9 2004, 10:17 AM
Post
#7
|
Wanted: Engine case GA003709 Group: Admin Posts: 7,952 Joined: 24-December 02 From: North Alabama Member No.: 6 Region Association: Galt's Gulch |
Here's a sample record:
000104659 W91XLM40940026 GSA00001000011BA042200418504208W91XLM40940026XXXBY I need to replace the four characters after BA0... "4220". It shouldn't matter, but that's a julian date. Unfortunately, the only option I have is to use the existing system (with real VI, on a stripped down version of Solaris), unless I want to use get/dosput to bring them over to my Windows system. I dunno copying them back will screw up their formatting. thanks for the input (no pun intended!) |
Root_Werks |
Aug 9 2004, 10:39 AM
Post
#8
|
Village Idiot Group: Members Posts: 8,416 Joined: 25-May 04 From: About 5NM from Canada Member No.: 2,105 Region Association: Pacific Northwest |
(IMG:style_emoticons/default/idea.gif) Oh man, it has been a while. But even Solaris should have sed:
sed /BOA2440/BAO9999/g Is that it? Man, I need to fire up my linux box and play around some again. First string you want to look for, second one whatever you want to change it to and the "g" for global. (IMG:style_emoticons/default/wacko.gif) I think? |
Rusty |
Aug 9 2004, 10:48 AM
Post
#9
|
Wanted: Engine case GA003709 Group: Admin Posts: 7,952 Joined: 24-December 02 From: North Alabama Member No.: 6 Region Association: Galt's Gulch |
The BA0 only varies a bit. I could run several swaps if it was that simple.
However, the 4220 is a date, and I have a range in that field from 3351 to 4222. P.S. I'm working with ~2000 records. I'm not good enough with vi (or fast enough) to edit each record individually. I'm trying to make this little program do something it's not designed to do. |
thomasotten |
Aug 9 2004, 10:54 AM
Post
#10
|
Senior Member Group: Members Posts: 1,552 Joined: 16-November 03 From: San Antonio, Texas Member No.: 1,349 |
Ok, how about writting a shell script that reads every line, then prints out the concatination of the prefix string, with your replacement string, then the remainder string
I just tried it: test.sh ---------------------------------- s="000104659 W91XLM40940026 GSA00001000011BA042200418504208W91XLM40940026XXXBY" echo ${s:0:42}"REPL"${s:46} ------------------------------------------ run it by typing "sh test.sh" "REPL" above is the replacement text Now you just need to make the script read the file, and do this same operation on every line. |
kafermeister |
Aug 9 2004, 11:00 AM
Post
#11
|
Senior Member Group: Members Posts: 778 Joined: 20-January 03 From: Cincinnati/Northern KY Member No.: 174 |
Ok, Don't laugh. The last time I ran into something like this, I was using SecureCRT to console into a Linux box. I copied the contents of the file (although not 2000 lines) from SecureCRT into Notepad and did a replace all. I backed up my original file on the Linux machine and opened it. Deleted all lines (holding 'd' for many minutes), went into insert mode and pasted the whole shebang from Notepad once it was modified. I didn't want to re-create the file on the Linux box because of possible ownership and permissions issues.
Sorry, I love Unix like OS but I put food on the table supporting Windows. HTH Rick |
Rusty |
Aug 9 2004, 11:01 AM
Post
#12
|
Wanted: Engine case GA003709 Group: Admin Posts: 7,952 Joined: 24-December 02 From: North Alabama Member No.: 6 Region Association: Galt's Gulch |
Thomas,
The rest of the data is different in every record. Will the script wildcard the rest of the record? -Lawrence |
thomasotten |
Aug 9 2004, 11:04 AM
Post
#13
|
Senior Member Group: Members Posts: 1,552 Joined: 16-November 03 From: San Antonio, Texas Member No.: 1,349 |
yeah, it is just looking for the same position. It is counting chars up to that point, poping in your replacement, then tagging on the remainder string. Make yourself that test.sh file like I showed, and try to run it to see.
|
mikester |
Aug 9 2004, 11:18 AM
Post
#14
|
Member Group: Members Posts: 326 Joined: 18-June 03 From: CA Member No.: 837 |
The simplest way to do this is in vi.
If it's only dates between 3351 to 4222 then maybe you could try the following. in vi type the following; :%s/3351/newstring/g Then press enter, that will replace all of the 3351's in the file with the new string - if 3351 occurs anywhere else in the line though it will be replaced as well. You could do this for each date you want to change - depending on the number of entries it's relatively quick. YMMV (IMG:style_emoticons/default/beerchug.gif) |
tat2dphreak |
Aug 9 2004, 11:56 AM
Post
#15
|
stoya, stoya, stoya Group: Benefactors Posts: 8,797 Joined: 6-June 03 From: Wylie, TX Member No.: 792 Region Association: Southwest Region |
ok, if this is on a windows machine that you are telenting or connecting to UNIX through:
threre is a great program called Ultraedit... you can edit all the lines at once... if not you can do something like this as well for line in " sect_1=$(echo $line |cut -c 1-41) old_data=$(echo $line |cut -c 42-46) sect_3=$(echo $line |cut -c 47-) new_data='4225' print "${sect_1}${new_data}${sect_3}\n" done >> new_file_name |
fiid |
Aug 9 2004, 12:16 PM
Post
#16
|
Turbo Megasquirted Subaru Member Group: Members Posts: 2,827 Joined: 7-April 03 From: San Francisco, CA Member No.: 530 Region Association: Northern California |
QUOTE(Root_Werks @ Aug 9 2004, 08:39 AM) (IMG:style_emoticons/default/idea.gif) Oh man, it has been a while. But even Solaris should have sed: sed /BOA2440/BAO9999/g Is that it? Man, I need to fire up my linux box and play around some again. First string you want to look for, second one whatever you want to change it to and the "g" for global. (IMG:style_emoticons/default/wacko.gif) I think? you can change this to match any 4 numbers by doing something like: /BAO[0-9]{4,4}/BAOxxxx/g (You can probably refine this a bit - read the man page or appropriate o'reilly book) Fiid. |
opera guy |
Aug 9 2004, 12:47 PM
Post
#17
|
Member Group: Members Posts: 337 Joined: 28-June 04 From: upland, ca Member No.: 2,268 |
holy crap, all the unix hackers are coming out of the woodworks in the 914 forum. (IMG:style_emoticons/default/smilie_pokal.gif) (IMG:style_emoticons/default/smilie_pokal.gif)
|
Root_Werks |
Aug 9 2004, 02:23 PM
Post
#18
|
Village Idiot Group: Members Posts: 8,416 Joined: 25-May 04 From: About 5NM from Canada Member No.: 2,105 Region Association: Pacific Northwest |
QUOTE(fiid @ Aug 9 2004, 10:16 AM) QUOTE(Root_Werks @ Aug 9 2004, 08:39 AM) (IMG:style_emoticons/default/idea.gif) Oh man, it has been a while. But even Solaris should have sed: sed /BOA2440/BAO9999/g Is that it? Man, I need to fire up my linux box and play around some again. First string you want to look for, second one whatever you want to change it to and the "g" for global. (IMG:style_emoticons/default/wacko.gif) I think? you can change this to match any 4 numbers by doing something like: /BAO[0-9]{4,4}/BAOxxxx/g (You can probably refine this a bit - read the man page or appropriate o'reilly book) Fiid. Yep, that just triggered my memory! (IMG:style_emoticons/default/biggrin.gif) So should we think about making a "914 in a Nutshell"? (IMG:style_emoticons/default/boldblue.gif) |
fiid |
Aug 9 2004, 04:24 PM
Post
#19
|
Turbo Megasquirted Subaru Member Group: Members Posts: 2,827 Joined: 7-April 03 From: San Francisco, CA Member No.: 530 Region Association: Northern California |
We should start a 914 Wiki is what we should do.
|
Gint |
Aug 9 2004, 05:15 PM
Post
#20
|
Mike Ginter Group: Admin Posts: 16,093 Joined: 26-December 02 From: Denver CO. Member No.: 20 Region Association: Rocky Mountains |
Get this figured out yet? Here's a very simple solution and explanation. This assumes that each line only contains one "BA0...." string. Otherwise we'll have to use some of the positional commands some of these other "hacks" (and I mean that in the nicest way possible (IMG:style_emoticons/default/rolleyes.gif) ) listed. Also, all of this changes if the old replacement string has a data value that needs to relate to the replacement string. If the replacement string for every line will be the same, then it's this simple. If not, we have to be more creative.
cat tmpfile | sed 's/BA0..../BAO1234/g' > tmpfile_new cat your file (tmpfile in this example), pipe it to sed. the "s" means substitute. Your sub-ing BA.... (that's four dots, or wildcards for one character each in regular expression speak) for the char set infollowing the next forward slash. In this case you would want to replace them with the sam BA0 and then the 4 character string you want to use as a replacement string. I used 1234 in this example. The > re-directs the new text to a new file, leaving the existing file intact. You can then rename the new file as necessary. If you just run this cat tmpfile | sed 's/BA0..../BAO1234/g' without the file re-direct, you can see the output on your terminal display and verify it's what you want before creating the new file. This should work on any Solaris system (I tested it on one to make certain). The systems standard path should have the location of sed and cat. |
Lo-Fi Version | Time is now: 24th September 2024 - 01:01 PM |
All rights reserved 914World.com © since 2002 |
914World.com is the fastest growing online 914 community! We have it all, classifieds, events, forums, vendors, parts, autocross, racing, technical articles, events calendar, newsletter, restoration, gallery, archives, history and more for your Porsche 914 ... |