Re: File Manipulation in Linux



Marc Schwartz wrote:
Marc Schwartz wrote:

tman wrote:

I want to search a text file for certain words and pipe them to another file, each word on a seperate line. Here is an example:

File1.txt

index: A name_110022763763
index: A name_110022028309
index: A name_110022162534

I want to search File1.txt for all the name_xxxxxxxxxxxx words and pipe them to another file each word on a seperate line. The other file would look like this:

File2.txt

name_110022763763
name_110022028309
name_110022162534

Does anyone know how to do this?

Thanks


This should work:

$ cat File1.txt
index: A name_110022763763
index: A name_110022028309
index: A name_110022162534

$ grep -o "name.*" File1.txt > File2.txt

$ cat File2.txt
name_110022763763
name_110022028309
name_110022162534

See 'man grep' for further help.


Here is one more solution:

cut -d' ' -f3 File1.txt > File2.txt

This uses 'cut' to take the third field in each line, where a space character is the delimiter between fields.

See 'man cut'

HTH (also)

Marc

I would have offered to use a combo of 'awk' and 'grep'. One script and multiple output files
Of course, perl would also work quite nicely as well.
.



Relevant Pages

  • Re: File Manipulation in Linux
    ... Marc Schwartz wrote: ... I want to search File1.txt for all the name_xxxxxxxxxxxx words and pipe them to another file each word on a seperate line. ... $ cat File1.txt ... This uses 'cut' to take the third field in each line, where a space character is the delimiter between fields. ...
    (linux.redhat)
  • Re: comparing two files yy.unl and xx.dat
    ... You could use cut on one file to seperate each field to a seperate file, ... $ cat xx.dat ... Bluescreen leads to downtime. ... Downtime leads to suffering. ...
    (freebsd-questions)
  • Re: OpenBSD 3.5 Internet Gateway
    ... >>cat nat.conf ... I wanted to seperate the NAT rules, but afterwards i decided to put ...
    (comp.unix.bsd.openbsd.misc)
  • Re: [x86] kernel/audit.c cleanup according to checkpatch.pl
    ... It can easily deceive a reader to ... assume two seperate lines printed out and sometimes defeats grepping ... That's pretty much the only grep pattern that would break. ...
    (Linux-Kernel)
  • Re: File Manipulation in Linux
    ... tman wrote: ... I want to search File1.txt for all the name_xxxxxxxxxxxx words and pipe them to another file each word on a seperate line. ... $ cat File1.txt ... Marc Schwartz ...
    (linux.redhat)