Inserting blank lines after string change


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting blank lines after string change
# 1  
Old 02-18-2012
Inserting blank lines after string change

My input data looks like this
Code:
...
-150 120 8
-150 122 7
-150 124 11
-150 126 8
-150 128 19
-150 130 13
-150 132 26
-150 134 38
-150 136 45
-150 138 62
-150 140 75
-150 142 110
-150 144 139
-150 146 138
-150 148 158
-150 150 173
-150 152 217
-150 154 256
-150 156 268
-150 158 280
-150 160 281
-150 162 309
-150 164 325
-150 166 313
-150 168 297
-150 170 258
-150 172 203
-150 174 203
-150 176 175
-150 178 125

-148 -136 1
-148 -138 1
-148 -140 1
-148 -142 2
-148 -144 2
-148 -146 1
-148 -148 5
-148 -150 5
-148 -152 4
-148 -154 7
-148 -156 8
-148 -158 8
-148 -160 14
-148 -162 11
-148 -164 18
-148 -166 31
-148 -168 35
-148 -170 67
-148 -172 85
-148 -174 88
-148 -176 106
-148 -178 139
-148 -40 1
-148 -44 1
-148 -52 1
-148 -60 1
-148 -62 1
-148 -64 1
-148 -74 1
-148 -96 1
-148 104 1
-148 110 1
-148 114 1
-148 116 3
-148 118 2
-148 120 8
-148 122 6
-148 124 9
-148 126 13
-148 128 23
-148 130 30
-148 132 36
-148 134 37
-148 136 53
-148 138 78
-148 140 80
-148 142 119
-148 144 134
-148 146 173
-148 148 196

..,

Every time the string changes in $1 (from -150 to -148 to -146 etc.) I wish to separate the change by a blank line. Any idea on how this might be done?
# 2  
Old 02-18-2012
Code:
awk 'FNR==1 {old=$1}
       old != $1 {printf("\n")}
       {print $0; old=$1} ' oldfile > newfile

# 3  
Old 02-18-2012
Another awk one:
Code:
awk '$1 != X && X = $1 { if(NR>1)print "" } 1' file

This User Gave Thanks to Scott For This Post:
# 4  
Old 02-18-2012
Quote:
Originally Posted by jim mcnamara
Code:
awk 'FNR==1 {old=$1}
       old != $1 {printf("\n")}
       {print $0; old=$1} ' oldfile > newfile

That actually adds a newline for every string
line, whereas I only wanted a newline separation
for a number increment (-150 to -148 etc)
# 5  
Old 02-18-2012
not sure if i complicated the simple task..but this is how...i tried for the required logic
Code:
cat test.txt|cut -d' ' -f1|sort -u |while read i
do
cat test.txt|grep ^$i >> test1.txt
echo " " >> test1.txt
 done

test.txt is your data file and test1.txt is the result ..

Last edited by ulab; 02-18-2012 at 01:34 PM..
# 6  
Old 02-19-2012
Quote:
Originally Posted by Scott
Another awk one:
Code:
awk '$1 != X && X = $1 { if(NR>1)print "" } 1' file

Small catch: this will not work for lines where $1 is 0. Alternatively:
Code:
awk '$1!=p{if(NR>1)print x; p=$1}1' infile


Last edited by Scrutinizer; 02-19-2012 at 05:59 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Inserting a blank line at the end of a .txt file?

Hi there, I am having this problem: a) I am uploading a txt file from windows (notepad) with some Gaussian 09 command lines; b) Gaussian needs in certain command files, that those files have a blank line at the end of the file! c) I open the command file with vi and no blank line at the of... (2 Replies)
Discussion started by: luismga
2 Replies

2. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

3. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

4. Shell Programming and Scripting

Inserting blank columns in already present CSV file

Hi, i have a csv file which have headers and values of it like below : headers --> CI Ref SerialNumber LastScanDate values --> VMware-42,VMware-42,Tue, 20 May 2014 11:03:44 +0000 i want to have a above csv in below format : headers --> CI Name CI Description CI Ref... (6 Replies)
Discussion started by: omkar.jadhav
6 Replies

5. Shell Programming and Scripting

Inserting new line if two sequential lines begin with the same string

Hi I have a file like this: Peter North Mary Peter North Peter Borough Mary I need there to put 'X' (or anything) on a new line between the two lines where 'Peter' begins the line. There will be many matches to this string, but they will always begin with 'Peter'. Ie, the resulting... (2 Replies)
Discussion started by: majormajormajor
2 Replies

6. Shell Programming and Scripting

Insert a string instead of blank lines

how can i insert a string sush as "###" instead of blank lines in a file? i try this code but it doesn't work! awk 'NF<1 {$1=="###" ; print$0}' in_file > out_file (13 Replies)
Discussion started by: oreka18
13 Replies

7. Shell Programming and Scripting

Delete blank lines, if blank lines are more than one using shell

Hi, Consider a file named "testfile" The contents of file are as below first line added for test second line added for test third line added for test fourth line added for test fifth line added for test (5 Replies)
Discussion started by: anil8103
5 Replies

8. Shell Programming and Scripting

Print lines after the search string until blank line is found

All I want is to look for the pattern in the file...If I found it at # places... I want print lines after those pattern(line) until I find a blank line. Log EXAMPLE : MT:Exception caught The following Numbers were affected: 1234 2345 2346 Error java.lang.InternalError:... (3 Replies)
Discussion started by: prash184u
3 Replies

9. Shell Programming and Scripting

Find a string and place two blank lines

Hi friends, I am looking for a line to find a particular string in my file and once found then replace with 2-3 blank lines before the string Example: aaa 11 bbb 1 2 3 aaa 22 bbb 4 5 6 Output (4 Replies)
Discussion started by: shaliniyadav
4 Replies

10. Shell Programming and Scripting

change of a string in different lines

hello, i have a dynamic file, which is generated by an ORACLE tool. Now i want to change a string, which begins with TABLESPACE following by "Name of Tablespace". The name of the new TABLESPACE is static. Example: TABLESPACE new : APPLI old: TABLESPACE "AMITTEL" new: TABLESPACE "APPLI" ... (4 Replies)
Discussion started by: bora99
4 Replies
Login or Register to Ask a Question