Inserting some text if a field in the last column changes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting some text if a field in the last column changes
# 1  
Old 09-21-2012
Inserting some text if a field in the last column changes

Hi,

I have a file which looks like this:
Code:
A 01 00
B 02 00
C 04 00
D 00 01
E 01 01
F 02 01
G 01 04
H 02 04

I want to insert some text if the field if the last column changes. It should look like this:
Code:
Value 00
A 01 00
B 02 00
C 04 00
Value 01
D 00 01
E 01 01
F 02 01
Value 04
G 01 04
H 02 04

I tried using the following loop:
Code:
      while read line
      do
      NEW_VALUE=`echo "$line" | gawk '{printf $NF}'`
      if [ "$NEW_VALUE" != "$OLD_VALUE" ]; then
         echo Value "$NEW_VALUE" >> $tmp
      fi
      echo "  $line" >> $tmp
      OLD_VALUE="$NEW_VALUE"
      done < $file

However, this loop runs slow. Do you have any ideas?

Thanks in advance
# 2  
Old 09-21-2012
Try this:
Code:
awk 'NR==1 || v != $3 {v=$3; print "Value " v}1' $file > $tmp

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 09-21-2012
Thank you very much, it works great!

However, I have another question. Is it possible to cut the last column of the file and to use some loop in this gawk function which will print the exact amount of the same chars (dashes) which are followed by a space and some amount of dashes? So it should look like this (let's assume that we declared in a variable the amount of dashes =9 which are followed by 4 dashes:
Code:
Value 00
--------- ----
A 01
B 02
C 04
Value 01
--------- ----
D 00
E 01
F 02
Value 04
---------
G 01
H 02

Thanks in advance
# 4  
Old 09-21-2012
Quote:
Originally Posted by wenclu
Code:
Value 00
--------- ----
A 01
B 02
C 04
Value 01
--------- ----
D 00
E 01
F 02
Value 04
--------- #and why here "----" are not present..?
G 01
H 02

# 5  
Old 09-21-2012
I'm sorry, i made a mistake in that place. Of course, it should look like this:

Code:
Value 00
--------- ----
A 01
B 02
C 04
Value 01
--------- ----
D 00
E 01
F 02
Value 04
--------- ----
G 01
H 02

# 6  
Old 09-21-2012
Try this...

Code:
var=9
awk -v VM="$var" '{if (NR==1 || v != $3) {v=$3; print "Value " v;$3="";{for(i=1;i<=VM;i++) {printf "-"}{print " ----"}}} else{$3=""}}1' file

This User Gave Thanks to pamu For This Post:
# 7  
Old 09-24-2012
Thank you very much, it works perfectly!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting a field without disturbing field separator on other fields

Hi All, I have the input as below: cat input 032016002 2.891 97.109 16.605 27.172 24.017 32.207 0.233 0.021 39.810 0.077 0.026 19.644 13.882 0.131 11.646 0.102 11.449 76.265 23.735 16.991 83.009 8.840 91.160 0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625 0.121 1.126 40.189... (15 Replies)
Discussion started by: am24
15 Replies

2. UNIX for Dummies Questions & Answers

Inserting the last field first

I have a list of more than 1000 files on the following format. roman_pottery_in_the_archaeological_record_2007.pdf power_politics_and_religion_in_timurid_iran_2007.pdf toward_a_theory_of_human_rights_religion_law_courts_2006.pdf i_was_wrong_the_meanings_of_apologies_2008.pdf I want to... (2 Replies)
Discussion started by: kristinu
2 Replies

3. UNIX for Dummies Questions & Answers

Inserting a sequential number into a field on a flat file

I have a csv flatfile with a few million rows. I need to replace a field (field number is 85) in the file with a sequential number. As an example, let's assume there are only 4 fields in the file: A,A,,32 A,A,,27 A,B,,43 C,C,,354 If I wanted to amend the 3rd field in this way my... (2 Replies)
Discussion started by: BristolSmithy
2 Replies

4. Shell Programming and Scripting

Inserting string in between field in comma separated file

Hello Mates, I have one txt file having commo seperated values. I have to insert string "FALSE" in 2nd field from the end. E.G SE18 6RN,,,,5439070,1786840,,1000002148671600,123434 Out put should be: SE18 6RN,,,,5439070,1786840,FALSE,1000002148671600,123434 Can some one help me to... (8 Replies)
Discussion started by: krsnadasa
8 Replies

5. Shell Programming and Scripting

Inserting a new field inbetween two exisitng field

I have a '|' delimited file. My file looks like below 23|nationalhoilday|feb12||||||||||||||california|northdistrict|| In the same way, each record has 164 fields. I have to insert one more field after the 85th field. Expected output... (3 Replies)
Discussion started by: machomaddy
3 Replies

6. UNIX for Dummies Questions & Answers

Inserting a column into a text file

I have a tab delimited text file with multiple columns (data.txt). I would like to insert a column into the text file. The column I want to insert is in a text file (column.txt). I want to insert it into the 5th column of data.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

7. Shell Programming and Scripting

ORA-01756 Error while inserting a file in CLOB field

Hi, Please guide me where i am doing wrong, i am getting ORA-01756:quoted string not properly terminated when i am trying to insert file into CLOB cloumn of Oracle DB. Please find below the code where log file variable is myLogFile. Please let me know where i am doing wrong. ... (0 Replies)
Discussion started by: rajeshorpu
0 Replies

8. UNIX for Dummies Questions & Answers

inserting a text after a certain word in text files

I need insert a text file content in other text file after certain word like insert content of tagfav.txt in all my html files after the <head> tag. Anyone can help me? (2 Replies)
Discussion started by: ItaloAG
2 Replies

9. Shell Programming and Scripting

Question about sed. Inserting text in field?

Hi, I have tried to develop a sed script that inserts date and time in the third field in the first and second row below. The third row is an example and it shows where the date and time should be inserted. The script should check if the row already has date and time in the third field and if it... (2 Replies)
Discussion started by: pcrs
2 Replies

10. UNIX for Advanced & Expert Users

Inserting a new column in a file

Hey.. I'm writing a code to download some stuff from Informix database and put it on Xls. It works fine, but I have a problem fitting in a new requirement. I have currently a file which has information like below. f_name|Ronnie|Johnson|23.00| f_sal|Ronnie|Jhonson|4000.00|... (4 Replies)
Discussion started by: rosh0623
4 Replies
Login or Register to Ask a Question