Find the answer to your Linux question:
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 27
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #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???

  2. #2
    Linux User DThor's Avatar
    Join Date
    Jan 2006
    Location
    Ca..na...daaa....
    Posts
    319
    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

  3. #3
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    I, for one, would do it like this:
    Code:
    while read line; do echo -n "$line "; done <infile >outfile
    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:
    grep "$(cat somefile)" mylist.txt

  4. $spacer_open
    $spacer_close
  5. #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!!!

  6. #5
    Quote Originally Posted by Dolda2000
    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:
    grep "$(cat somefile)" mylist.txt
    Or, if you're not a process hog, let the shell read the file for you:
    [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:
    Code:
    echo $(< somefile)
    Note the absence of quotes around it to avoid preserving extant whitespace. Then you can do something like:
    Code:
    echo $(< somefile) | sed 's/ /' '/g
    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!).

  7. #6
    Linux 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:
    Quote Originally Posted by scm
    grep "$(< somefile)" mylist.txt
    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.
    Quote Originally Posted by scm
    echo $(< somefile)
    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?).

  8. #7
    ok, say im doing this:

    Code:
    echo $(< somefile)
    or this:

    Code:
    grep -v "$(cat somefile)" mylist > myeditedlist
    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??)...

    how would i change the command to do this?

  9. #8
    Linux User DThor's Avatar
    Join Date
    Jan 2006
    Location
    Ca..na...daaa....
    Posts
    319
    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

  10. #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??

  11. #10
    Linux User DThor's Avatar
    Join Date
    Jan 2006
    Location
    Ca..na...daaa....
    Posts
    319
    It uses Regex by default('man' is your friend ).

    grep 22644$ myfile

    should work fine. "$" indicates end of line.

    DT

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •