Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Showing results for 
Search instead for 
Did you mean: 

Simple awk - printf question

Go to solution
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

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

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.

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?
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

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..

No need for any responses..


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.
Occasional Visitor

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