![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Replace one digit by two digit using sed | Jae | Shell Programming and Scripting | 5 | 04-17-2008 09:32 PM |
| why "expr "${REPLY}" : '\([1-9][[:digit:]]*\)" can figure out whether it is a digit? | sleepy_11 | Shell Programming and Scripting | 6 | 08-08-2007 12:51 AM |
| adding a 6 digit number retaining 0s on the left | kanchan_cp | Shell Programming and Scripting | 8 | 03-08-2007 06:58 AM |
| Extracting 10 digit number from txt files | c19h28O2 | Shell Programming and Scripting | 9 | 08-07-2006 03:06 PM |
| calculating a check digit | jim9418 | UNIX for Dummies Questions & Answers | 1 | 02-16-2005 12:14 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi All,
How can i convert a number 24 to 0024 In the same way how can i convert 123 to 0123? All this has to be done inside a script Thanks in advance JS |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
If you have printf, that's the ticket.
Code:
vnix$ printf '%04i\n' 24 0024 Code:
echo 24 | awk '{ printf "%04i\n", $0 }'
Last edited by era; 04-23-2008 at 01:23 AM. Reason: awk as fallback |
|
#3
|
|||
|
|||
|
if you would like the transformed value inside the same variable then you could use
one=1 one=`printf "%04i" $one` |
|
#4
|
|||
|
|||
|
try typeset
$ typeset -R4Z tmp
$ tmp=23 $ echo $tmp 0023 $ |
|
#5
|
|||
|
|||
|
Hi All,
I have a file in teh format given below: catanimal 0 animal 90 number90 cat_number_name catanimal 1 animal 91 number91 cat_animal_name catanimal 2 animal 92 number92 cat_name_animal kiwiani 0 bird 90 number90 kiwi_number_name kiwiani 1 bird 91 number91 kiwi_animal_name kiwiani 2 bird 92 number92 kiwi_name_animal cat 0 animal 90 number90 cat_number_name cat 1 animal 91 number91 cat_animal_name cat 2 animal 92 number92 cat_name_animal bat 0 animal 90 number90 bat_number_name bat 1 animal 91 number91 bat_animal_name bat 2 animal 92 number92 bat_name_animal When i use awk '$1 ~ /bat/ {if ($2==0){print $5} } ' file , i get the result i want But if i give awk '$1 ~ /cat/ {if ($2==0){print $5} } ' file, I get result for $1=cat and $1=catanimal where as i want the result for only $1="cat" Thanks in advance JS |
|
#6
|
|||
|
|||
|
awk '$1 ~ /^cat / {if ($2==0){print $5} } ' file,
|
|
#7
|
|||
|
|||
|
Or use equality (==) instead of pattern matching (~). The ~ operator will match the pattern anywhere in the field.
Code:
awk '$1 == "cat" && $2 == 0 { print $5 }' file
|
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|