ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
B0 49.2230869 -102.5743868
C 1LPRK 654.90
C 1PREU 3693.90
C 1JRSU 3693.90
C 1UVNG 3693.90
C 1LVGD 3924.90
C 1USHV 4064.00
C 1LRSV 4120.10
C 1GVBG 4180.10
C 1LGVB 4270.00
C 1WTRS 4310.00
C 1SPRF 4482.90
C 1MLKR 1912.10
C 1PLZU 4728.00
C 1MSSU 4728.00
C 1PPLR 4728.00
C 1RCLF 4768.00
C 1ONGR 4821.90
C 1MIDL 4857.90
C 1CLRD 2519.00
C 1SSPK 2756.00
C 1LCLD 2853.00
C 1BFS 2995.10
C 1VKNG 3097.00
C 1JLFU 3157.20
C 1MNVL 3346.10
I need to insert text ABCDEF starting at position 26 if a line starts with C.
I need it to look like:
C 1VKNG ABCDEF 3097.00
C 1JLFU ABCDEF 3157.20
C 1MNVL ABCDEF 3346.10
Any help would be appreciated. I would prefer to use sed to do this and not perl.
The file above does not look right. It lost the formatting when i saved the post.
I want to search a file for a line that starts with C. on this line I want to insert ABCDEF into column 26 - 31. All the other text on the line needs to remain untouched.
This works if C is the first character of the line. Otherwise use CODE tags - as suggested above - to show the exact structure of the file. In any case you got the idea, didn't you?
Might need OP to do the code tag thing as I am a bit confused on whether the data already has multiple spaces that are to be replaced or if the original file is as shown but
only the new data should contain spaces (as per my solution)??
@colucix: I do believe this is a file with fixed width columns (your solution moves everything after the substitution to the right.
Good point! I tried your solution but it inserts ABCDEF starting from the 30th character. Maybe something like this works:
Code:
sed -r '/^C/s/^(.{25}).{6}/\1ABCDEF/' file
The interval {25,30} actually means the preceding regexp is repeated 25 to 30 times, hence if there are more than 25 characters in the line they will be catched until the 30th.
You're welcome. But just out of curiosity, can you post a sample of the input file using CODE tags? So the ABCDEF string is inserted and the rest of the line is shifted to the right?
Good point! I tried your solution but it inserts ABCDEF starting from the 30th character. Maybe something like this works:
Code:
sed -r '/^C/s/^(.{25}).{6}/\1ABCDEF/' file
The interval {25,30} actually means the preceding regexp is repeated 25 to 30 times, hence if there are more than 25 characters in the line they will be catched until the 30th.
ROFL!! You are correct One of those times I don't actually test my stuff and I get immediately slapped on the wrist
Quote:
Originally Posted by Mecalith
sed -r '/^C/s/^(.{25})/\1ABCDEF/' infile
This works great.
Are you sure? Look at colucix last reply (post #12)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.