Adding a line before


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Adding a line before
# 1  
Old 12-13-2013
Adding a line before

Hi
I had a question related to adding a particular line in my file.

My file looks like this

Code:
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 same entry as the line which is bolded.

The bolded line needs to have the 3rd column entry " assumption " to be replaced by predictive.

An example:
Code:

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;

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

Thanks

Last edited by Scrutinizer; 12-13-2013 at 04:05 AM.. Reason: code tags
# 2  
Old 12-13-2013
You could try:
Code:
awk '$3=="assumption"{$0=$0 ORS $0; sub($3, "predictive")}1' file

But you would also need to adjust the ID in the last column, no?

Perhaps sed:
Code:
sed 's/\(.*\)assumption\(.*\)\(ID=[^.]*\)[^;]*\(;.*\)/\1predictive\2\3\4\
&/' file


Last edited by Scrutinizer; 12-13-2013 at 04:21 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 12-13-2013
Code:
#!/usr/bin/env perl

open( $fh, "<", "file") or die "Cannot open file: $!\n";

while( my $line = <$fh>) {
	chomp($line);
	my @array = split /\s+/, $line;			# split to get 3rd field	
	if ( $array[2] eq "assumption"){
		my $new = $line;					# save current line
		$new =~ s/assumption/predictive/;	        # replace assumption
		$line = $new."\n" . $line ;			# store new version
	}
	print $line."\n";						

}

This User Gave Thanks to brianadams For This Post:
# 4  
Old 12-14-2013
Quote:
Originally Posted by Scrutinizer
You could try:
Code:
awk '$3=="assumption"{$0=$0 ORS $0; sub($3, "predictive")}1' file

But you would also need to adjust the ID in the last column, no?

Perhaps sed:
Code:
sed 's/\(.*\)assumption\(.*\)\(ID=[^.]*\)[^;]*\(;.*\)/\1predictive\2\3\4\
&/' file

I tried both the codes.
The first awk code did makethe necessary changes in 3rd column but didnt do the ID change.

As per your advice the 2nd sed command will help in making the changes in ID.
However the 2nd sed command just replaced the entire line having assumption in 3rd column with a line having predictive in 3rd column

I wanted to add a line which has the same contents as line of assumption but change in ID value.
(ddint want to remove the already existing line. )
I hope its not confusing.

Thanks a lot though.
# 5  
Old 12-14-2013
Code:
perl -lape 'if($F[2] eq assumption) {$_.="$/$_"; $_=~s/$F[2](.*?ID=\w+)\.\d/predictive\1/}' file


Last edited by jethrow; 12-14-2013 at 05:42 PM..
This User Gave Thanks to jethrow For This Post:
# 6  
Old 12-14-2013
Quote:
Originally Posted by sa@@
I tried both the codes.
The first awk code did makethe necessary changes in 3rd column but didnt do the ID change.

As per your advice the 2nd sed command will help in making the changes in ID.
However the 2nd sed command just replaced the entire line having assumption in 3rd column with a line having predictive in 3rd column

I wanted to add a line which has the same contents as line of assumption but change in ID value.
(ddint want to remove the already existing line. )
I hope its not confusing.

Thanks a lot though.
I am not sure I understand what you mean. The 2nd command does not replace but adds a "predictive" line:

Code:
$ sed 's/\(.*\)assumption\(.*\)\(ID=[^.]*\)[^;]*\(;.*\)/\1predictive\2\3\4\
&/' file
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;
$

The command uses two lines the first line ends with a \ as the last character on the line.

Some sed's may be able to do this:
Code:
sed 's/\(.*\)assumption\(.*\)\(ID=[^.]*\)[^;]*\(;.*\)/\1predictive\2\3\4\n&/' file

awk version:
Code:
awk '
  $3=="assumption"{
    split($NF,F,/[.;]/)
    $0=$0 ORS $0
    sub($3, "predictive")
    sub(F[1]"\."F[2], F[1])
  }
  1
' file


Last edited by Scrutinizer; 12-14-2013 at 03:47 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 02-16-2014
additional doubts

To add on to my previous query,I have some additional questions using the code
Code:
sed 's/\(.*\)assumption\(.*\)\(ID=[^.]*\)[^;]*\(;.*\)/\1predictive\2\3\4\n&/' file

In my file, I have some instance where there are variants for the id name :For example One variant of id is f000012.1 and the other is f000012.2
File looks like this

Code:
sorce1       LEN   assumption    354569    361011   0.628724        +       .       ID=f000012.1;source_id=A.off_LEN_10008425;
sorce1       LEN   descriptive        354600    360111    .       +       0       Parent=f000012.1;

sorce1       LEN   assumption    350567    354686    0.628724        +       .       ID=f000012.2;source_id=A.off_LEN_10008425;
sorce1       LEN   descriptive     350567    353321    .       +       0                       Parent=f000012.2;


Using the mentioned command adds 2 lines

Code:
sorce1       LEN   predictive   354569    361011   0.628724        +       .       ID=f000012.1;source_id=A.off_LEN_10008425;

sorce1       LEN   assumption    354569    361011   0.628724        +       .       ID=f000012.1;source_id=A.off_LEN_10008425;
sorce1       LEN   descriptive        354600    360111    .       +       0       Parent=f000012.1;


sorce1       LEN  predictive     350567    354686    0.628724        +       .       ID=f000012.2;source_id=A.off_LEN_10008425;

sorce1       LEN   assumption    350567    354686    0.628724        +       .       ID=f000012.2;source_id=A.off_LEN_10008425;
sorce1       LEN   descrptive     350567    353321    .       +       0                       Parent=f000012.2;

---------- Post updated at 12:37 PM ---------- Previous update was at 12:35 PM ----------

Instead of two separate predictive lines for same id ,
Is there a way I could only a single line with predictive with using the earliest start point ie : and farthest away end point to represent the predictive statement?


The ID should have only the name without the variants like this
Code:
sorce1       LEN   predictive    350567    361011    0.628724        +       .       ID=f000012;source_id=A.off_LEN_10008425;

This should happen only for cases having variants of IDS ie have two ids one with.1 and .2
For other cases,it should follow the normal procedure of just adding a single line

Can this be possible?

Last edited by Scrutinizer; 02-17-2014 at 01:23 AM.. Reason: corrected formatting
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Adding a new line after a specific line with sed

Hi All, My requirement is to add a specific line in a file after a certain line that contains 'setenv' the existing code is like setenv SEQFILES "/ConvWrk/inteng03/alltars/bnymais1" LIBDEF scope='JOB' type='PGM' dataset='SUNAR.PJ90000P.JOBLIB'... (5 Replies)
Discussion started by: gotamp
5 Replies

2. Shell Programming and Scripting

Adding line in a file using info from previous line

I have a shell script that looks something like the following: mysql -uroot db1 < db1.sql mysql -uroot db2 < db2.sql mysql -uroot db3 < db3.sql mysql -uroot db4 < db4.sql .... different db names in more than 160 lines. I want to run this script with nohup and have a status later. So,... (6 Replies)
Discussion started by: MKH
6 Replies

3. Shell Programming and Scripting

Adding comma to end of each line if more than 1 line

I have a file with dates as '2013-01-01' '2013-01-02' I want the output to be '2013-01-01','2013-01-02' if there is only 1 entry then there should not be any comma. (6 Replies)
Discussion started by: ATWC
6 Replies

4. Shell Programming and Scripting

Adding tab/new line at the end of each line of a file

Hello Everyone, I need a help from experts of this community regarding one of the issue that I am facing with shell scripting. My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line. The... (20 Replies)
Discussion started by: Sourav Das
20 Replies

5. Shell Programming and Scripting

Adding a blank line after every 5th line

Hello... I have a file which contain certain number of records. I want to generate another file from this file which will contain 1st line as a blank line & after every 5 lines one blank line will be inserted. How to achieve this through shell scripting? Thanks... (5 Replies)
Discussion started by: 46019
5 Replies

6. Shell Programming and Scripting

adding characters end of line where line begins with..

Hi all, using VI, can anyone tell me how to add some characters onto the end of a line where the line begins with certain charactars eg a,b,c,......., r,s,t,........, a,b,c,......., all lines in the above example starting with a,b,c, I want to add an x at the end of the line so the... (6 Replies)
Discussion started by: satnamx
6 Replies

7. UNIX for Dummies Questions & Answers

Adding a line feed to each line

Hi folks, I did a search on this and didn't really find what fit my needs. I have a file with fixed record length of 436 bytes. I want to add a line feed x"0A" to the end x"0A" of each record(in byte 437). What's the easiest way to accomplish this? Thanks. (5 Replies)
Discussion started by: keeferb
5 Replies

8. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies

9. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

10. UNIX for Dummies Questions & Answers

adding line terminator

Can someone help , how to add a terminator at the end of each line through shell command. ex: input file: abc xyz2 outputfile (terminator is !) abc! xyz2! (1 Reply)
Discussion started by: thanuman
1 Replies
Login or Register to Ask a Question