user3177391
user3177391

Reputation:

Shell script to edit a large text file

I need to append the server name to the front of each user entry associated with that server. Below is an example of the file I'm curently dealing with:

<server_name>
at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash
bin:x:1:1:bin:/bin:/bin/bash
<server_name>
at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash
bin:x:1:1:bin:/bin:/bin/bash

I need a script to strip out the server name and append it to the front of the user entry. So the file would end up looking like this:

<server_name_a>
<server_name_a>:at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash
<server_name_a>:bin:x:1:1:bin:/bin:/bin/bash
<server_name_b>
<server_name_b>:at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash
<server_name_b>:bin:x:1:1:bin:/bin:/bin/bash

Upvotes: 0

Views: 84

Answers (2)

twalberg
twalberg

Reputation: 62379

Here's a simple awk program that should do what you're describing:

awk '/^INFO/{prefix=$NF;next}{printf "%s:%s\n", prefix, $0}' < ${FILENAME} > ${OUTPUT}

Upvotes: 2

Marian
Marian

Reputation: 7472

Try something like this:

#!/bin/bash
cat $* | while read line
do
    if echo $line | grep "^INFO:" > /dev/null
    then
        SERVER=`echo "$line" | sed -e 's/.*<\(.*\)>.*/\1/g'`
    else 
        echo $SERVER:$line
    fi
done

Upvotes: 1

Related Questions