LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 05-28-2010, 02:34 AM   #1
docaia
LQ Newbie
 
Registered: Jan 2008
Posts: 18

Rep: Reputation: 0
Post Insert a column of text to a text file


Hello Gurus,
I want to do a simple action which is to insert a column of text to a text file. in my case i want to insert the letters KL on columns 9 and 10 for all lines in the file. my file looks like this:
Code:
S  16            10245207                     5117960 9088449    0
S  16            11145209                     5076020 9104790    0
S  16            11145217                     5077389 9108548    0
S  16            11175211                     5074953 9106243    0
S  16            11175213                     5075295 9107182    0 0
I want it to look like this
Code:
S  16  KL        10245207                     5117960 9088449    0
S  16  KL        11145209                     5076020 9104790    0
S  16  KL        11145217                     5077389 9108548    0
S  16  KL        11175211                     5074953 9106243    0
S  16  KL        11175213                     5075295 9107182    0 0
The file to too big to do this manually line by line.
I am not too familiar with the advanced VI commands that begin with %S//H/ something like this but I believe a command like this will solve my problem.
May be also an awk command can do the trick.
Any ideas on how to do that?
thanks a lot
 
Old 05-28-2010, 03:05 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Untested, but this might work:

Code:
sed -i.bak 's/S  16    /S  16  KL/' infile
The -i.bak options creates a backup of the original.

Or, from vi:

Code:
:s/S  16    /S  16  KL/
Hope this helps.
 
Old 05-28-2010, 03:18 AM   #3
docaia
LQ Newbie
 
Registered: Jan 2008
Posts: 18

Original Poster
Rep: Reputation: 0
Hi druuna,
sed does exactly what I wanted...thanks a lot for that.
from vi it works only for the current line (line that the courser is on) can we change it so that it is applied to all lines?

Thanks again for your reply, it saved me a lot of time
 
Old 05-28-2010, 03:24 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Sorry about the vi command, I forgot to set the range:

Code:
:1,$s/S  16    /S  16  KL/
The above should work.
 
Old 06-02-2011, 10:45 AM   #5
docaia
LQ Newbie
 
Registered: Jan 2008
Posts: 18

Original Poster
Rep: Reputation: 0
add column not based on criteria

now suppose that I want to add a column to the end of the file (after the last column) so we want from:
Code:
S  16  KL        10245207                     5117960 9088449    
S  16  KL        11145209                     5076020 9104790    
S  16  KL        11145217                     5077389 9108548    
S  16  KL        11175211                     5074953 9106243    
S  16  KL        11175213                     5075295 9107182
to add 400 at the end of every line as below:
Code:
S  16  KL        10245207                     5117960 9088449    400
S  16  KL        11145209                     5076020 9104790    400
S  16  KL        11145217                     5077389 9108548    400
S  16  KL        11175211                     5074953 9106243    400
S  16  KL        11175213                     5075295 9107182    400
 
Old 06-02-2011, 11:55 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
hmmm ... how did you get rid of the zeroes at the end? (ie from the previous example)

Also if we assume you might not know about S and 16 for the first example:
Code:
sed -r -i.bak 's/.{8}/&KL/' file
As for vi, if you put % at the start of the change it will also perform for all lines.

As for your last query, you should really do some of the work yourself, so all I will tell you is that $ represents the end of each line
 
  


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
Read text file column by column RVF16 Programming 11 05-31-2009 08:16 AM
Swap column of text file kushalkoolwal Programming 4 09-05-2008 12:30 PM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 03:36 PM
Combine multiple one column text file into one text file with multiple colum khairilthegreat Linux - Newbie 7 11-23-2007 02:31 PM
ripping a column from a text file dominant Linux - Newbie 1 01-31-2006 05:15 AM


All times are GMT -5. The time now is 02:45 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration