LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 12-12-2013, 11:31 PM   #1
sam@
Member
 
Registered: Sep 2013
Posts: 31

Rep: Reputation: Disabled
adding line


Query related to adding a particular line in my file.

My original file looks like this

sorce1 LEN assumption 695 3570 0.770047 - . ID=f000001.1;source_id=A.off_LEN_10008424;
sorce1 LEN descriptive 3334 3570 . - 0 Parent=f000001.1;
sorce1 LEN extra 913 993 . - 0 Parent=f000001.1;
sorce1 LEN descriptive 695 736 . - 0 Parent=f000001.1;

sorce1 LEN assumption 8859 11328 0.628724 + . ID=f000002.1;source_id=A.off_LEN_10008425;
sorce1 LEN descrptive 8859 9032 . + 0 Parent=f000002.1;

sorce1 LEN assumption 24331 34483 1 - . ID=f000003.1;source_id=A.off_LEN_10008426;part_support_id=CUFF1.1.1;
sorce1 LEN extra 34479 34483 . - 0 Parent=f000003.1;


For every line which has assumption in 3rd column, I have to add a line which has an entry"predictive " in 3rd column.

It can be seen in bolded line

The added line needs to have the 3rd column entry as predictive.

An example:

result file should look like this
sorce1 LEN predictive 695 3570 0.770047 - . ID=f000001;source_id=A.off_LEN_10008424;
sorce1 LEN assumption 695 3570 0.770047 - . ID=f000001.1;source_id=A.off_LEN_10008424;
sorce1 LEN descriptive 3334 3570 . - 0 Parent=f000001.1;
sorce1 LEN extra 913 993 . - 0 Parent=f000001.1;
sorce1 LEN descriptive 695 736 . - 0 Parent=f000001.1;

sorce1 LEN predictive 8859 11328 0.628724 + . ID=f000002;source_id=A.off_LEN_10008425;

sorce1 LEN assumption 8859 11328 0.628724 + . ID=f000002.1;source_id=A.off_LEN_10008425;
sorce1 LEN descrptive 8859 9032 . + 0 Parent=f000002.1;

[B]sorce1 LEN predictive 24331 34483 1 - . ID=f000003;source_id=A.off_LEN_10008426;part_support_id=CUFF1.1.1;
sorce1 LEN assumption 24331 34483 1 - . ID=f000003.1;source_id=A.off_LEN_10008426;part_support_id=CUFF1.1.1;
sorce1 LEN extra 34479 34483 . - 0 Parent=f000003.1;

Please do help on what I could do to add a line for every statement which has assumption in 3rd column

Thanks
 
Old 12-13-2013, 12:54 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
 
sorce1 LEN assumption 695 3570 0.770047 - . ID=f000001.1;
sorce1 LEN descriptive 3334 3570 . - 0 Parent=f000001.1;
sorce1 LEN extra 913 993 . - 0 Parent=f000001.1;
sorce1 LEN descriptive 695 736 . - 0 Parent=f000001.1;

sorce1 LEN assumption 8859 11328 0.628724 + . ID=f000002.1;
sorce1 LEN descrptive 8859 9032 . + 0 Parent=f000002.1;

sorce1 LEN assumption 24331 34483 1 - . ID=f000003.1;
sorce1 LEN extra 34479 34483 . - 0 Parent=f000003.1;
... this awk ...
Code:
awk '{save=$0;
      if ($3=="assumption") {$3="predictive"; print};
      print save}' $InFile >$OutFile
... produced this OutFile ...
Code:
 
sorce1 LEN predictive 695 3570 0.770047 - . ID=f000001.1;
sorce1 LEN assumption 695 3570 0.770047 - . ID=f000001.1;
sorce1 LEN descriptive 3334 3570 . - 0 Parent=f000001.1;
sorce1 LEN extra 913 993 . - 0 Parent=f000001.1;
sorce1 LEN descriptive 695 736 . - 0 Parent=f000001.1;

sorce1 LEN predictive 8859 11328 0.628724 + . ID=f000002.1;
sorce1 LEN assumption 8859 11328 0.628724 + . ID=f000002.1;
sorce1 LEN descrptive 8859 9032 . + 0 Parent=f000002.1;

sorce1 LEN predictive 24331 34483 1 - . ID=f000003.1;
sorce1 LEN assumption 24331 34483 1 - . ID=f000003.1;
sorce1 LEN extra 34479 34483 . - 0 Parent=f000003.1;
Daniel B. Martin

Last edited by danielbmartin; 12-13-2013 at 01:37 PM. Reason: Tighten the code, slightly
 
Old 12-13-2013, 02:10 PM   #3
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
Looking at the data provided I wonder if specifically looking at column 3 is needed.

Here's a sed solution based on that:
Code:
sed -r '/assumption/{ h ; s/assumption/predictive/ ; G }' infile
Example run:
Code:
$ sed '/assumption/{ h ; s/assumption/predictive/ ; G }' infile 
sorce1 LEN predictive 695 3570 0.770047 - . ID=f000001.1;source_id=A.off_LEN_10008424;
sorce1 LEN assumption 695 3570 0.770047 - . ID=f000001.1;source_id=A.off_LEN_10008424;
sorce1 LEN descriptive 3334 3570 . - 0 Parent=f000001.1;
sorce1 LEN extra 913 993 . - 0 Parent=f000001.1;
sorce1 LEN descriptive 695 736 . - 0 Parent=f000001.1;

sorce1 LEN predictive 8859 11328 0.628724 + . ID=f000002.1;source_id=A.off_LEN_10008425;
sorce1 LEN assumption 8859 11328 0.628724 + . ID=f000002.1;source_id=A.off_LEN_10008425;
sorce1 LEN descrptive 8859 9032 . + 0 Parent=f000002.1;

sorce1 LEN predictive 24331 34483 1 - . ID=f000003.1;source_id=A.off_LEN_10008426;part_support_id=CUFF1.1.1;
sorce1 LEN assumption 24331 34483 1 - . ID=f000003.1;source_id=A.off_LEN_10008426;part_support_id=CUFF1.1.1;
sorce1 LEN extra 34479 34483 . - 0 Parent=f000003.1;
 
Old 12-13-2013, 11:31 PM   #4
sam@
Member
 
Registered: Sep 2013
Posts: 31

Original Poster
Rep: Reputation: Disabled
Question

Thanks for your valuable suggestion.
This definitely changes the 3rd columns but is there a way I could modify the id values as well

sorce1 LEN predictive 695 3570 0.770047 - . ID=f000001;source_id=A.off_LEN_10008424;
sorce1 LEN assumption 695 3570 0.770047 - . ID=f000001.1;source_id=A.off_LEN_10008424;
sorce1 LEN descriptive 3334 3570 . - 0 Parent=f000001.1;
sorce1 LEN extra 913 993 . - 0 Parent=f000001.1;
sorce1 LEN descriptive 695 736 . - 0 Parent=f000001.1;

sorce1 LEN predictive 8859 11328 0.628724 + . ID=f000002;source_id=A.off_LEN_10008425;
sorce1 LEN assumption 8859 11328 0.628724 + . ID=f000002.1;source_id=A.off_LEN_10008425;
 
Old 12-14-2013, 02:19 AM   #5
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
@sam@: Try this:
Code:
sed -r '/assumption/{ h ; s/assumption/predictive/ ; s/ID=(.*[0-9])\.[0-9];s/ID=\1;s/; G }' infile
Example:
Code:
$ sed -r '/assumption/{ h ; s/assumption/predictive/ ; s/ID=(.*[0-9])\.[0-9];s/ID=\1;s/; G }' infile
sorce1 LEN predictive 695 3570 0.770047 - . ID=f000001;source_id=A.off_LEN_10008424;
sorce1 LEN assumption 695 3570 0.770047 - . ID=f000001.1;source_id=A.off_LEN_10008424;
sorce1 LEN descriptive 3334 3570 . - 0 Parent=f000001.1;
sorce1 LEN extra 913 993 . - 0 Parent=f000001.1;
sorce1 LEN descriptive 695 736 . - 0 Parent=f000001.1;

sorce1 LEN predictive 8859 11328 0.628724 + . ID=f000002;source_id=A.off_LEN_10008425;
sorce1 LEN assumption 8859 11328 0.628724 + . ID=f000002.1;source_id=A.off_LEN_10008425;
sorce1 LEN descrptive 8859 9032 . + 0 Parent=f000002.1;

sorce1 LEN predictive 24331 34483 1 - . ID=f000003;source_id=A.off_LEN_10008426;part_support_id=CUFF1.1.1;
sorce1 LEN assumption 24331 34483 1 - . ID=f000003.1;source_id=A.off_LEN_10008426;part_support_id=CUFF1.1.1;
sorce1 LEN extra 34479 34483 . - 0 Parent=f000003.1;
 
Old 12-14-2013, 01:09 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by sam@ View Post
... is there a way I could modify the id values as well ...
With this InFile ...
Code:
sorce1 LEN assumption 695 3570 0.770047 - . ID=f000001.1;abc
sorce1 LEN descriptive 3334 3570 . - 0 Parent=f000001.1;def
sorce1 LEN extra 913 993 . - 0 Parent=f000001.1;ghi
sorce1 LEN descriptive 695 736 . - 0 Parent=f000001.1;jkl

sorce1 LEN assumption 8859 11328 0.628724 + . ID=f000002.1;mno
sorce1 LEN descrptive 8859 9032 . + 0 Parent=f000002.1;pqr

sorce1 LEN assumption 24331 34483 1 - . ID=f000003.1;stu
sorce1 LEN extra 34479 34483 . - 0 Parent=f000003.1;vwx
... this awk ...
Code:
awk '{save=$0;
    if ($3=="assumption")
       {$3="predictive";
        $9=substr($9,1,index($9,".")-1)  \
           substr($9,  index($9,".")+2);
    print};
    print save}' $InFile >$OutFile
... produced this OutFile ...
Code:
sorce1 LEN predictive 695 3570 0.770047 - . ID=f000001;abc
sorce1 LEN assumption 695 3570 0.770047 - . ID=f000001.1;abc
sorce1 LEN descriptive 3334 3570 . - 0 Parent=f000001.1;def
sorce1 LEN extra 913 993 . - 0 Parent=f000001.1;ghi
sorce1 LEN descriptive 695 736 . - 0 Parent=f000001.1;jkl

sorce1 LEN predictive 8859 11328 0.628724 + . ID=f000002;mno
sorce1 LEN assumption 8859 11328 0.628724 + . ID=f000002.1;mno
sorce1 LEN descrptive 8859 9032 . + 0 Parent=f000002.1;pqr

sorce1 LEN predictive 24331 34483 1 - . ID=f000003;stu
sorce1 LEN assumption 24331 34483 1 - . ID=f000003.1;stu
sorce1 LEN extra 34479 34483 . - 0 Parent=f000003.1;vwx
This solution assumes the ID= field will always be the 9th blank-delimited word.

Daniel B. Martin
 
  


Reply



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] Adding a character in each line depending upon number of such characters present in a line. ls_milkyway Linux - Newbie 13 08-24-2013 03:19 AM
Adding # to the first line leblinux Linux - Newbie 6 06-23-2011 08:12 AM
[SOLVED] adding new line after each line in perl KManepalli Linux - Newbie 9 04-28-2011 08:02 AM
[SOLVED] adding line from file1 into a line of another file based on maching IDs rossk Programming 6 01-06-2011 12:06 AM
Adding a line to file mikz Linux - General 2 03-03-2005 12:35 PM

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

All times are GMT -5. The time now is 03:54 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