f



AWK

hi,

I am having following txt file.
-------------------------------------------------------------------
00032    Clemson Univ, Howard , Dept Chem, Clemson, SC 29634 USA.
00032    Clemson Univ, Howard , Films, Clemson, SC 29634 USA.
10032    Oak Ridge Natl Lab, High , Oak Ridge, TN 37831 USA.
12029    Oak Ridge Natl Lab,  Oak Ridge, TN 37831 USA.
00029    Univ Texas, Dept Phys, Austin, TX 78712 USA.

------------------------------------------------------------------

i want output in following manner.


00032 Clemson Univ
00032 Clemson Univ
10032 Oak Ridge Nat1 Lab
12029 Oak Ridge Nat1 Lab
00029 Univ Texas


i.e first filed (number), and second filed upto first comma.

0
friend.05 (36)
10/26/2005 4:29:27 PM
comp.unix.questions 2631 articles. 0 followers. Post Follow

3 Replies
712 Views

Similar Articles

[PageSpeed] 20


friend.05@gmail.com wrote:

> hi,
> 
> I am having following txt file.
> -------------------------------------------------------------------
> 00032    Clemson Univ, Howard , Dept Chem, Clemson, SC 29634 USA.
> 00032    Clemson Univ, Howard , Films, Clemson, SC 29634 USA.
> 10032    Oak Ridge Natl Lab, High , Oak Ridge, TN 37831 USA.
> 12029    Oak Ridge Natl Lab,  Oak Ridge, TN 37831 USA.
> 00029    Univ Texas, Dept Phys, Austin, TX 78712 USA.
> 
> ------------------------------------------------------------------
> 
> i want output in following manner.
> 
> 
> 00032 Clemson Univ
> 00032 Clemson Univ
> 10032 Oak Ridge Nat1 Lab
> 12029 Oak Ridge Nat1 Lab
> 00029 Univ Texas
> 
> 
> i.e first filed (number), and second filed upto first comma.
> 

awk -F, '{$1=$1;print $1}' file

0
morton (234)
10/26/2005 4:49:17 PM
In <1130344167.350437.11520@g43g2000cwa.googlegroups.com> "friend.05@gmail.com" <hirenshah.05@gmail.com> writes:

> I am having following txt file.
> -------------------------------------------------------------------
> 00032    Clemson Univ, Howard , Dept Chem, Clemson, SC 29634 USA.
> 00032    Clemson Univ, Howard , Films, Clemson, SC 29634 USA.
> 10032    Oak Ridge Natl Lab, High , Oak Ridge, TN 37831 USA.
> 12029    Oak Ridge Natl Lab,  Oak Ridge, TN 37831 USA.
> 00029    Univ Texas, Dept Phys, Austin, TX 78712 USA.
> ------------------------------------------------------------------

> i want output in following manner.

> 00032 Clemson Univ
> 00032 Clemson Univ
> 10032 Oak Ridge Nat1 Lab
> 12029 Oak Ridge Nat1 Lab
> 00029 Univ Texas

> i.e first filed (number), and second filed upto first comma.

Easy -- just tell awk that you want the fields to be separated by
commas instead of spaces by using the -F argument:

  awk -F, '{print $1}' textfile

This example does preserve the spacing between the number and the start
of the university name, so if you want to eliminate the extra spaces, pass
the output though sed, like so:

  awk -F, '{print $1}' textfile | sed 's/  */ /'

-- 
John Gordon            "It's certainly uncontaminated by cheese."
gordon@panix.com

0
gordon16 (684)
10/26/2005 4:53:17 PM
Ed Morton wrote:
 >
 >
 > friend.05@gmail.com wrote:
 >
 >> hi,
 >>
 >> I am having following txt file.
 >> -------------------------------------------------------------------
 >> 00032    Clemson Univ, Howard , Dept Chem, Clemson, SC 29634 USA.
 >> 00032    Clemson Univ, Howard , Films, Clemson, SC 29634 USA.
 >> 10032    Oak Ridge Natl Lab, High , Oak Ridge, TN 37831 USA.
 >> 12029    Oak Ridge Natl Lab,  Oak Ridge, TN 37831 USA.
 >> 00029    Univ Texas, Dept Phys, Austin, TX 78712 USA.
 >>
 >> ------------------------------------------------------------------
 >>
 >> i want output in following manner.
 >>
 >>
 >> 00032 Clemson Univ
 >> 00032 Clemson Univ
 >> 10032 Oak Ridge Nat1 Lab
 >> 12029 Oak Ridge Nat1 Lab
 >> 00029 Univ Texas
 >>
 >>
 >> i.e first filed (number), and second filed upto first comma.
 >>
 >
 > awk -F, '{$1=$1;print $1}' file
 >

Make that either of these:

	awk -F, '{sub(/  */," ");print $1}' file
	awk 'sub(/,.*/,"")($1=$1)' file

Regards,

	Ed.
0
morton (234)
10/26/2005 5:04:21 PM
Reply: