Insert specific line when found similar value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert specific line when found similar value
# 1  
Old 01-27-2012
Insert specific line when found similar value

Hi All,

I have file like this:

file1:

Code:
3778 10474 24
3778 10475 24
3778 10476 25
3778 10495 26
3794 10001 33
3794 10002 33
3794 10004 33
3794 10007 34
3794 10008 34
3794 10011 34
3794 10012 34
3794 10013 34
3794 10017 34
3810 10282 27
3810 10283 27
3810 10284 27
3810 10285 27
3810 10286 27
3810 10287 27
3810 10288 27
3906 10154 34
3906 10155 34
3906 10156 34
3906 10157 34

output that I want :
Code:
                              LINE  3778 ,
                              10474 =  24 ,
                              10475 =  24 ,
                              10476 =  25 ,
                              10495 =  26 ,
                              LINE  3794 ,
                              10001 =  33 ,
                              10002 =  33 ,
                              10004 =  33 ,
                              10007 =  34 ,
                              10008 =  34 ,
                              10011 =  34 ,
                              10012 =  34 ,
                              10013 =  34 ,
                              10017 =  34 ,  
                              LINE  3794 ,
                              10282 =  27 ,
                              10283 =  27 ,
                              10284 =  27 ,
                              10285 =  27 ,
                              10286 =  27 ,
                              10287 =  27 ,
                              10288 =  27 ,
                              LINE  3906 ,
                              10154 =  34 ,
                              10155 =  34 ,
                              10156 =  34 ,
                              10157 =  34 ,

Thanks in advance
- Attila
# 2  
Old 01-27-2012
Hi


Code:
$ awk 'x!=$1{print "LINE ",$1,",";x=$1}{ print $2," = ",$2," ,";}' file1

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 01-27-2012
Nice code guru.

But it needs a small correction

Quote:
Originally Posted by guruprasadpr
Hi


Code:
$ awk 'x!=$1{print "LINE ",$1,",";x=$1}{ print $2," = ",$3," ,";}' file1

Guru.
This User Gave Thanks to itkamaraj For This Post:
# 4  
Old 01-27-2012
my mistake...thanks itkamaraj

Guru.
# 5  
Old 01-27-2012
above awk command will not work for the below input. ( it only works on sorted data )

so, if you have data's like below, sort it before apply the awk. ( sort -n )

Code:
 
3778 10474 24
3778 10475 24
3778 10476 25
3778 10495 26
3794 10001 33
3794 10002 33
3778 10495 26

# 6  
Old 01-27-2012
I just little edit like this:

Code:
awk 'x!=$1{print "                              LINE ",$1,",";x=$1}{print "                             " ,$2," = ",$3," ,";}' file1 > file2


Solved, Thank you all.
# 7  
Old 01-27-2012
Code:
awk '{printf "\t\t\t\t%s  ,\n", a[$1]++?$2 "  =  " $3:"LINE  " $1}' file1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert new line after a specific character in scripts?

Hi, I'm trying to add a new line after finding a specific String. That is my string: volumes: - ${DIR_WORK}/loadbalancer/html:/var/www/html and I want to change that file to: volumes: - ${DIR_WORK}/loadbalancer/html:/var/www/html extra_hosts: -... (4 Replies)
Discussion started by: siamak
4 Replies

2. Shell Programming and Scripting

Insert character at specific location in a each line of the file

Hi All, I am trying to write a shell script where it should insert character 'I' in 180th position of each line(except first and last line) of the file. Below is the script for file in /home/test/bharat/*.RET do # Process file echo "File Name=" $file #l_fileName="${file##*/}" ... (19 Replies)
Discussion started by: bharath561989
19 Replies

3. Shell Programming and Scripting

Insert line based on found string

Hi All I'm trying to insert a new line at the before each comment line in a file. Comment lines start with '#-----' there are other comments with in lines but I don't want a new line there. Example file: blah blah #do not insert here #this is a comment blah #some more #another comment... (10 Replies)
Discussion started by: Mudshark
10 Replies

4. Solaris

Insert a file at specific line

Hi, Anyone can help me in Solaris command on how to insert a file at specific line. I want file1.sql content to be inserted on file2.sh after "recover database using backup controlfile until cancel". # file1.sql /archivelogs/927_822338133.arc /archivelogs/671_822338107.arc... (3 Replies)
Discussion started by: fspalero
3 Replies

5. UNIX for Dummies Questions & Answers

insert string at end of line if it found from list

Hi all, can some one help me out file 1 i have 06/01 3:14 d378299 06/01 8:10 d642036 06/01 10:51 d600441 06/01 10:52 d600441 06/01 11:11 d607339 06/01 11:49 d398706 something like this and in file named list i have ( there is space btwn 06/01 and 11:49 and d398706) d607339... (5 Replies)
Discussion started by: zozoo
5 Replies

6. Shell Programming and Scripting

Korn Shell script to insert at specific line

Hi, I am trying to put together a Korn Shell script to insert at a specific line. The system we use is SunOS 5.10 I can get the line number by using:- num=`sed -n '/export ENV/=' ./tmp.file` Not getting much headway using the above variable's value to insert - export SYBASE=/opt/sybase15... (5 Replies)
Discussion started by: aj8200
5 Replies

7. Shell Programming and Scripting

Insert blank line if grep not found

Hi all, I've googling around forum regarding my prob, the nearest would same as thread tittled Insert blank line if grep not found, but she/he did not mention the solution, so I would like to request your help I've this task, to search in file2 based on pattern in file1 and output it to... (4 Replies)
Discussion started by: masterpiece
4 Replies

8. Shell Programming and Scripting

search a line and insert string into specific at position

Hi, guys. I have one question: How can I search for a line with certain string in it and then insert a string into this line? For example: There is a file called shadow, the contents of it are below: ************************** ... yuanz:VIRADxMsadfDF/Q:0:0:50:7:::... (9 Replies)
Discussion started by: daikeyang
9 Replies

9. Shell Programming and Scripting

insert new line at found chars

Hey gang, I have: XXZZXXZZXX 123 asdaffggh dfghyrgr ertyhdhh XXZZXXZZXX 234 sdg XXZZXXZZXX 456 gfg fggfd That is all on one line. Very simply put I want to do is something like: sed s'/XXZZXXZZXX /\n/g' or tr 'XXZZXXZZXX ' '/n' I have tried various things but can never get the desired... (6 Replies)
Discussion started by: crowman
6 Replies

10. Shell Programming and Scripting

Insert blank line if grep not found

Hello everyone... please help if you can -- I'm stumped. Making this work will save me hours of manual labor: I need to search file2 for pattern in file1. If pattern found append file2 line to file3. If pattern not found append a blank line to file3. file1 contents example: 123 456 789... (6 Replies)
Discussion started by: michieka
6 Replies
Login or Register to Ask a Question