translate sed to awk

hi,

can someone tell me how can I translate the following line from sed to awk?

`sed 's/^[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:\([^:]*\):.*/\1/
awk -F":" '{print $6}' file

hi bartus

thank you.

now can i translate

=`sed 's/^[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:\([^:]*\):.*/\1/' file|awk '{ printf "%-2s", $1 }'`

to

awk -F":" '{print $6}' file |awk '{ printf "%-2s", $1 }'`

Thanks

awk -F":" '{ printf "%-2s", $6 }'

HTH
--ahamed

Thanks Ahmed,

what about the file?

Thanks

awk -F":" '{ printf "%-2s", $6 }' file

--ahamed

Hello Ahmed and everyone,

Now I have another issue. I have a flat file as example test
inside this file I have information as following :
user:date:number:phone (all using : as delimiter)

now I am passing $VAR1, $VAR2, $VAR3 and $VAR4

as example if I want to modify date inside flat file I am trying to use something like this

echo $VAR2| awk -F ":" '{printf %-2s,$2}'test

but I am getting error, do you know how can i resolve this?

Thanks

What's the contents of VAR2?

What's the contents of 'test'?

What do you expect to get out?

You're missing lots of quotes, brackets, and spaces:

echo "$VAR2" | awk -F: '{ printf("%-2s", $2) }' file

That's correct syntax but I still think your logic is incorrect. Show the data you have and the data you want please.

my data is inside test file is as following:

jsmith:12-11-2011:r234:50

now as I said before I am getting passing 4 variables to over-write the file :

as example I am passing $var2 to modify the date

the new date is going to be 14-11-2011 and will replace the previous date

i was thinking i can use

echo $var2| awk -F ":" '{printf %-2s, $2}'test

but it doesnt work.

Thanks again

now

I still don't understand you. You want to replace field 2 in all lines? Or only one line? or what?

You're not "passing" it to awk. You're feeding it into its standard input, something awk doesn't even read when you give it a filename. you pass variables with -v

If you want to replace field 2 in all lines:

awk -v VAR2="$VAR2" -v OFS=":" -F: '{ $2=VAR2 } 1' file

sorry , you are right I am not explaining very well.

I am trying to find one instance and replace with variable.

as i said

jsmith:12-11-2011:55:r234

I am going to find 12-11-2011 in al llines and replace it with new $VAR2.

Thanks again for your help

awk -F: -v OFS=":" -v OLD="12-11-2011" -v NEW="$VAR2" '$2 == OLD { $2=NEW } 1' inputfile > outputfile

sorry to keep bothering you.

my input is not a file is a user entry coming from a read command .

I have tried your way but it just over-writing the file and makes an empty file.

i did as following :

awk -F: -v OFS=":" -v OLD="a2345" -v NEW="r2345" '$4 == "a2345" { $4="r2345" } 1' test2 > test2

Thanks again

That's not going to work for user input then, because you hardcoded it. Remember that the -v variables define what OLD and NEW mean inside awk, so you don't have to:

awk -F: -v OFS=":" -v OLD="a2345" -v NEW="r2345" '$4 == OLD { $4=NEW } 1' test2 > test2

so you can substitute:

awk -F: -v OFS=":" -v OLD="a2345" -v NEW="$VAR2" '$4 == OLD { $4=NEW } 1' test2 > test2

Thank you very much. it worked!!