ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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).......?
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
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.
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.
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 ?
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 .. :}
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.