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
# 1  
Old 12-07-2009
Insert new line based on numerical number of column

My input 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
.
.
.

My 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

.
.
.

I got a long list of file. How can I added new line by using awk,sed, or perl based on the numerical number of column 1?
Each section of the content begin with "Class".
Thanks a lot for suggestion.

Last edited by patrick87; 12-07-2009 at 04:50 AM..
# 2  
Old 12-07-2009
Code:
number=1
outputFile="/tmp/output"

for line in `cat inputFile`
do
    if (`awk -F" " '{print $1}'` -eq $number )
    then
          echo $line >> $outputFile
    else
          echo "\n$line" >> $outputFile
          $number++;
    fi
done

# 3  
Old 12-07-2009
Hi tene,
I just try the script that you suggestion, but it seems like can't work?
Do I got did anything wrong?
Thanks for your reply.
# 4  
Old 12-07-2009
Bug

try this :
Code:
#!/bin/bash
NN=""
outputFile="/tmp/output"
while read N LINE
do
    [ "$NN" != "$N" ] && echo
    echo "$N $LINE"
    NN=$N
done < $inputfile > $outputfile

# 5  
Old 12-07-2009
Or with awk:
Code:
awk '
   (NR > 2) && (X != $1) { print "" } { X = $1 } 1
' input_file > output_file

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

# 6  
Old 12-07-2009
thanks a lot, frans.
Your code work perfectly Smilie
Do you mind roughly to explain the back story behind your code?
Thanks again.

---------- Post updated at 03:04 AM ---------- Previous update was at 03:01 AM ----------

thanks scottn.
Your code is worked as well ^^
thanks for sharing Smilie
# 7  
Old 12-07-2009
Quote:
Originally Posted by patrick87
Hi tene,
I just try the script that you suggestion, but it seems like can't work?
Do I got did anything wrong?
Thanks for your reply.
I just gave you the logic.
Did u add #!/usr/bin/ksh at the top.

What error are you getting?
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