how do I append new lines to awk variable?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how do I append new lines to awk variable?
# 1  
Old 11-02-2011
how do I append new lines to awk variable?

I want to build an array using awk, consisting only of a subset of lines of a file. I know how to have awk assess whether a key phrase is in a particular line, but I can't find anywhere how to then append the line containing that phrase to an array that has previously-found lines also containing that phrase (that I will eventually print out as a separate text file). I can't just print out the line itself each time I find one matching the phrase, because later lines indicate whether the previous lines were "good" or not. In need to save the lines, and only print them out if I at some point hit a line telling me they were good.

Hope this makes sense? Basically, I can see how to append awk variables to print, but how to append them to a variable/array?? I need each to be on a new line in the array, so they ultimately print out correctly into a file.

-Tom
# 2  
Old 11-02-2011
I am not sure I understood - posting an example of input and expected output is often helpful; when doing so use code tags please. Nevertheless:
Code:
$> cat infile
this bla one
this two
this three
this bla four
this bla five
$> awk '/bla/ {arr[c++]=$0} END{for(i=0; i<c; i++){print i,arr[i]}}' infile
0 this bla one
1 this bla four
2 this bla five

Filling an array with the lines matching /bla/ and using c as counter. At the END cycling through the filled array, using a new counter i, while printing index number and array elements.
# 3  
Old 11-02-2011
I have some awk code that, at one point, does this:

Code:
{
    if ($4 == "good_trial") {
        goodTrialCount++  # increment the goodTrial counter
	stuffImKeeping=$0
}

I'm assuming that

Quote:
stuffImKeeping=$0
is wrong, if I want to add the current line ($0) to stuffImKeeping. If it is wrong, how do I add $0 as a new line to the end of stuffImKeeping?
# 4  
Old 11-02-2011
Do you just want to append? I ask because you talk of an array and the code you wrote has no sign of an array. It incerements some variable, but that variable is not being used as counter when assigning values. An array is more than just a list of elements as you can address elements either by it's index or keys.

Regarding my former example just appending without using an array:
Code:
$> awk '/bla/ {l ? l=l FS $0 : l=$0} END{print l}' infile
this bla one this bla four this bla five

Change FS to RS if you want each row on it's own line.


For your code it might look like:
Code:
{
    if ($4 == "good_trial") {
        stuffImKeeping[goodTrialCount++]=$0
}

That's the short form - you could just use the variable and increment it (the index) in the next line:
Code:
{
    if ($4 == "good_trial") {
        stuffImKeeping[goodTrialCount]=$0
        goodTrialCount++
}


Last edited by zaxxon; 11-02-2011 at 04:33 AM.. Reason: added code and clarifications, typo
# 5  
Old 11-02-2011
Hmmm.. not sure what is best for my purposes. I think I just need to append.

Basically, I want to go through a log file looking for certain items in specific fields. In column 4, I'm looking for "good_trial". If I find it, I want to append that entire line into a temporary variable/array/whatever-is-best-for-awk that I will keep appending to, and ultimately print out to a file.

I can't print out the line immediately each time I come to one that has "good_trial" in it, because the log has 2 types of "good_trials": type "A" and type "B". I don't know if a set of "good_trials" is type A or B until after a series of "good_trials" have passed. So I need to save these lines in a temporary file until a later entry in the log tells me what to do with them (put them in type "A" category or type "B" category).

So I think that appending is all I need right now.

---------- Post updated at 03:28 AM ---------- Previous update was at 03:20 AM ----------

Did you mean:

Code:
{
    if ($4 == "good_trial") {
        stuffImKeeping[goodTrialCount]=$0
        stuffImKeeping++
}

or did you mean:

Quote:
{
if ($4 == "good_trial") {
stuffImKeeping[goodTrialCount]=$0
goodTrialCount++
}
# 6  
Old 11-02-2011
Got me, meant
Code:
{
    if ($4 == "good_trial") {
        stuffImKeeping[goodTrialCount]=$0
        goodTrialCount++
}

I think you do not need an array, as you don't use any of the advantages to address an element directly for example. So it sounds like
Code:
awk '/good_trial/ {l ? l=l RS $0 : l=$0} END{print l}' infile > newfile

might be suitable for you. An array might be just overhead/overpowered in this case.
# 7  
Old 11-02-2011
Thanks much for your help.

Can you unpack this part for me and explain what it is doing?

Code:
{l ? l=l RS $0 : l=$0}

my brain hurts trying to figure out what it does, and why it does it.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append variable value with sed

I have a requirement where I need to add a variable value based on a pattern match. When I try this it works: sed '/IS_ETL_DEV/a\ ABCD' File1.txt > File1.tmp Now I want to replace IS _ETL_DEV with a variable like $Pattern and replace the values ABCD with a variable $Var the value "$$XYZ=1".... (4 Replies)
Discussion started by: vskr72
4 Replies

2. UNIX for Dummies Questions & Answers

append following lines to 1st line, every 3 lines

I have output like this: USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 ... where USER_ID is a unique user login followed by their login timestamp and... (6 Replies)
Discussion started by: MaindotC
6 Replies

3. Shell Programming and Scripting

Append 0 to a variable

Hi, I have a variable... it can take a value from 1 to 99... now when it is 1 to 9 i want to save it as 01 to 09 in the same variable... can this be done using sed or awk??? a=1 i want it as a=01 Thanks in advance... (8 Replies)
Discussion started by: Nithz
8 Replies

4. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

5. Shell Programming and Scripting

Append lines in a file

Legends, Please help me out. I have a file abc.txt with the following entries JACK JIL SANDY amer europe pak Now, i want to append the character after each line. but, condition is if it is UPPER case then character appended should be uppercase, else lowercase example: JACKL2... (5 Replies)
Discussion started by: sdosanjh
5 Replies

6. Shell Programming and Scripting

Append lines in a file

Hi All, I have a file separated by , and each line end with ".If the line doesnt end with " then i need to join the current line and the next one and put them in another file a, b, c,d" d,f,g,h k, l m" o,p,q,r,t" ouput : a,b,c,d" d,f,g,h,k,l,m" o,p,q,r,t" (2 Replies)
Discussion started by: gwrm
2 Replies

7. Shell Programming and Scripting

Append zeros before a value as per variable

Hello- I have a variable which contains a number, I need to populate number of zeros before another value as per this variable value. for example: I have variable X whose content is 5, variable Y whose content is 123 Now append number of zeros as per variable X before varible 'Y'... (4 Replies)
Discussion started by: pasupuleti81
4 Replies

8. Shell Programming and Scripting

Append variable to only certain lines

Hi There! I'm trying to write a shell script which generates a random hexadecimal number and then appends it to the end of lines XX onwards of a certain file. XX is determined by a certain string on the previous line. Thus for example, if the input file is I search for the string... (3 Replies)
Discussion started by: orno
3 Replies

9. Shell Programming and Scripting

Append lines with SED

I have the following data in a file and would like to append the lines inside each section 10, 20, 30, 40, etc... onto one line for each of the sections. Is it possible with SED to make this happen?? 10 00039393 DOCK: RECEIVE PART, TAG. D D ... (3 Replies)
Discussion started by: miklacic
3 Replies

10. UNIX for Dummies Questions & Answers

append word to lines

Hi all, Can someone suggest how to append some word to all lines in file. for example word "Honey" to file f1 with lines: Mia Katrin Elizabeth to get Honey Mia Honey Katrin Honey Elizabeth Thanks in advance Givi (3 Replies)
Discussion started by: giviut
3 Replies
Login or Register to Ask a Question