Operating System - HP-UX
1752845 Members
3789 Online
108789 Solutions
New Discussion юеВ

Simple awk - printf question

 
SOLVED
Go to solution
SHABU KHAN
Trusted Contributor

Simple awk - printf question

Hi All,

I am trying to process a file with uneven spaces (actually I found a pattern which is two or more spaces) to insert pipes as field delimiters and I am successful in doing so ... but I would like to keep the pipe for further processing in the shell script how do I do that ? I am not that familiar with printf ? Any help would be appreciated .. here is an example:

prompt>cat $testfile
string1<2spaces>string2<10spaces>string3<6spaces>....

prompt>grep "WF" ${testfile} | grep -i "online" | grep -v -i "gentrig" | sed 's/[ ][ ] */\|/g' | awk -F\| '{print $1,$6,$9,$10}' | while read SIEB_SRVR STATUS D
ATE TIME

but unfortunately the second field ($6 in this case) is a string with spaces and dashes ... so it is a hindrance for further processing and hence I want to keep the PIPE even after splitting the fields ???

Any ideas would be appreciated..

I found a workaround but would like to keep it simple ...

I can explain further if needed.

Thanks,
Shabu
4 REPLIES 4
Deepak Extross
Honored Contributor

Re: Simple awk - printf question

Hi Shabu,
I'm not quite clear about your requirements..if you aren't considering spaces as party of the string, have you considered doing a
tr -s " "
on the string first to eliminate duplicate spaces?
SHABU KHAN
Trusted Contributor

Re: Simple awk - printf question

Hi All,

Never mind ... Deepak and All,

I fixed it ...

This is what I did:
-------------------
WKFS=`grep "WF" ${SMGR_LOG} | grep -i "online" | grep -v -i "gentrig"`

WKFS_TRIM=`echo "${WKFS}" | sed -e 's/[ ][ ] */\|/g' -e 's/ [0-9][0-9]/\|&/g'`

echo "${WKFS_TRIM}" | sed -e 's/\| /\|/g' -e 's/ /_/g' | awk -F\| '{print $1,$2,$6,$9,$10}' | while read SIEB_SRVR WKF STATUS DATE TIME

do
WF_STATUS_SCRATCH=`echo "${WKF}" | sed -e 's/_/ /g'`
WF_STATUS_ORIG=`grep "${WF_STATUS_SCRATCH}" ${SMGR_LOG} | grep "Running"`

if [ -z ${WF_STATUS_SCRATCH}" ]; then
print messages ...
notify etc..
fi

No need for any responses..

Thanks,
Shabu

Deepak Extross
Honored Contributor

Re: Simple awk - printf question

part part ... i meant part not party.
still in the weekend mood. monday morning arrived too fast :-)

[N/A] pls.
waknine
New Member
Solution

Re: Simple awk - printf question

in awk , you can tell which outpout field separator you want to use. In your case you want to keep the | separator in outpout . You can write
awk -F\| 'BEGIN {OFS = "|" }
{ print $1,$2,...} ' file

best regards
Marc