Operating System - HP-UX
1826043 Members
3471 Online
109690 Solutions
New Discussion

Re: shell help - How to include prefix information when copying data

 
SOLVED
Go to solution
Jack C. Mahaffey
Super Advisor

shell help - How to include prefix information when copying data

I'm sure someone can help with this.

I want a general purpose command that either prefixes or appends information when displaying or copying data.

For example:


ndname=`/usr/bin/uname -n`

/etc/passwd -F: -p $ndname

Output:

server1:root:*:...
server1:adm:*:...
...

where server1 is the host name and the remaining data is info returned from cat /etc/passwd.

server1 prefixes every record.

Thanks... Jack...
5 REPLIES 5
Tom Danzig
Honored Contributor

Re: shell help - How to include prefix information when copying data

How about something like:
PREFIX="$(hostname)_$(logname)_"

$PREFIX whould yieled "host1_joeblow_" which could be used in file naming i.e.:

OUTFILE="${PREFIX}file1" which would expand to host1_joeblow_file1
Stuart Abramson
Trusted Contributor

Re: shell help - How to include prefix information when copying data

ndname=`/usr/bin/uname -n`

cat /etc/passwd | while read LINE
do
.. echo $ndname ":" $LINE
done
Jack C. Mahaffey
Super Advisor

Re: shell help - How to include prefix information when copying data

I was hoping for a general purpose utility/script rather than looping through the file(s).


Reason for the script is to be able to consolidate sulogs, sudo logs, lastb output from mulitple servers into a single file so that one file can be examined rather than many.


Examples:

plogger is the script name that contains the capability.

# plogger -p "server1:sulog:" /var/adm/sulog

or

# cat /var/adm/sulog | plogger -p "server1:sulog:" > /tmp/sulog_with_prefix

c_51
Trusted Contributor
Solution

Re: shell help - How to include prefix information when copying data

here is a good start

#!/usr/bin/ksh

file=""
cmd=""
preText=""
preCmd=""
postText=""
postCmd=""

for i in $@
do
case "$i" in
"-preText")
shift;preText="$1"
;;
"-postText")
shift;postText="$1"
;;
"-preCmd")
#just take first line of command output
# i.e. don't want multi line command
###
# might want to do some checking on what the command is
# might not want to run just anything
###
shift;preCmd=$($1 | awk '{print $0;exit;}')
;;
"-postCmd")
shift;postCmd=$($1 | awk '{print $0;exit;}')
;;
"-f"|"-file")
shift;file="$1"
;;
"-c"|"-cmd")
shift;cmd="$1"
;;
*) print "your error message";exit 1;;
esac
done



# do some error checking
# -z $file && -z $cmd
# -n $file && -n $cmd
# etc
###

# you'll need to do want you want
# about weather preText comes before preCmd
# or what order
###
if [[ -n $file ]] ;then

cat $file |
while read line
do
print "${preText}${preCmd}${line}${postCmd}${postText}"
done
else
$cmd |
while read line
do
print "${preText}${preCmd}${line}${postCmd}${postText}"
done
fi
Muthukumar_5
Honored Contributor

Re: shell help - How to include prefix information when copying data

WE can do with good shell scripting more effectivly. I hope you got the solution, but there is a suggestion to use this script as,

#!/bin/sh
######################################################################
# Execution :
# logcon
#
# Requirement :
# Remote login enablement with r* commands
######################################################################

PATH=$PATH:/usr/bin/:/usr/sbin/:/bin/:/sbin/:./usr/local/bin/

if [[ $# -ne 0 ]]
then
echo " : logcon
exit 1
fi

host=$1
basename=$(basename $2)
filename=$2

rcp $host:$filename /tmp/$basename_$host.tmp

if [[ -f /tmp/$filename_$host ]]
then
while read line; do
echo "$host:$basename # $line"
done < /tmp/$filename_$host.tmp > /tmp/$basename_$host_$(date +"%d-%m-%y")
fi

# Remove temporary file
rm -f /tmp/$filename_$host.tmp

# Normal exit
exit 0

If you are interested in r* commands(remsh,rcp without passwd login into server).

HTH.
Easy to suggest when don't know about the problem!