Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hello I'm trying to do something like this but it wont work, can you guys tell me help me out?

/usr/bin/mysql -B -r -h ******** -u******* -p****** -D***** \
    -e'SELECT `username`, `uid`, `gid`, `homedir` FROM `some_table`)' | \
        awk '{print $1":x:"$2":"$3"::"$4":/usr/sbin/nologin"}' >> /tmp/file1 ;
        awk '{print $1":"$5":13753:0:99999:7:::"}'>>/tmp/file2

Changing ";" to "&&" doesn't work either. Both file1 and file2 is created, but only file1 contains data. So basically what I want to do is using the same variables for two commands

share|improve this question
up vote 3 down vote accepted

Try this:

/usr/bin/mysql -B -r -h ******** -u******* -p****** -D***** \
    -e'SELECT `username`, `uid`, `gid`, `homedir` FROM `some_table`)' | \
        awk '{ print $1":x:"$2":"$3"::"$4":/usr/sbin/nologin" >> "/tmp/file1"; 
               print $1":"$5":13753:0:99999:7:::" >> "/tmp/file2" }'
share|improve this answer
Good call, get awk to do both operations in one call. I was thinking of the more generic case :) – MikeyB Oct 18 '11 at 16:14
Thank you, thats a nice solution! – Apaz Oct 19 '11 at 8:25

When you want to run two different commands on the same incoming pipe data, you need to use an intermediate file:

/usr/bin/mysql -B -r -h ******** -u******* -p****** -D***** \
               -e'SELECT `username`, `uid`, `gid`, `homedir` FROM `some_table`)' >"$TMPFILE"
awk '{print $1":x:"$2":"$3"::"$4":/usr/sbin/nologin"}' <"$TMPFILE" >>/tmp/file1
awk '{print $1":"$5":13753:0:99999:7:::"}' <"$TMPFILE" >>/tmp/file2
rm -f "$TMPFILE"

But as quanta writes, you can ask a single awk instance to do both operations.

share|improve this answer

You've made quite a mess, starting with the formatting of your question (see what I've done above which should make things below more clear).

First problem:
If you have a semicolon (;) or a "continue-on-success (&&) you are running two separate sets of commands.
Completely separate. It's like you ran one, hit enter and then ran the other.
This means your second awk is looking on stdin for input, and not finding anything it's giving you no output (which is why file2 is empty)

Second Problem:
Separate instances of awk can't share variables. Either you want the second awk to chew on the output of the MySQL query, or you want it to chew on the output of the first awk.
Since you've got 5 variables in your second awk I'm assuming the latter.

Run the first half of the command list (everything up to the semicolon).
Now run the second awk, and specify an input file (awk '{print $1":"$5":13753:0:99999:7:::"}' /tmp/file1 >> /tmp/file2).

share|improve this answer
Thank you! Sorry for the messy question, was in a bit of hurry when I wrote it, Ive think about it next time! – Apaz Oct 19 '11 at 8:26
No worries - That was more a "If you format your commands so they're pretty sometimes the answer jumps out" note than a "your question is ugly so I'm going to beat you with a dirty sock" one :-) – voretaq7 Oct 19 '11 at 15:24

Your command as shown will never work, as processing is entirely sequential, and the output of mysql is already used up by the first awk statement.

I would simply move all that to a tiny bash script:

mycmd="SELECT `username`, `uid`, `gid`, `homedir` FROM `some_table`"
while read -r username, uid, gid, homedir; do
printf "%s:x:%s:%s::%s:/usr/sbin/nologin" "$username" "$uid" "$gid" "$homedir" >> /tmp/file1
printf "%s:%s:13753:0:99999:7:::" "$username" "missing variable" >>/tmp/file2
done <(/usr/bin/mysql -B -r -h * -u* -p* -D* -e "$mycmd")

UNtested, visit the Wooledge Wiki for more bash magic than you knew existed :)

share|improve this answer
I'll keep that site in mind! – Apaz Oct 19 '11 at 8:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.