LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-07-2005, 01:53 PM   #1
manjushp
LQ Newbie
 
Registered: Oct 2004
Posts: 20

Rep: Reputation: 0
selecting patterns in file


I have floating point data (193 rows & 2 columns) 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(rows).......?
 
Old 09-07-2005, 03:01 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Code:
grep  -f test.sed input_file > outputfile
keeps only the stuff in input_file based on the patterns in test.sed

grep -v removes the stuff in the patterns in test.sed and keeps everything else. -- not clear to me which one you want

Last edited by jim mcnamara; 09-07-2005 at 03:03 PM.
 
Old 09-07-2005, 03:15 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,983
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Re: selecting patterns in file

Quote:
Originally posted by manjushp

sed -n -f test.sed input_file > output

Cheers,
Tink
 
Old 09-07-2005, 03:15 PM   #4
manjushp
LQ Newbie
 
Registered: Oct 2004
Posts: 20

Original Poster
Rep: Reputation: 0
hi jim,
thanx for the reply. i tried the command u said ( grep -v.....) but it copied the whole input file to the output file. It might have been because test.sed had a /p option in it.
but this one seems to work
sed -n -f test.sed input_file > output_file
as it did not print anything else other than the pattern in test.sed (with th /p option) and redirected it to the output file.
what do u say about this
 
Old 09-07-2005, 03:18 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,983
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Sorry mate, maybe I don't understand what you try to
achieve or say ... can you reword what the expected
result is?


Cheers,
Tink
 
Old 09-07-2005, 03:32 PM   #6
manjushp
LQ Newbie
 
Registered: Oct 2004
Posts: 20

Original Poster
Rep: Reputation: 0
sorry if my phrase was confusing........
I have a file with 2 columns of floating pt. data ( many rows). i only want some rows and not all. And this selection is based on the first column values. So if type in all the first column values "that i need" into a test.sed file then how can i remove all other rows which are not necessary for me? That was my question. I did use the /p option in the test.sed file. as in my first post. so the output file had both the required data and the unrequired data but with /p option the required data got repeated. This only made my problem worse.

however
sed -n -f test.sed input_file > output_file
seems to have solved the problem ( with -n option).

Thats for what happened.
Now in the output file i dont want the data placed line by line. ( when i use less outputfile or vim outputfile i find a ^M after each line.^M stands for ENTER/return) I want them placed next to each other without ^M. I tried some commands but they did not work out.
 
Old 09-07-2005, 06:42 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,983
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
What's the origin of your file, did the file have the
C-M originally?


Cheers,
Tink

Last edited by Tinkster; 09-08-2005 at 02:15 AM.
 
Old 09-07-2005, 10:20 PM   #8
manjushp
LQ Newbie
 
Registered: Oct 2004
Posts: 20

Original Poster
Rep: Reputation: 0
yes the inputfile is a .txt file from which i am trying to coming with the data that i need. the ^M is not there when i try "less file.txt" but in the output file is do see ^M after every set of values that i obtained by the sed operation mentioned above.
 
Old 09-07-2005, 11:12 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,983
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
What platform are you on?
 
Old 09-07-2005, 11:13 PM   #10
manjushp
LQ Newbie
 
Registered: Oct 2004
Posts: 20

Original Poster
Rep: Reputation: 0
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 ?
 
Old 09-07-2005, 11:41 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,983
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
You need to escape the . with a \


0\.400
 
Old 09-08-2005, 01:31 AM   #12
manjushp
LQ Newbie
 
Registered: Oct 2004
Posts: 20

Original Poster
Rep: Reputation: 0
hi andre,
that did not work out. i replaced all the . 's (dots) with \. but it resulted the same.
 
Old 09-08-2005, 02:15 AM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,983
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Can you post a sampe of your original file, and the
values that gave you problems? Also, you didn't
answer my question what OS/environment you're
using .. :}


Cheers,
Tink
 
Old 09-08-2005, 02:25 AM   #14
manjushp
LQ Newbie
 
Registered: Oct 2004
Posts: 20

Original Poster
Rep: Reputation: 0
ok the input file looks like this........posting only some samples not possible to post all........

2.316 49.7220
2.312 49.8296
2.296 50.4684
2.292 50.3888
2.288 50.5628
2.284 50.7176
2.280 50.8542
.....
......
0.476 12.0300
0.472 11.9727
0.468 11.9170
0.464 11.8480
now i wrote the test.sed as
/2\.308/p
/2\.296/p
/2\.288/p
/2\.276/p
..........
.........
/0\.468/p
/0\.460/p
now i use some thing like this : sed -n -f test.sed inputfile > outputfile

now the outputfile is like this
2.308 50.0838
2.296 50.4684
2.296 50.4684
2.288 50.5628
2.276 50.9888
...............
.......
0.480 12.0624
0.468 11.9170
0.460 11.7603

Now from the input and test.sed my output should have only one (2.296 50.4684) but i have two? reason: because i am also looking for a pattern with .468 so sed also considered the .468 of the 50.4684 of this value and printed that line twice. now my search critirea is only column one of the data. Not only that sed is considering repeated patterns in column one also........
hope u understand the question
 
Old 09-08-2005, 01:38 PM   #15
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,983
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
If the search-criterial is always in the first column all
you need to do is
Code:
/^2\.308/p
/^2\.296/p
/^2\.288/p
/^2\.276/p
/^0\.468/p
/^0\.460/p
to avoid the false positive on the 50.4684


Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting text between two different patterns activeco Linux - General 7 09-13-2005 06:05 PM
Remembering patterns and printing only those patterns using sed bernie82 Programming 5 05-26-2005 05:18 PM
design of badram patterns? mritch Linux - Hardware 0 05-22-2004 03:10 AM
Easy problem, selecting file types Daredevil Linux - General 1 04-28-2004 12:58 PM
Searching patterns from file MichaelVaughn Programming 1 04-06-2004 11:18 AM


All times are GMT -5. The time now is 09:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration