extract string

Paul Lussier p.lussier at comcast.net
Tue Jan 10 15:05:01 EST 2006


Zhao Peng <greenmt at gmail.com> writes:

> Hi
>
> Suppose that I have a file called abc.txt, which contains the
> following 5 lines (columns are delimited by ",")
>
> "name","age","school"
> "jerry" ,"21","univ of Vermont"
> "jesse","28","Dartmouth college"
> "jack","18","univ of Penn"
> "john","20","univ of south Florida"
>
> My OS is RedHat Enterprise, how could I extract the string which
> contains "univ" and create an output file called def.txt, which only
> has 3 following lines:
>
> univ of Vermont
> univ of Penn
> univ of south Florida
>

Here are 3, pick your poison:

  awk -F, '/univ/ && gsub(/\"/,"") {print $3}' abc.txt > def.txt
  perl -F, -ane 'if (/univ/) { $F[2] =~ s/\"//g; print $F[2]};' abc.txt \
    > def.txt
  grep univ abc.txt | cut -f3 -d, | sed 's/\"//g' > def.txt


> Please suggest the simplest command line approach.

Simplest by what measurement?

 - fewest processes spawned
 - most efficient
 - least amount of typing
 - easiest to remember
 - easiest to understand
 - ability to debug
 - extensibility

Simplest is a rather subjective approach...





-- 

Seeya,
Paul



More information about the gnhlug-discuss mailing list