Insert new line based on numerical number of column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert new line based on numerical number of column
# 8  
Old 12-07-2009
Thanks tene.
I think the error I get is because that I can't run the script with ksh Smilie
Anywhere still thanks for your help.
Sorry for my mistakes.

---------- Post updated at 03:57 AM ---------- Previous update was at 03:33 AM ----------

Hi scottn,
By editing your awk code, I got try this way to achieve my another desired output result:
Code:
awk 'BEGIN {FS="Class"} (NR > 2) && (X != $1) { print "" } { X = $1 } 1' input_file > output_file

Unfortunately, it can't get my desired output result. Do you got any suggestion for me to improve your code?
thanks again.
# 9  
Old 12-07-2009
Code:
awk '/^[0-9]/ && (X != $1) { print "" } { X = $1 } 1' input_file > output_file

Class
Number Position Range

1 Initial 50
1 Initial 50

2 Terminal 150
2 Terminal 20
2 Single 10

3 Single 20

4 Double 50

5 Initial 50
5 Initial 60

Class
Number Position Range

1 Initial 150

2 Terminal 250
2 Terminal 200

3 Single 20

4 Double 50
4 Single 100

5 Initial 100

Please don't change your requirement bit by bit. Try to think about everything you need to achieve. Thanks ;-)
# 10  
Old 12-07-2009
Code:
#!/bin/bash
NN=""
outputFile="/tmp/output"
while read N LINE # Puts the first field (the number) in N and the rest in LINE
do
    [ "$NN" != "$N" ] && echo # insert newline if the N is different than the previous value
   # the && executes the next command if the return of the previous one returned TRUE
   # the "!" negates the "=" and  "[ ... ]" means "test if"
    echo "$N $LINE"
    NN=$N
done < $inputfile > $outputfile

# 11  
Old 12-07-2009
Code:
my $flag = "-1.1";
while(<DATA>){
	my @tmp = split(" ",$_,2);
  if (($tmp[0] ne $flag) && /^[0-9]/){
  	print "\n";
  	$flag = $tmp[0];
  }
  print;
}
__DATA__
Class
Number Position Range
1 Initial 50 
1 Initial 50
2 Terminal 150
2 Terminal 20 
2 Single 10 
3 Single 20
4 Double 50
5 Initial 50
5 Initial 60

Class
Number Position Range
1 Initial 150 
2 Terminal 250
2 Terminal 200 
3 Single 20
4 Double 50
4 Single 100 
5 Initial 100

# 12  
Old 12-07-2009
Thanks a lot for your explanation, frans.
I fully understand the back story of your script now Smilie
Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Insert value of column based on file name matching

At the top of the XYZ file, I need to insert the ABC data value of column 2 only when ABC column 1 matches the prefix XYZ file name (not the ".txt"). Is there an awk solution for this? ABC Data 0101 0.54 0102 0.48 0103 1.63 XYZ File Name 0101.txt 0102.txt 0103.txt ... (7 Replies)
Discussion started by: ncwxpanther
7 Replies

3. Shell Programming and Scripting

Insert a new column with sequence number (Delimiter as comma)

Hi All, I have a file which has data like a,b c,d e,f g,h And I need to insert a new column at the begining with sequence no( 1 to n) 1,a,b 2,c,d 3,e,f 4,g,h Please let me know how to acheive this in unix (3 Replies)
Discussion started by: weknowd
3 Replies

4. Shell Programming and Scripting

Insert Columns before the last Column based on the Count of Delimiters

Hi, I have a requirement where in I need to insert delimiters before the last column of the total delimiters is less than a specified number. Say if the delimiters is less than 139, I need to insert 2 columns ( with blanks) before the last field awk -F 'Ç' '{ if (NF-1 < 139)} END { "Insert 2... (5 Replies)
Discussion started by: arunkesi
5 Replies

5. Shell Programming and Scripting

Insert data in first column(if blank) from previous line first column

Dear Team I need to insert field(which is need to taken from previous line's first field) in first column if its blank. I had tried using sed but not find the way. Detail input and output file as below. Kindly help for same. INPUT: SCGR SC DEV DEV1 NUMDEV DCP ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

6. UNIX for Dummies Questions & Answers

Extracting rows from a text file based on numerical values of a column

I have a text file where the second column is a list of numbers going from small to large. I want to extract the rows where the second column is smaller than or equal to 0.0001. My input: rs10082730 9e-08 12 46002702 rs2544081 1e-07 12 46015487 rs1425136 1e-06 7 35396742 rs2712590... (1 Reply)
Discussion started by: evelibertine
1 Replies

7. UNIX for Dummies Questions & Answers

How to sort a column based on numerical ascending order if it includes e-10?

I have a column of numbers in the following format: 1.722e-05 2.018e-05 2.548e-05 2.747e-05 7.897e-05 4.016e-05 4.613e-05 4.613e-05 5.151e-05 5.151e-05 5.151e-05 6.1e-05 6.254e-05 7.04e-05 7.12e-05 7.12e-05 (6 Replies)
Discussion started by: evelibertine
6 Replies

8. Shell Programming and Scripting

Insert text at line number

I wrote a script to grep for a closing XML node. Then I need it to navigate up a line and insert some XML. Then go to the next occurrance. I have this INSERT_NODE='<QUANTITATIVE NAME="'${QR_NAME}'" QUANT="1" />' GREP_FOR='</JOB>' TMP_FILE=/tmp/lineArray.$$ if ]; then continue else ... (7 Replies)
Discussion started by: J-Man
7 Replies

9. Shell Programming and Scripting

Insert comma based on max number of column

Hi, I am new to unix shell shell scripting. I have a specific requirement where I need to append comma's based on the max number of column in the file. Eg: If my source file look something like this, sengwa,china tom,america,northamerica smith,america walter My output file... (8 Replies)
Discussion started by: nicholas_ejn
8 Replies
Login or Register to Ask a Question