The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to extract certain part of log file? superHonda123 Shell Programming and Scripting 10 05-24-2008 12:56 PM
How to extract field from variable more efective pp56825 Shell Programming and Scripting 11 01-19-2008 10:34 AM
Extract a part of file name dnat UNIX for Dummies Questions & Answers 6 12-18-2007 11:19 AM
How to extract elements in a field using a number ahjiefreak Shell Programming and Scripting 17 12-13-2007 04:13 AM
Moving Part of a field to another field using AWK rjsha1 Shell Programming and Scripting 5 08-04-2006 05:39 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-10-2007
stakuri stakuri is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 3
Extract Part of string from 3rd field $3 using AWK

I'm executing "wc -lc" command in a c shell script to get record count and byte counts and writing them to a file. I get the result with the full pathname of the file. But I do not want the path name to be printed in the output file. I heard that using Awk we can get this but I don't have any knowledge on Awk. Could someone please help me.

Here is what I have in my script

#!/usr/bin/csh

echo " Records Bytes Filename" >! ${DATDIR}/hlp_NONCE_counts.rtf
wc -lc ${DATDIR}/wm_adj_mtg_actvy.dat >> ${DATDIR}/hlp_NONCE_counts.rtf

Output of the above 2 commands in the "hlp_NONCE_counts.rtf" file:

Records Bytes Filename
2040314 71024727 /export/appl/wodevl/mf/dat/wm_adj_mtg_actvy.dat

I do not want this path '/export/appl/wodevl/mf/dat/' to be printed. Instead I just need only the file name like below.
Records Bytes Filename
2040314 71024727 wm_adj_mtg_actvy.dat


Could someone please help ..

Thanks in adv,
ST
  #2 (permalink)  
Old 10-10-2007
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
See this code snippet. That should be a start.
Code:
[/tmp]$ cat try.ksh
#! /bin/ksh

set -- $(wc -lc /etc/passwd)

echo $1 $2 $3
echo $1 $2 ${3##*/}
[/tmp]$ ./try.ksh
57 2711 /etc/passwd
57 2711 passwd
[/tmp]$
  #3 (permalink)  
Old 10-10-2007
stakuri stakuri is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 3
vino,
Thanks for the snippet. But the code doesn't work since I'm using C Shell..
This command doesn't work in c shell
"set -- $(wc -lc /etc/passwd)"

I tried like below (which isn't efficient since I don't know awk)but the last echo statement isn't working. It gives error as permission denied though the file has read permission:

set a=`wc -lc /export/appl/wodevl/mf/dat/wm_adj_mtg_actvy.dat`
set xx=`echo $a | awk '{print $1,$2}'`
set yy=`echo $a | awk '{print $3}'`
set zz=`echo $yy | awk 'BEGIN {FS="/"} {print $7}'`
echo $xx | $zz >! out.rtf

Can you or anyone suggest me a simple way to do this.
Thanks,
ST
Error Message:
wodevl@dwarehouse-udb01lp1:/appl/wodevl 75 % echo $xx | $zz >! out.rtf
/export/apps/sched/local/sbin/wm_adj_mtg_actvy.dat: Permission denied.

wodevl@dwarehouse-udb01lp1:/appl/wodevl 81 % pwd
/export/appl/wodevl/mf/dat
-rw-r----- 1 wodevl wodevl 71024727 Oct 8 11:38 wm_adj_mtg_actvy.dat
  #4 (permalink)  
Old 10-10-2007
stakuri stakuri is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 3
ZS,
THANKS A LOT....
Excellent piece of code...I was just looking something like that kind of 1 line code...Since I do not know awk, I was just playing by creating variables and concatenate them which is NOT a perfect way..Your code is simple and works..

Thanks again,
ST
  #5 (permalink)  
Old 10-10-2007
zeus101 zeus101 is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 3
Code:
wc -lc ${DATDIR}/wm_adj_mtg_actvy.dat | nawk '{ n=split($3,path,"/"); print $1" "$2" "path[n] }' >>${DATDIR}/hlp_NONCE_counts.rtf
Hope this helps
ZS
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0