LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed to search and insert column (https://www.linuxquestions.org/questions/programming-9/sed-to-search-and-insert-column-864770/)

Mecalith 02-24-2011 09:26 AM

sed to search and insert column
 
I have the following file.

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.

Thanks in advance

quanta 02-24-2011 09:37 AM

position 26? I don't understand with your example.

Mecalith 02-24-2011 09:44 AM

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.

C 1VKNG ABCDEF 3097.00

Mecalith 02-24-2011 09:47 AM

i cant get the line to show the spaces. ABCDEF should be 26 spaces from the left.

crts 02-24-2011 09:50 AM

Quote:

Originally Posted by Mecalith (Post 4269724)
i cant get the line to show the spaces. ABCDEF should be 26 spaces from the left.

Hi,

use code-tags to preserve the correct formatting.

[CODE]post data here[/CODE]

druuna 02-24-2011 09:50 AM

Hi,
Quote:

Originally Posted by Mecalith (Post 4269724)
i cant get the line to show the spaces. ABCDEF should be 26 spaces from the left.

Use the code tags, this way layout is saved:

[code] your stuff goes here [/code]

colucix 02-24-2011 09:59 AM

Code:

sed -r '/^C/s/^(.{25})/\1ABCDEF/' file
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? :)

grail 02-24-2011 10:04 AM

Not sure about sed, but did you mean something like:
Code:

$awk '/^C/{$NF = sprintf("%31s %s","ABCDEF",$NF)}1' file

B0 49.2230869 -102.5743868
C 1LPRK                          ABCDEF 654.90
C 1PREU                          ABCDEF 3693.90
C 1JRSU                          ABCDEF 3693.90
C 1UVNG                          ABCDEF 3693.90


quanta 02-24-2011 10:07 AM

Try this:
Code:

$ grep "^C" input | sed 's/.\{25\}$/ABCDEF &/'

druuna 02-24-2011 10:08 AM

Hi,

Code:

sed -r '/^C/s/^(.{25,30})/\1ABCDEF/' infile
@colucix: I do believe this is a file with fixed width columns (your solution moves everything after the substitution to the right.

grail 02-24-2011 10:25 AM

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 02-24-2011 10:29 AM

Quote:

Originally Posted by druuna (Post 4269751)
@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.

Mecalith 02-24-2011 10:30 AM

Quote:

Originally Posted by druuna (Post 4269751)
Hi,

Code:

sed -r '/^C/s/^(.{25,30})/\1ABCDEF/' infile
@colucix: I do believe this is a file with fixed width columns (your solution moves everything after the substitution to the right.


sed -r '/^C/s/^(.{25})/\1ABCDEF/' infile

This works great. thanks for the help everyone.

colucix 02-24-2011 10:35 AM

Quote:

Originally Posted by Mecalith (Post 4269782)
sed -r '/^C/s/^(.{25})/\1ABCDEF/' infile

This works great. thanks for the help everyone.

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?

druuna 02-24-2011 10:36 AM

Quote:

Originally Posted by colucix (Post 4269781)
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)

If it is what you wanted: You're welcome :)


All times are GMT -5. The time now is 05:30 PM.