| UNIX/Linux Forum Archives · Rules and Guidelines · Disclaimer |
Help
Search
Members
Calendar
|
| Welcome Guest ( Log In | Register ) | Resend Validation Email |
![]() ![]() ![]() |
| eveaug |
|
|
Newbie ![]() Group: Members Posts: 1 Member No.: 977 Joined: 6-May 05 |
Hi
Using sed is it possible to copy a string from a line in a file to another location on the same line: Example file: aaa bbb ccc eee fff ggg to become aaa bbb ccc > ccc eee fff ggg > ggg ? Any help would be great Thanks |
| andyb1ack |
Posted: May 10 2005, 04:01 PM
|
||||||
|
User Level: 4 ![]() ![]() ![]() ![]() Group: Members Posts: 98 Member No.: 513 Joined: 9-September 04 |
Uh-oh. This isn't going to be easy to explain, but here's the code for your example:
I've split up each line into two parts separated by a space. . = any character * = 0 or more of the preceeding character hence .* = 0 or more of any character \(.*\) means save anything into named space 1 Repeat it with a space in between ie the \(.*\) \(.*\) means, put everything up to the last space into named space 1, and from the last space to the end of the line in named space 2. I could have done it the following way too:
Alternately, you could have used the following shell commands:
|
||||||
| vibhor_agarwalin |
Posted: May 11 2005, 02:47 PM
|
|
User Level: 3 ![]() ![]() ![]() Group: Members Posts: 34 Member No.: 788 Joined: 1-February 05 |
Hi Andy,
I remember you and your sed explanation. I am able to solve most of my problems with the sed, but i am a bit new to your while and read one. Can you help me a bit in that. |
| andyb1ack |
Posted: May 11 2005, 03:21 PM
|
||
|
User Level: 4 ![]() ![]() ![]() ![]() Group: Members Posts: 98 Member No.: 513 Joined: 9-September 04 |
The while read contruct is really useful. You can put configuration info into a file and have it read by a script. To change the script you just then update the config file, which is better practice than updating code. The important thing to ensure is that each line in the config file has the number of fields as you expect, or things start going a bit funny! Here's an example of a simple while read in action:
|
||
![]() |
![]() ![]() ![]() |