nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

using sed to delete everything except needed patterns

This is a discussion on using sed to delete everything except needed patterns within the Shell scripting forums, part of the Development/Scripting category; I have floating point data (193 rows & 2 column in a file from which i would like to delete ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 09-07-2005, 11:24 AM
guest
Guest
 
Posts: n/a
Default using sed to delete everything except needed patterns

I have floating point data (193 rows & 2 column in a file from which i would like to delete every row except some required rows. I have written a test.sed file which had
' pattern1/p'
' pattern2/p'
.....
and after that used
sed -f test.sed input_file > output
however this was not appending the whole input file to the output file but my required patterns were written twice in the output.
How do i delete all other lines(row.......?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-08-2005, 01:46 AM
kavi
Guest
 
Posts: n/a
Default

use

Code:
sed -n -f test.sed input_file > output_file
all the needed patterns are in output_file.
Reply With Quote
  #3 (permalink)  
Old 09-08-2005, 10:44 AM
guest
Guest
 
Posts: n/a
Default new problem

got a new problem, in my test.sed file i used the pattern /0.400/p to get the two columns with 0.400 in column one. however i also have a 14.400 in the input file.
now the 14.400 and its corresponding column two also got copied to the output file. This also happened with two other values........is there a way that i can select only 0.400 and not 14.400 or 3.400 ?
Reply With Quote
  #4 (permalink)  
Old 09-08-2005, 06:14 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

Well create file and then omit those file with grep or better write awk script. Whenever you have colum based data it is better to use awk, IMPO.
Reply With Quote
  #5 (permalink)  
Old 09-10-2005, 06:59 AM
Junior Member
User
 
Join Date: Sep 2005
Posts: 24
Rep Power: 0
kavi
Default

data reads different in C program
I wrote a Cprogram to just read the output file created using our above sed lines.
the output file was 12703 bytes after removing ^M (discussed in our forum topic 'contents of file') the total no of floating pt data is 1760.
So diving file size by 1760, i get 7.2176 bytes per each data. i dont know how many bytes shell script used to represent floating pt.
I declared malloc for double data. then read 7 bytes and printed it..........some differnt data got printed. then i tried reading 6 bytes & 4 bytes still it did not work.

does this mean the output file created by shell is in a different format??
Reply With Quote
  #6 (permalink)  
Old 09-10-2005, 06:59 AM
Junior Member
User
 
Join Date: Sep 2005
Posts: 24
Rep Power: 0
kavi
Default

data reads different in C program
I wrote a Cprogram to just read the output file created using our above sed lines.
the output file was 12703 bytes after removing ^M (discussed in our forum topic 'contents of file') the total no of floating pt data is 1760.
So diving file size by 1760, i get 7.2176 bytes per each data. i dont know how many bytes shell script used to represent floating pt.
I declared malloc for double data. then read 7 bytes and printed it..........some differnt data got printed. then i tried reading 6 bytes & 4 bytes still it did not work.

does this mean the output file created by shell is in a different format??
Reply With Quote
  #7 (permalink)  
Old 09-10-2005, 04:03 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

Data should remain same even if you use the shell script or C program. It should not change it. Can you put your sample input data as well as what output you need from that data. Btw I also love to do C coding. It is kind of fun not a big programmer but for fun only.
Reply With Quote
  #8 (permalink)  
Old 09-11-2005, 10:02 AM
Junior Member
User
 
Join Date: Sep 2005
Posts: 24
Rep Power: 0
kavi
Default

Quote:
Originally Posted by monk
Data should remain same even if you use the shell script or C program. It should not change it. Can you put your sample input data as well as what output you need from that data. Btw I also love to do C coding. It is kind of fun not a big programmer but for fun only.
hi,
ya i acutally got the solution. shell wrote the output file as characters. so if data was 16.2345 then the seven characters each had one byte and that y my file size was about 1760x7.something(decimal) data in the c code i was reading float data using read(fp,buf,sizeof(float)) and this was giving me trash.
Now i changed the code to
Code:
while(!feof(fp)){
		fscanf(fp,"%f",&f);
		/*printf("%f\n",f);*/
		fwrite(&f,sizeof(float),1,fout);
		
	}
the data in f was float and then when i wrote all data to a new file, this time the file size was exactly 1760(float data) x 4 = 7040.
Can give me a small C program to draw a graph. As i mentioned in the beginning, i have 2 columns of data and i just want to plot a graph in C with colm 1 as x axis and all the different column 2's from all the .txt files as y axis. Note: column 1 data is same in all .txt files.
Reply With Quote
  #9 (permalink)  
Old 09-12-2005, 03:57 AM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

Okay try out following urls

Plplot online doc: http://plplot.sourceforge.net/docboo...lot-html-5.5.3

An example of Plplot: http://plplot.sourceforge.net/examples/index.html

Try it and lemme know it...
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
postfix + google = headache. your help needed TECK Linux software 2 02-26-2008 05:45 PM
equivalent command needed in both Solaris and Linux bglassberg Linux software 1 02-21-2007 09:36 AM
Guidance needed jithendra Linux software 2 11-07-2006 10:26 AM
Help needed regarding bash scripting of a webpage chris411 Shell scripting 2 09-07-2006 11:40 AM
Help needed regarding bash scripting ---full description chris411 Shell scripting 1 09-07-2006 05:21 AM


All times are GMT +5.5. The time now is 05:27 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36