formatting the fields

hi i am using the following code in my shell script

awk -F":" '{printf("%-20.20s:%-20.20s:%-20.20s:%-20.20s:%-15.15s:%-3.3s:%-19.19s:%-2.2s:%-20.20s:%-15.15s:%-2.2s\n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)}' $DIALPBIN/temp1.txt > ${DIALPBIN}/temp2.txt

actually the 5th field should not contain spaces or any special character.:confused:
what is the solution?

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

******************************************************

Can you please give an excerpt of your input file and the wanted output? Use code tags please.

what my code is doing, it is executing a sql file and the resullset of the query is getting stored in the text file in a fixed format. for that fixed format i have used the following code::

awk -F":" '{printf("%-20.20s:%-20.20s:%-20.20s:%-20.20s:%-15.15s:%-3.3s:%-19.19s:%-2.2s:%-20.20s:%-15.15s:%-2.2s\n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)}' $DIALPBIN/temp1.txt > ${DIALPBIN}/temp2.tx

now 5th filed is a phone number. in the database it contains brackets like (0124)-23456
but in the file the output should be like 012423456

Could you please help????

Nvm.

Example:

echo "(0124)-23456"| awk '{gsub(/[()-]/,""); print}'
012423456

So in your awk line you might change following part:

... $2,$3,$4,(gsub(/[()-]/,"",$5)),$6,$7,...

Have currently no example to test it but it should be alike, sorry.

this is not working..

I missed a closing bracket. Maybe try this again. Corrected it in my former post. If it still does not work, give a complete example line, ty.

---------- Post updated at 02:41 PM ---------- Previous update was at 02:34 PM ----------

Ok, found the real problem you can see by the example how it works:

echo "aa (0124)-23456 bb"| awk '{gsub(/[()-]/,"",$2); printf("%s %s %s\n", $1,$2,$3)}'
aa 012423456 bb

So keep your line as is but add in front of the printf add the gsub part. Just change the $2 in the gsub into $5.

hi

i am getting these two errors

awk: syntax error near line 1
awk: illegal statement near line 1

---------- Post updated at 07:55 AM ---------- Previous update was at 07:53 AM ----------

gsub is not found..actually mine is a solaris OS.
could u suggest somne other option

Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.

Try with other versions of awk like nawk , gawk .

when i am running

echo "aa (0124)-23456 bb" | awk '{printf("%s %s %s\n", $1,$2,$3)}'

it is giving the correct output
but if i am running

echo "aa (0124)-23456 bb" | awk '{gsub(/[()-]/,"",$2) ; printf("%s %s %s\n", $1,$2,$3)}'

it is giving error

what could be the cause?

---------- Post updated at 08:28 AM ---------- Previous update was at 08:26 AM ----------

actually its working with nawk.....but white spaces are still there....they are not getting removed

could you please suggest some change

---------- Post updated at 08:29 AM ---------- Previous update was at 08:28 AM ----------

actually it working with nawk..but the white spaces are still there..
could you plz suggest something to remove those white spaces

What version of awk are you using?
What is the error output?

its working...

just to remove whitespaces...how to do that?

plz help

---------- Post updated at 08:32 AM ---------- Previous update was at 08:30 AM ----------

one more thing it is not removing all the special characters in the field like * & etc.

Something like that :

^$ > echo "aa *&+|(0124)-23456 bb" | awk '{gsub(/[^[:alnum:]]*/,"",$2); print $1 $2 $3}'
aa012423456bb
$

Jean-Pierre.

i tried with

 
echo "aa *&+|(0%124)-23456#$@!+= bb" | nawk '{gsub(/[^[:alnum:]]*/,"",$2); print $1 $2 $3}'

the expected output is 012423456
but the output that is coming is aabb

plz help:confused:

echo "(0124)-23456" | awk '{ gsub(/[^0-9]/,"",$0);print }'

thanx a lot....its working

---------- Post updated 08-05-09 at 01:22 AM ---------- Previous update was 08-04-09 at 09:15 AM ----------

actually again i am facing some problem

if am doing like this

nawk -F":" '{printf("%-20.20s:%-20.20s:%-20.20s:%-20.20s:%-15.15s:%-3.3s:%-19.19s:%-2.2s:%-20.20s:%-15.15s:%-2.2s\n", $1,$2,$3,$4,gsub(/[^0-9]/,"",$5),$6,$7,$8,$9,$10,$11)}' $DIALPBIN/temp1.txt > ${DIALPBIN}/temp2.txt
echo "TRLR|${count1}" >> ${DIALPBIN}/temp2.txt

then the output comes out to be like this

hyperarc            :199.37.175.53       :hyperarc            :199.37.175.53       :3              :wn3:dial10             :3
 :syrnypritc          :PALMYRA        :NY
hyperarc            :199.37.175.53       :hyperarc            :199.37.175.53       :0              :wn3:dial10             :3
 :fbkaklocal          :FAIRBANKS      :AK
hyperarc            :199.37.175.53       :hyperarc            :199.37.175.53       :0              :wn3:dial10             :3
 :syrnypritc          :POPLAR RIDGE   :NY
hyperarc            :199.37.175.53       :hyperarc            :199.37.175.53       :0              :wn3:dial10             :3
 :syrnypritc          :ROME           :NY

see the fifth field only 3 has come..

if am using gsub in front of print like:

nawk -F":" '{gsub(/[^0-9]/,"",$5) ; printf("%-20.20s:%-20.20s:%-20.20s:%-20.20s:%-15.15s:%-3.3s:%-19.19s:%-2.2s:%-20.20s:%-15.15s:%-2.2s\n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)}' $DIALPBIN/temp1.txt > ${DIALPBIN}/temp2.txt
echo "TRLR|${count1}" >> ${DIALPBIN}/temp2.txt

then i am getting the file like this

hyperarc  199.37.175.53  hyperarc 199.37.175.53 315479980 3 dial10 3
hyperarc            :199.37.175.53       :hyperarc            :199.37.175.53       :315479980     :wn3:dial10             :3
 :syrnypritc          :PALMYRA        :NY
hyperarc            :199.37.175.53       :hyperarc            :199.37.175.53       :9074516030     :wn3:dial10             :3
 :fbkaklocal          :FAIRBANKS      :AK
hyperarc            :199.37.175.53       :hyperarc            :199.37.175.53       :3155639010     :wn3:dial10             :3
 :syrnypritc          :POPLAR RIDGE   :NY




see the fiorst line is getting repeat and without colons..
what to do....:confused:

Priyanka,

Can You Pls post the exact input your giving and the output your expecting.

now i am geting my output....thanx to all:b: