Results 1 to 10 of 27
|
|
Enjoy an ad free experience by logging in. Not a member yet? Register.
|
|
-
02-10-2006 #1
"grep sed what!?" removing line breaks?
ok i have this:
08001NAT
230026
03463
101003
101011
101013
121097
155366
155376
...
(230 lines like this)
and i want to turn it into this:
'08001NAT' '230026' '03463' '101003' ... and so on...
can grep or sed or similar replace line breaks with something else??? comma, tab, space, ???
also, would it be possible to grep 'specified file' /home/mylist.txt???
like instead of putting the whole string of 230 characters, could i just make grep read for what im looking for straight out of a file???
-
02-10-2006 #2
This does it *mostly*...

sed -e :a -e '$!N;s/\n/" "/;ta' -e 'P;D' inputfile.txt > outputfile.txt
although you're still left with the beginning and end of the final line needing a quote. I'm in a rush but that should get you started. Here's an excellent little reference that someone did:
http://www.student.northpark.edu/pem...sed1line52.txt
[EDIT] Sorry just caught the last bit - not sure what you mean exactly about grep, but absolutely grep can work on a file i.e.
grep '155' myfile.txt
Returns every line with '155' in it.
DT
-
02-10-2006 #3Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
I, for one, would do it like this:
As for that last grep bit, if I'm understanding you right (which I'm not sure if I do), what you want to do is this:Code:while read line; do echo -n "$line "; done <infile >outfile
Code:grep "$(cat somefile)" mylist.txt
-
02-10-2006 #4
yep you got it dolda2000...
thats what i meant....
i worded it all screwed up but yeah what i wanted do was
grep 'whole buncha different expressions' /home/file
instead of
grep '123' '453' '7685' '994832' '... 230times' /home/file
(which im not sure would work anyway...)
thanks a bunch Dolda2000 and Dthor!!!! Worked like a charm guys!!!
-
02-12-2006 #5Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
Or, if you're not a process hog, let the shell read the file for you:
Originally Posted by Dolda2000
[quote]
grep "$(< somefile)" mylist.txt
[/code]
Incidentally, the easiest way to get everything in a file into a single line, space separated is with echo:
Note the absence of quotes around it to avoid preserving extant whitespace. Then you can do something like:Code:echo $(< somefile)
to put quotes between the words. I'll leave you to add the first and last quotes (you have to do some of the work yourself!).Code:echo $(< somefile) | sed 's/ /' '/g
-
02-12-2006 #6Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
Indeed, scm's examples may be the best for this particular case, but they do have limitations. For example:
I'm not 100% sure, but I believe this syntax is specific to bash. If you want the code to be portable between different Bourne-compatible shells, I do not believe that this syntax should be used.
Originally Posted by scm
As long as your file is on the size of hundreds of lines, this is OK, and probably faster than most solutions (if not the most memory saving), but be aware that both bash and the kernel place limits on the length of command lines. I think the current limit is somewhere on the order of tens of thousands of arguments (65536?).
Originally Posted by scm
-
02-21-2006 #7
ok, say im doing this:
or this:Code:echo $(< somefile)
and say 22644 appears in one of the cat outputs.... but there is an entry in "mylist" that has 22644876 (this one gets filtered too) in it... but i dont want that filtered i want only 22644 filtered (filtered the right word??)...Code:grep -v "$(cat somefile)" mylist > myeditedlist
how would i change the command to do this?
-
02-21-2006 #8
Depends on what you want. If you specifically want 22644 to be followed by a space, then you can include that in your grep i.e. "22644 ". This depends on how your file is formatted and what you do and don't want to extract.
DT
-
02-21-2006 #9
but see i cant just simply "22644 " because this is merely one search term in a 1000 line text file...
ie
33699
22887
4499765
238267
238167
22644
44622
54873392COMP
68574ST
...
etc...
so would i have to add a space at the end of each line in the "expression list" file to do what i want??
-
02-21-2006 #10
It uses Regex by default('man' is your friend
).
grep 22644$ myfile
should work fine. "$" indicates end of line.
DT


Reply With Quote
