Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

concatenate variable in a loop

Status
Not open for further replies.

alfie002

Technical User
Mar 3, 2004
121
GB
Dear all,

I have written an awk script to read through the fields in a line in a text file and then concatenate each field up to the point where the it detects a field called "Running"

Example of file is as follows;

Log Collector Running 05-12-20 15:43 25991

so in this case the first and second field would be used and the output Log Collector then written to a file for further processing.

I have included my script, which isn't complete but equally it doesn't work properly either.

I have attached the output from the script as follows;


Log Collector Running 05-12-20 15:43 25991 Log
Log Collector Running 05-12-20 15:43 25991 Log Collector
Log Collector Running 05-12-20 15:43 25991 Log Collector 05-12-20
Log Collector Running 05-12-20 15:43 25991 Log Collector 05-12-20 15:43
Log Collector Running 05-12-20 15:43 25991 Log Collector 05-12-20 15:43 25991

for some reason the script is printing out the complete line as well as the those fields that are identified as not the "Running" field.

Any help would be appreciated.

I don't think I am getting my variable assignment right.

Thanks

Alf
 
I have included my script
Really ? I don't see it ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
oops

try again

# Start: Main script
{
process_name = ""
max_record = NF
# print NF
#do
for (i = 1 ; i <= max_record ; i++ ) {
if ( $i != "Running" ) {
process = $i
$running_process = $running_process " " $i
#print $i
print $running_process
}
}

#while ( $i != "Running" )
}

thanks

alfie
 
Could you please post more complete input samples and expected result ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Something like this ?
{sub(/ Running .*/,"");print}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hello all,

thanks for the update.

more detailed input as follows;

Log Collector Running 05-12-20 15:43 25991
Context Server Running 05-12-20 15:43 26004
MNSD Agent Running 05-12-20 15:43 26044
Host Group Directory Running 05-12-20 15:43 26057
PP Comms Manager Running 05-12-20 15:43 26097
GMDR Running 05-12-20 15:43 26112


Yes, I want to be able to print out everything field up to but excluding the Running field. The output should then go to a file.

Hello, I tested the following;

/Running/ {
for (i=1;i<=NF;i++) if ($i=="Running") NF=i-1
print
}

the full line was printed.

Thanks

Alf
 
Here the result from your last posted input with my awk program:
Log Collector
Context Server
MNSD Agent
Host Group Directory
PP Comms Manager
GMDR

Is it the expected one ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
ok phv, and feherke. Thanks

will try what phv recommended.

Thanks

alf
 
phv,

how do you run that command

awk -f file_name -input_file

how is sub defined in the program file ?

Thanks

alf
 
I've tested it like this (in a unix shell):
awk '{sub(/ Running .*/,"");print}' /path/to/input

how is sub defined in the program file ?
sub and print are builtin functions in awk.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
thanks PHV,

just a couple of things. I am fine with the print command, is the sub command a subroutine definition ????

What would be the syntax if I wanted to put this command line structure into the program file and then execute with awk -f <file_name> <input_file> ??

Thanks

alf
 
Hello PHV,

I understand the awk command line syntax, however when I insert your code into a file and then excute with awk -f <prog_file> <input_file> I get the following error message

awk: syntax error near line 2
awk: illegal statement near line 2

This is the prog file.


{ sub
( / Running .*/,"") ; print
}

thanks

alfie

 
I already posted the program file in the post stamped 14 Feb 06 12:52.
An alternative, if you prefer:
{
sub(/ Running .*/,"")
print
}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
thanks feherke.

PHV, are you running nawk ??

Thanks

Alfie
 
If you have both awk and nawk I strongly suggest you use nawk.
 
hello phv,

i am running awk, will switch to nawk.

Thanks

Alf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top