Need help with awk to get values from input (newbie here)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with awk to get values from input (newbie here)
# 1  
Old 11-19-2011
Need help with awk to get values from input (newbie here)

Hello everyone!

I have a small text file called processes.txt which contains a few lines in this fashion:
Code:
ID: 35; Arrival_Time: 0; Total_Exec_Time: 4;
ID: 36; Arrival_Time: 1; Total_Exec_Time: 6;

I am trying to figure out how I can get the values from between the delimiters ';' and ':' per line.

So if I want to first get the ID, would I have to write something like this? After that I want to store the ID times as variables that I can use to figure out other things.
Code:
ids='cat processes.txt'
echo $ids | awk -f ';' '{print $1}'

Its obviously incorrect (it gives me errors), which is why I'm asking here if anyone would kindly help me figure out a way to obtain those values for ID, arrival times, and total exec times.

Thanks and regards!


Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples, thank you.

Last edited by Franklin52; 11-19-2011 at 10:46 AM..
# 2  
Old 11-19-2011
If you are looking for output like:

Code:
35 0 4
36 1 6

Then try this:
Code:
awk '{ for( i=2; i <= NF; i += 2 ) printf( "%d ", $(i) ); printf( "\n" );  }' data-file

Awk can read your input file, so no need to put it in a variable and echo the contents to awk.
This assumes that your values are always the second, fourth and sixth blank separated fields.
# 3  
Old 11-19-2011
Ah cool ;o

Can you please explain the following to me (im noob :-( /cry):

%d, and \n?

Last edited by mehungry; 11-19-2011 at 01:09 AM..
# 4  
Old 11-19-2011
Quote:
Originally Posted by mehungry

Can you please explain the following to me (im noob :-( /cry):

%d, and \n?
Gladly, we all started at some point.

The printf() function takes a format string as it's first parameter. This is used to determine the number of parameters, and their types, that follow. The manual page for the shell version does a wonderful job of explaining the details:
Man Page for printf (OpenSolaris Section 1) - The UNIX and Linux Forums

In brief, the percent sign followed by one or more characters, indicates the position that the next parameter is to be placed into the output. %d is integer, %s is string, %f is floating point decimal, etc. So, the format string "Today is %s the %d day of %s\n" would fill in the day of the week (first %s), the day of the month (%d) and the month (last %s) using the parameters. This assumes that the values are properly assigned to variables passed to the function. The following may help:
Code:
day=22;
month="May";
wday="Tue";
printf( "Today is %s the %d day of %s\n", wday, day, month );

This is a silly example, but I think it works to show how it goes.

The \n represents a newline character causing the next output written after this to be placed at the start of a new line. In the bit of code I wrote, I took advantage that without the new line, all three values are written on the same line, and then a final new line is written after the loop is done.

I also took avantage of a property of awk's printf with the integers. The fields were actually nn; and by using %d to print them, awk converted the string to integer and thus the trailing semicolon was removed.

The printf() function is wonderful and is supported in awk, shells and C/C++. Each function pretty much the same, but there are subtle differences like the automatic conversion of a string to integer by awk. Check out the manual page above, and the one for the C function to learn more.
This User Gave Thanks to agama For This Post:
# 5  
Old 11-19-2011
Awesome! That was perhaps the most well done piece of information relayed to me... EVER!

In my CS class we have to learn either bash or ksh on our own =/ I know Java and C++ but I'm finding this syntax really tough lol. >.<

Just glad there's ppl like you to help newbies like me Smilie

---------- Post updated at 12:44 AM ---------- Previous update was at 12:36 AM ----------

O just a question. Is the following supposed to return 30 0 4?
Code:
awk '{ for( i=2; i <= NF; i += 2 ) printf( "%d ", $(i) ); printf( "\n" );  }' processes.txt

Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples, thank you.

Last edited by Franklin52; 11-19-2011 at 10:47 AM..
# 6  
Old 11-19-2011
Quote:
Originally Posted by mehungry
Awesome! That was perhaps the most well done piece of information relayed to me... EVER!
Why, thanks.

Quote:
In my CS class we have to learn either bash or ksh on our own =/ I know Java and C++ but I'm finding this syntax really tough lol. >.<

Just glad there's ppl like you to help newbies like me Smilie

---------- Post updated at 12:44 AM ---------- Previous update was at 12:36 AM ----------

O just a question. Is the following supposed to return 30 0 4?
awk '{ for( i=2; i <= NF; i += 2 ) printf( "%d ", $(i) ); printf( "\n" ); }' processes.txt
For the input line
Code:
ID: 35; Arrival_Time: 0; Total_Exec_Time: 4;

it should output
35 0 4

In my testing with your two samples it did (the other was 36 1 6). I did just cut and pasted the code from your post and it seems to generate 35 and not 30. If 30 wasn't a typo, or 35 wasn't a typo in your original message, then something isn't right, but I'm not seeing what it could be.

---------- Post updated at 01:03 ---------- Previous update was at 01:00 ----------

Awk, a programming language in itself, can be confusing at first, but is wonderful. I think this is a pretty decent tutorial:

Awk - A Tutorial and Introduction - by Bruce Barnett
# 7  
Old 11-19-2011
O, I see!

Nice links Smilie

Hmmm when I put that code in and replace the last line with process.txt (which is the file I'm passing it), but the outputs I'm getting are 0 0 0 Smilie

---------- Post updated at 04:43 PM ---------- Previous update was at 01:36 AM ----------

hey im sorry to ask again, but when I type that code in, the outputs are 0 0 0 for each line.

am I doing something wrong? Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

3. Shell Programming and Scripting

Capturing multiple values from user input

Hello, I need to capture multiple values from user input and do not know how to do it. I'm writing a script to delete old files, but want to give the option to keep some by asking the user. This is what my output looks like... Old files to be deleted... 1 file1 2 file2 Then this bit of... (3 Replies)
Discussion started by: jrymer
3 Replies

4. Shell Programming and Scripting

Splitting record into multiple records by appending values from an input field (AWK)

Hello, For the input file, I am trying to split those records which have multiple values seperated by '|' in the last input field, into multiple records and each record corresponds to the common input fields + one of the value from the last field. I was trying with an example on this forum... (4 Replies)
Discussion started by: imtiaz99
4 Replies

5. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

6. Shell Programming and Scripting

Newbie help needed for comparing values

Being linux administrator my task is to keep the machines up and running .Everyday i get a report of list of machines that are up and running. it would be nice if i get list of machines that are shutdown in comparison to yesterdays report. for example.If there are 5 machines in my environment... (5 Replies)
Discussion started by: pinga123
5 Replies

7. Shell Programming and Scripting

Input new values into a column

Hi I would like to change certain columns in a file depending on the users choice of input and which columns they would like to change. Here is my csv file: SITEID,CELLID,Access Grant Blocks Reserved 1,1A,5 1,1B,2 1,1C,3 2,2A,7 2,2B,4 2,2C,0 If for example they would... (9 Replies)
Discussion started by: ladyAnne
9 Replies

8. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies

9. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

10. Shell Programming and Scripting

How to validate input values

Hi How would i validate value of a variable whether it is number,date or string Thanks in advance Sas (5 Replies)
Discussion started by: SasDutta
5 Replies
Login or Register to Ask a Question