Urgent help pls.how to extract two lines having same starting number

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Urgent help pls.how to extract two lines having same starting number
# 15  
Old 03-23-2010
Hi Drewk,

Thanks for the detailed explanation. The tool you referred is very handy. Trying to learn from that.Appreciate your guidelines.Very helpful.

Regards
uma
# 16  
Old 07-01-2010
Error

Code:
set -A array $(</tmp/inputfile) #reading file in array

c= ${#array[*]} #the no. of elements in the array
 
i=1
 
while [ $i -lt $c ]
do
   j=`expr $i +1`
   v1=`echo ${array[$i]}|cut -d" " -f1`
   v2=`echo ${array[$j]}|cut -d" " -f1`
   
   if [ $v1 = $v2 ]
       then
             array[$i]=`echo F.${array[$i]}`
   fi
   i=`expr $i +1`
done
 
i=1
 
while [ $i -le $c ]
do
   x=`echo ${array[$i]|cut -c1`
   
   if [ $x -ne "F" ]
     then
           `echo ${array[$i]}>>newfile`
   fi
done
 
`cat newfile`

see if the code given above works.

here i am trying to store each lines of the inputfile in an array.
c is the number of array elements. (or the numbert of lines in the input file.)
i am checkimg of the two consecutive fileds have same value. if true i am setting F as the 1st character in the 1st of the two lines. then store the same in the array.

later in another loop i am checking if the 1st character of any line is not F (this marks that this line has not been repeated). if true i am writing that to newfile.

newfile is the reqd ouput file.

Last edited by Scott; 07-01-2010 at 07:45 AM.. Reason: Added code tags
# 17  
Old 07-01-2010
Thanks a ton, very helpful and informative

Regards
Uma
# 18  
Old 07-02-2010
Quote:
Originally Posted by drewk
I cannot overemphasize how easy this becomes if you use a regex tool.

Try this: Regex Powertoy (interactive regular expressions) or this RegExr: Online Regular Expression Testing Tool
Agreed. I think everyone should use these types regex tools. I do and they save a lot of time. I also use "fat client" version on my XP machine. It is really good and stores prior patterns, explains things, etc.
# 19  
Old 07-03-2010
heres a python code Smilie
Code:
heres a python code
#code:
#!/usr/bin/env python
present_line=[]
fob = open('question.temp', 'r')
previous_line = []
while 1:

present_line = []
linea = fob.readline()
present_line = linea.split()
try:
if (present_line[0]==previous_line[0]):
print ' '.join(previous_line)

except:
pass

previous_line = present_line
if not present_line : break

fob.close()


Last edited by skishore; 07-03-2010 at 11:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

2. Shell Programming and Scripting

Extract between lines starting with and

I have a combination of patterns to search. file.txt contains below: H2016-02-10 A74867712 I1556539758 Xjdflk534jfl W0000055722327732 W0000056029009389 A74867865 I1556536434 W0000055822970840 W0000055722325916 A74868015 I1556541270 C0000055928920421 E lines starting with A are... (5 Replies)
Discussion started by: chakrapani
5 Replies

3. UNIX for Dummies Questions & Answers

Extract lines starting with rs

hello! could u, please, help: i have a file that includes 6 columns space delimited 1 rs4477212 0 82154 0 T 1 rs6680825 0 91472 0 G 1 rs9326626 0 570178 0 T 1 rs12123356 0 724702 0 C I need to extract to a separate file lines... (5 Replies)
Discussion started by: kush
5 Replies

4. Shell Programming and Scripting

extract the lines by index number

Hi All, I want to extract the lines from file1 by using the index numbers from file2. In example, cat file1.txt 265 ABC 956 ... 698 DFA 456 ... 456 DDD 145 ... 125 DSG 154 ... 459 CGB 156 ... 490 ASF 456 ... 484 XFH 489 ... 679 hgt 481 ... 111 dfg 986 ... 356 vhn 444 ...... (7 Replies)
Discussion started by: senayasma
7 Replies

5. UNIX for Dummies Questions & Answers

Extract n number of lines from a file successively

Hello, I have a file with over 100,000 lines. I would like to be able extract 5000 lines at a time and give it as an input to another program. sed -n '1,5000p' <myfile> > myOut Similarly for 5001-10000 10001-15000 .... How can I do this in a loop? Thanks, Guss (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

6. Shell Programming and Scripting

Awk to extract lines with a defined number of characters

This is my problem, my file (file A) contains the following information: Now, I would like to create a file (file B) containing only the lines with 10 or more characters but less than 20 with their corresponding ID: Then, I need to compare the entries and determine their frequency. Thus, I... (7 Replies)
Discussion started by: Xterra
7 Replies

7. Shell Programming and Scripting

awk if statement matching all lines starting w/ a number

Does awk have a syntax for a range of numbers or is this the best way? if ($1 >= 0 && $1 <= 9 ) (7 Replies)
Discussion started by: Arsenalman
7 Replies

8. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

9. Shell Programming and Scripting

How to print the number of lines from a file, the starting string should be passed`

Hi , I have file, which has the below content: line 100 a b c d line300 a s d f s line200 a s d a (3 Replies)
Discussion started by: little_wonder
3 Replies

10. UNIX for Dummies Questions & Answers

extract some specific text file urgent pls

i have a big text file . i want to create new file as extract some specific text from the big file i am using hp ux please help (2 Replies)
Discussion started by: reyazan
2 Replies
Login or Register to Ask a Question