You are not logged in.

#1 2008-02-10 12:29:13

drakosha
Member
Registered: 2006-01-03
Posts: 253
Website

awk/smth_else wizard required :)

Hi!

I'd like to do the following: having a file with let's say 2 columns:

a 1
b 2
c 3

I'd like to get a file with 2 columns, while one of the columns is passed to a certain command and other is not. For example if 1 2 3 are dates, i'd like to convert it to other format using date and get something like:

a 1/1/01
b 2/1/01
c 3/1/01

I know i can write a python/c/etc program, but i was looking for some kind of oneliner.
Any suggestions?

Offline

#2 2008-02-10 12:46:22

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: awk/smth_else wizard required :)

When you just want to add something to the last column:

sed 's#$#/1/01#' yourfile

But there are a lot of possibilities:
sed 's#\(anycolumn's\) \(changing column\)#\1\2changes#' yourfile. Or use awk:
awk '{print $1 $2"changes"}' yourfile. Every column (defauld seperator is space) specified by '$n' with n symbolizing it's (order) number.

Offline

#3 2008-02-10 12:48:12

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: awk/smth_else wizard required :)

If you need more explanation, ask... I'd recommend you have a look at the man-pages of awk and sed too.

Offline

#4 2008-02-10 14:08:46

drakosha
Member
Registered: 2006-01-03
Posts: 253
Website

Re: awk/smth_else wizard required :)

I'm familiar with sed and awk, but i'm not a guru. The idea is i want to execute a command - something similar to `` in bash - on some column(s) and get the output from the command, not simple string replace.
Let's say replace username with user home directory or something like that. I hope the example is clear.

Offline

#5 2008-02-10 16:32:40

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: awk/smth_else wizard required :)

If you give an example of the command you are referring at, that'd be helpful. The date example you gave is just a substitution/replace. If you want to replace the username with the user home directory it still simply is a replace. No need for anything more advanced then the answers I gave you.
But if you can come up with something more complicated: rrr show me smile

Offline

#6 2008-02-11 07:59:02

drakosha
Member
Registered: 2006-01-03
Posts: 253
Website

Re: awk/smth_else wizard required :)

OK smile
Let's say i want to replace user name with user id:

>id -u ftp
14
>id -u root
0

I.e. having file:

root a b c
ftp d e f

i want to have:

0 a b c
14 d e f

Just an example which illustrates the point that i need to run a command on some column(s)

Offline

#7 2008-02-11 12:04:13

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: awk/smth_else wizard required :)

I understand what you mean now. Although you could still use replace functions because you normally know what user id's belong to which user.
I couldn't come up with something real nice in awk... so here's a solution using sed, piping to bash:

echo "root a b c
ftp d e f" >/tmp/test
sed 's#\([^ ]*\) \(.*\)$#echo $(id -u \1) \2#' /tmp/test | bash

This is all I could think of for now...
If I have some time later, I will see if there's something better.

Offline

#8 2008-02-11 15:16:42

drakosha
Member
Registered: 2006-01-03
Posts: 253
Website

Re: awk/smth_else wizard required :)

GREAT idea!!! Thanks! cool

Offline

#9 2008-02-11 15:42:48

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: awk/smth_else wizard required :)

That'll run into problems if you have bash-specific characters hanging around in your file (like $ or something).  This will work better, as it escapes the rest of your line in single-quotes, but still run into problems if your file actually has single-quotes in it.

$ sed "s#\([^ ]*\) \(.*\)\$#echo \$(id -u \1) '\2'#" /tmp/test | bash

Last edited by Cerebral (2008-02-11 15:43:42)

Offline

Board footer

Powered by FluxBB