LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2011, 09:26 AM   #1
Mecalith
LQ Newbie
 
Registered: May 2008
Posts: 5

Rep: Reputation: 0
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
 
Old 02-24-2011, 09:37 AM   #2
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 101Reputation: 101
position 26? I don't understand with your example.
 
Old 02-24-2011, 09:44 AM   #3
Mecalith
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Thumbs up

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

Last edited by Mecalith; 02-24-2011 at 09:45 AM.
 
Old 02-24-2011, 09:47 AM   #4
Mecalith
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 0
i cant get the line to show the spaces. ABCDEF should be 26 spaces from the left.
 
Old 02-24-2011, 09:50 AM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Mecalith View Post
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]
 
Old 02-24-2011, 09:50 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by Mecalith View Post
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]
 
Old 02-24-2011, 09:59 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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?
 
Old 02-24-2011, 10:04 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 02-24-2011, 10:07 AM   #9
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 101Reputation: 101
Try this:
Code:
$ grep "^C" input | sed 's/.\{25\}$/ABCDEF &/'
 
Old 02-24-2011, 10:08 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 02-24-2011, 10:25 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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)??
 
Old 02-24-2011, 10:29 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by druuna View Post
@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.

Last edited by colucix; 02-24-2011 at 10:33 AM.
 
Old 02-24-2011, 10:30 AM   #13
Mecalith
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
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.
 
Old 02-24-2011, 10:35 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Mecalith View Post
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?
 
Old 02-24-2011, 10:36 AM   #15
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by colucix View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Insert column with awk or sed between two columns captainentropy Linux - Newbie 9 11-27-2014 11:49 AM
[SOLVED] sed one-liner: search text in file and replace it. if not present, insert it pkramerruiz Programming 8 10-17-2010 09:49 AM
A macro for solve insert/delete merged cell/column kstan Linux - General 0 11-02-2008 11:01 PM
Vi command: howto insert '\n' at column 80? jhwilliams Linux - Software 5 08-15-2007 02:07 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration