Extract Part of string from 3rd field $3 using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract Part of string from 3rd field $3 using AWK
# 1  
Old 10-10-2007
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  
Old 10-10-2007
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  
Old 10-10-2007
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
# 4  
Old 10-10-2007
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
# 5  
Old 10-10-2007
ZS,
THANKS A LOT....Smilie
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to extract part of string from all log files.?

Hi All, Let us say we have 5 log files, extract the data from all log files and save the output in a file. home/log/first.log home/log/second.log home/log/third.log home/log/four.log home/log/five.log I want to extract the following text from the log files and save the output in a file.... (7 Replies)
Discussion started by: ROCK_PLSQL
7 Replies

2. UNIX for Advanced & Expert Users

Need help to extract part of the string

Hi, I have a string with name as 20140412-s1-Potopolive_promos_20140412. So I want to extract only Potopolive string. Could you please help me the command. O/p : Potopolive Thx in advance (5 Replies)
Discussion started by: lkeswar
5 Replies

3. Shell Programming and Scripting

Print particular string in a field of csv file - part 2

Hi, all I need your help and suggestions. I want to print particular strings in a field of a csv file and show them in terminal. Here is an example of the csv file. SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw... (7 Replies)
Discussion started by: refrain
7 Replies

4. Shell Programming and Scripting

Extract number part from the string in ksh 88

I have to extract number part (Date and timestamp part ) from the following 3 strings AB_XYZA_20130930183017.log AB_DY_XYZA_20130930183017.log AB_GZU_20130930183017.log Output should be 20130930183017 Please help me to get the string like above Thanks (2 Replies)
Discussion started by: smile689
2 Replies

5. Shell Programming and Scripting

Extract part of a string

I have a file with 100s of lines of text. I want to perform an extraction of this line: Info bpzs(pid=2652) using 1000 bits I have not been able to extract it. Should I try expr match or is there another method? This line has data both in front of and in back of it. I do not have grep -o... (5 Replies)
Discussion started by: newbie2010
5 Replies

6. Shell Programming and Scripting

extract part of string using sed

Hi, I have the following string and I need to extract the date from it: TextHere,2012/07/11,1 I tried using sed: sed -n 's#^.*\({4}/{2}/{2}\).*$#\1#p' But it doesn't return anything. The following line doesn't even return '2012': sed -n 's/^.*\({4}\).*$/\1/p' I used to use grep -o... (6 Replies)
Discussion started by: Subbeh
6 Replies

7. Shell Programming and Scripting

Remove part of a string from second field

I have a file in below format (pipe delimited): 1234__abc|John__abc|xyz 3345__abc|Kate__abc|xyz 55344|Linda__abc|xyz 33434|Murray|xyz I want to remove any occurence of "__abc" in the second field of this file. I did some research and found a way to replace the entire second field with... (5 Replies)
Discussion started by: rajv
5 Replies

8. Shell Programming and Scripting

extract last part of string.

Hi, I have a string like this BUNDLE=/home/bob/flx/user.bun how to extract only the the last part ie, only user.bun (2 Replies)
Discussion started by: vprasads
2 Replies

9. Shell Programming and Scripting

Deleting every 3rd field using awk

I have a file whose format is like the following 350,2,16.2,195,2,8.0 every 3rd column of this file should be deleted. How can i achieve this tried with the following iostat -D -l 2 | /usr/xpg4/bin/awk ' NR>2 { for (i=0;i<=NF;i++)if(i%3==0)$i=""};' but no luck (3 Replies)
Discussion started by: achak01
3 Replies

10. Shell Programming and Scripting

Moving Part of a field to another field using AWK

Hi there, I have a comma seperated file with nine fields the fields are rerate: "numberTX",field2,field3,field4,field5..... I want to do this to the file reate: "field5TX",field2,field3,field4,field5 I know I can do this using AWK, but the thing giving me fits is that I... (5 Replies)
Discussion started by: rjsha1
5 Replies
Login or Register to Ask a Question