Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to add a line in my file with a certain pattern

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
esba (Visitor) Mar 18, 2002
I would like to add a line to my file that do not have the following pattern eg:

0 1 999 therefore if I 'll find in my a pattern on where the third field doesn't start with a 1 ie: i would add a second entry
eg:

0 1 1999 etc...

Basically I would read a file F then check for the pattern 0 1 and the check for a leading one in the third field if its missing I would add anew line ... So my new file would have:

0 1 999
0 1 1999

I have tried doing it with sed but I did't succeed:

sed -e '1,$s/0 1 [2-9]*/0 1 1/' F

sos ... Can anybody help ??

Tks!

 
This awk script might not do axactly what you want but it should get you started.
Code:
{
  print
  if ($0 ~/^0 1 [^1]/) {
    print "0 1 1" $3
  }
}
                                                                             .
Hope this helps. CaKiwi
 
CaKiwi,

how would I modify this script if I were to achieve the following:

Initial file F:
0 1 999
0 1 1999
1 1 1999
0 1 888
1 1 888

The goal would be to add a leading 1 for every line that does not have one.
therefore my end result would look like:

0 1 999
1 1 999
0 1 1999
1 1 1999
0 1 888
1 1 888

this means that I there was only one line that didnot have a leading one in front whiche is in fact the first one :

ie: 0 1 999

Also my end file would have to be sorted according to the third field:

for exple:


if my file was :

0 1 999
0 1 1999
1 1 1999
0 1 888
1 1 888
1 1 999

My end file would be :

0 1 999
1 1 999
0 1 1999
1 1 1999
0 1 888
1 1 888


tks!
 
With sed :

sed -f add.sed F

--- add.sed ---
p
s/^0 1 \([2-9].*\)$/0 1 1\1/p
d
--- End of add.sed --- Jean Pierre.
 
Aigles ,

Tks for ur feedback , but the sed file did'nt do the trick

here is what I got after running the sed:


initial file:
0 1 888
0 1 555
0 1 1999
1 1 1999
1 1 555

after running the sed the result was exactly the same.

what I want to achieve exactly in my second question is
to add a leading one for each pattern that hasn't one:

eg: as we can see from the above file the only line that doesnot have a leading 1 in its pattern is
0 1 888. whereas the others all do:

0 1 555 has a 1 1 555
0 1 1999 has a 1 1 1999 etc....
0 1 888 doe not

so my end result would be a script that would do this so that my end file would look like this bcos it has to be sorted according to the third field.


0 1 555
1 1 555 <----- this line would be added by a script
0 1 888
1 1 888
0 1 1999
1 1 1999


Tks!
 
sorry I have made a mistake it should be:


0 1 555
1 1 555
0 1 888
1 1 888 <----- this line would be added by a script
0 1 1999
1 1 1999


Apologies !!
 
Use sort to order the file based on the 3rd field then run this awk script.
Code:
{
  if (!first) {
    a = $0
    a3 = $3
    getline
  }
  first = 0
  if ($3 != a3) {
    if (substr(a,1,3) == &quot;0 1&quot;) {
      print a
      print &quot;1 1 &quot; a3
    }
    else {
      print &quot;0 1 &quot; a3
      print a
    }
    a = $0
    a3 = $3
    first = 1
  }
  else {
    print a
    print
  }
Hope this helps.
} CaKiwi
 
Tks a great deal CaKiwi for ur feedback !
I had to modify some of it.... but it was of a great help !

Tks again !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top