Printing certain number of rows using awk. AND skipping certain rows - parsing

I have a simple question, which is almost too simple to find on this forum or on awk learning sites.
I have some awk code that matches a line beginning with a number, and prints the 6th column of that line:
/^[1-9]/ {
print $6
}
How do I tell it to print only the first 50 rows of the column from the match?
ADDITIONAL QUESTION
I tried used my own version of the answers below and I got it to print 50 lines. However, now I am trying to choose which 50 lines I print. I do this by skipping a line that starts with a number and contains the word 'residue'. Then I skip 5 lines that start with a number and contain a 'w'. This method is working as if I am only skipping the line with residue and prints from the first line starting with a number after that. Do you know why my 'w's are not being considered.
#!/usr/bin/awk -f
BEGIN {
line = 0;
skipW = 0;
}
# Ignore all lines beginning with a number until I find one I'm interested in.
/^[0-9]+ residue/ { next }
# Ignore the first five lines beginning with a number followed by a 'w'.
/^[0-9]+ w/ {
skipW += 1;
if (skipW <= 5) next
}
# For all other lines beginning with a number, perform the following. If we are
# "printing", increment the line count. When we've printed 50 lines turn off
# printing from that point on.
/^[0-9]+/ {
++line
if ((line > 0) && (line <= 50)) print $6
}

Use a match counter as part of your condition:
/^[1-9]/ && matched < 50 {
print $6
matched++
}
You can use a shortcut method also:
/^[1-9]/ { print $6; matched++ }
matched == 50 { exit }
But this may not always work on a pipline, if the producer command does not handle SIGPIPE gracefully.

awk '/^[1-9]/ { if (num_printed++ < 50) print $6 }'
This increments num_printed each time a match is found and prints out the first 50 such lines, regardless of where the lines are in the files in the input.
This reads through all the input. If an early exit is OK, then you can use:
awk '/^[1-9]/ { print $6; if (++num_printed == 50) exit }'
Note the switch from post-increment to pre-increment.

Related

AWK take some data input from file and set as variable in output

I have some data in file and need to print in output some format to the data in print.
Example content to parse:
012231-33339411.sxz.ree.fg*-*
U2FsdGVkX1+1pfXeR/h4u6P/BrItX75L0wHVIka4yA6tqS9a5CFUWvLu1AB4x2m8NpmJ>fyoXdADqlWDiGWi6Pw1a8NgNDbdTOlMtGBz4FCi8n97UdVQX9f0a2u9d5l7lOCxVDDzd>wJXbi9x4O+Dmo/lm9DbWAjBGKwWu0tTQxsU2TIpqv
FhUZmGd3E6vN+puPXz4yXeVQhMfQ+K8OpSM2ZuTpKCtDgm0SdUDyFnalA4lxHaFZqh+E>3+9JgHK7/KiiZmIJshUmqrwnkX0yKihCcOXCzaFITiByxBM/7PGeJo0IBAjyKI/GflgQ>8GsIWWRkCJnz2OMiYKr8uOMOAfTHnW57Dq+orDG1p
012236-33349111.sxz.ree.fg*-*
bCRIVArOSClIWrZz6KciBFT2iPjqsS/qMRSBYinBzpDmESj8kZHoGQ46BMq+LgHJiY5P>7yygNxCkEv25GKGViKTX1X6KSSLZ+RVNEts4N7jzVLoufZ+X/TAv2Ib7pnnEj7h4rWDn>y7KP1XrTynItaas5z5fpFt2zUHFNElvNmyrjbFZVp
DUsnWWDuvemWUr5YwOLxeRCnwTvfw71gwGEVeBzIJq4TsZb2/G8j9vpb/L7KNybsyQNN>DlOTMW5CHzd5otyYaNBcYo9V/4ky63q2vZMzQDWtCwVPaTKREPUqPLRKea3VkQnnsUic>/iBe+6Sv5GYl+XPGbIjWbTJWLQmc1kv8LXPyvUmTm
cUVypKp9fDlyFUkOkEVAxW8dMxHJ0c83BPw37GkCvsR9itkzO0FpX0Zn+OvRQRkUCyzr>dgijhcH
I need some way to take in Awk the first variable from begin to "-"
Example:
variable1=012231
and
variable1=012236
Variable 2 the 4 digits after the - character
Example:
Variable2=3333
and
variable2=3334
Variable 3 the 2 digits after the 4 digits of variable2
Example:
variable3=94
and
variable3=91
Variable 4 as the text before the newline
Example:
variable4=U2FsdGVkX1+1pfXeR/h4u6P/BrItX75L0wHVIka4yA6tqS9a5CFUWvLu1AB4x2m8NpmJ>fyoXdADqlWDiGWi6Pw1a8NgNDbdTOlMtGBz4FCi8n97UdVQX9f0a2u9d5l7lOCxVDDzd>wJXbi9x4O+Dmo/lm9DbWAjBGKwWu0tTQxsU2TIpqv
FhUZmGd3E6vN+puPXz4yXeVQhMfQ+K8OpSM2ZuTpKCtDgm0SdUDyFnalA4lxHaFZqh+E>3+9JgHK7/K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7pnnEj7h4rWDn>y7KP1XrTynItaas5z5fpFt2zUHFNElvNmyrjbFZVp
DUsnWWDuvemWUr5YwOLxeRCnwTvfw71gwGEVeBzIJq4TsZb2/G8j9vpb/L7KNybsyQNN>DlOTMW5CHzd5otyYaNBcYo9V/4ky63q2vZMzQDWtCwVPaTKREPUqPLRKea3VkQnnsUic>/iBe+6Sv5GYl+XPGbIjWbTJWLQmc1kv8LXPyvUmTm
cUVypKp9fDlyFUkOkEVAxW8dMxHJ0c83BPw37GkCvsR9itkzO0FpX0Zn+OvRQRkUCyzr>dgijhcH'
Haved tested the following code with result of print selecting by number of record and counting the fixed width of the field, without care the format or shape of the content.
awk -v FIELDWIDTHS="6 1 4 2 2 15" 'NR==1{print $1" "$3" "$4}NR==2{print}NR==3{print $1" "$3" "$4}NR==4{print}' file
But it`s a large file with variable lenght of number of records in the large string so the equal will not work for this case I will need catch this string to a variable to print it later in the output as field in all the sequences of show this field.
Could help me with some code to parse the input and print the output as close to the need, please explain how to take the positions in the input.
Thank in advance.
Using any awk in any shell on every Unix box:
$ cat tst.awk
split($0,f,"-") > 1 {
if ( NR > 1 ) {
prt()
delete var
}
var[1] = f[1]
var[2] = substr(f[2],1,4)
var[3] = substr(f[2],5,2)
next
}
{ var[4] = var[4] $0 }
END { prt() }
function prt( i) {
for ( i=1; i<=4; i++ ) {
printf "\047%s\047%s", var[i], (i<4 ? OFS : ORS)
}
}
$ awk -f tst.awk file
'012231' '3333' '94' 'U2FsdGVkX1+1pfXeR/h4u6P/BrItX75L0wHVIka4yA6tqS9a5CFUWvLu1AB4x2m8NpmJ>fyoXdADqlWDiGWi6Pw1a8NgNDbdTOlMtGBz4FCi8n97UdVQX9f0a2u9d5l7lOCxVDDzd>wJXbi9x4O+Dmo/lm9DbWAjBGKwWu0tTQxsU2TIpqvFhUZmGd3E6vN+puPXz4yXeVQhMfQ+K8OpSM2ZuTpKCtDgm0SdUDyFnalA4lxHaFZqh+E>3+9JgHK7/KiiZmIJshUmqrwnkX0yKihCcOXCzaFITiByxBM/7PGeJo0IBAjyKI/GflgQ>8GsIWWRkCJnz2OMiYKr8uOMOAfTHnW57Dq+orDG1p'
'012236' '3334' '91' 'bCRIVArOSClIWrZz6KciBFT2iPjqsS/qMRSBYinBzpDmESj8kZHoGQ46BMq+LgHJiY5P>7yygNxCkEv25GKGViKTX1X6KSSLZ+RVNEts4N7jzVLoufZ+X/TAv2Ib7pnnEj7h4rWDn>y7KP1XrTynItaas5z5fpFt2zUHFNElvNmyrjbFZVpDUsnWWDuvemWUr5YwOLxeRCnwTvfw71gwGEVeBzIJq4TsZb2/G8j9vpb/L7KNybsyQNN>DlOTMW5CHzd5otyYaNBcYo9V/4ky63q2vZMzQDWtCwVPaTKREPUqPLRKea3VkQnnsUic>/iBe+6Sv5GYl+XPGbIjWbTJWLQmc1kv8LXPyvUmTmcUVypKp9fDlyFUkOkEVAxW8dMxHJ0c83BPw37GkCvsR9itkzO0FpX0Zn+OvRQRkUCyzr>dgijhcH'

How do you make a “range?” In Lua

I’m pretty new to scripting or coding and I’m trying to figure out a way to have an input that can be between 1 and 100 without having to write 100 lines of code. I am doing this in Lua and I will include my current lines below.
elseif input.text == '>coins add 100' then coins = coins + 100; print ("coins effected. "..coins)
elseif input.text == '>coins add 200' then coins = coins + 200; print ("coins effected. "..coins)
elseif input.text == '>coins add 300' then coins = coins + 300; print ("coins effected. "..coins)
I’m using this for a game I’m developing because i thought it would be a good way to practice. This in specific is for a console to change different values for debugging. Tell me if this doesn’t make sense and i will try to fix it.
Thanks!
The function that limits a number in a certain range usually called Clamp.
function math.Clamp(val, min, max)
return math.min(math.max(val, min), max)
end
It's basically and literally a math.max with math.min together.
To avoid 100 if statements, I suggest you to make a script to wait for >coins add and then ask for a number specifically.
[Edit] Apparently it doesn't allow you to do so, so here is a walkaround:
local num = string.sub(input.text, 12) -- remove 12 first characters from the string, this will leave a number in it
num = tonumber(num) -- convert a string into Lua number
if not num then -- if the conversion failed, stop the script, print the text
print("Not a number")
return
end
num = math.Clamp(num, 1, 100) -- Limit the number from 1 to 100
coins = coins + num -- add that number
print ("coins effected. " .. coins) -- New amount of coins

How to ignore a case in awk?

Hi I am trying to make my awk script ignore all lines of the input file who have in rw=none in it but all other rw=* should still be matched.
I have tried with this code, but it does not work out because I do not ignore the whole line if the string is matching
My attempt:
match($0,/(rw=[^,]*)/){
!/rw=none/
n=split(substr($0,RSTART+3,RLENGTH-3),N,/:/)
for(i=1; i<=n; i++)print '$NETAPP_ID', vFiler, $1, N[i];
}
Example output:
/vol/lnxpeayh -sec=sys,rw=none
/vol/lnxplmulhall -sec=sys,rw=172.17.10.78:sfilerp01.os.net
wished Output is:
/vol/lnxplmulhall 172.17.10.78
/vol/lnxplmulhall sfilerp01.os.net
The code should ignore every line which has rw=none in it, just ignoring the string is not enough
/rw=none/ {next}
match($0, /rw=([^,]*)/, m) {
n = split(m[1], N, /:/)
for (i=1; i<=n; i++) print '$NETAPP_ID', vFiler, $1, N[i];
}
Notice how I changed the parentheses in the match regex? Now you don't need the substr function to extract the rw value.

insert numerical sequence in large text file

I need to create a file in this format :
item0000001
item0000002
item0000003
item0000004
item0000005
I was doing this with UltraEdit which has column mode including insert number ( start + increment including leading zeros ).
Unfortunately, UltraEdit bombs out above 1 million rows.
Does anyone know of a text editor with large file capacity that has a similar operation?
BaltoStar has not written which version of UltraEdit was used and how exactly he has tried to create the file.
However, here is an UltraEdit script which can be used to create a file with lines containing an incrementing number with leading zeros according to last number.
To use that script with UltraEdit v14.20 or UEStudio v9.00 or any later version, copy the code block of the script and paste it into a new ASCII file with DOS line terminations in UltraEdit/UEStudio. Save the file for example as CreateLinesWithIncrementingNumber.js into your preferred directory of UE/UES scripts.
Now run the script by clicking on menu item Run Active Script in menu Scripting.
The script prompts the user for first and last value of the incrementing number, and for strings left and right of the incrementing number which can be both also empty strings.
Then lean back and see how the script writes the lines with the incrementing number into a new file in blocks. I created a file with more than 150 MB with an incrementing number from 0 to 5.000.000 within a few seconds using this UltraEdit script.
if (typeof(UltraEdit.clipboardContent) == "string")
{
// Write in blocks of not more than 4 MB into the file. Do not increase
// this value too much as during the script execution much more free
// RAM in a continous block is necessary than the value used here for
// joining the lines in user clipboard 9. A too large value results
// in a memory exception during script execution and the user of the
// script also does not see for several seconds what is going on.
var nBlockSize = 4194304;
// Create a new file and make sure it uses DOS/Windows line terminations
// independent on the user configuration for line endings of new files.
UltraEdit.newFile();
UltraEdit.activeDocument.unixMacToDos();
var sLineTerm = "\r\n"; // Type of line termination is DOS/Windows.
// Ask user of script for the first value to write into the file.
do
{
var nFirstNumber = UltraEdit.getValue("Please enter first value of incrementing number:",1);
if (nFirstNumber < 0)
{
UltraEdit.messageBox("Sorry, but first value cannot be negative.");
}
}
while (nFirstNumber < 0);
// Ask user of script for the last value to write into the file.
do
{
var nLastNumber = UltraEdit.getValue("Please enter last value of incrementing number:",1);
if (nFirstNumber >= nLastNumber)
{
UltraEdit.messageBox("Sorry, but last value must be greater than "+nFirstNumber.toString(10)+".");
}
}
while (nFirstNumber >= nLastNumber);
var sBeforeNumber = UltraEdit.getString("Please enter string left of the incrementing number:",1);
var sAfterNumber = UltraEdit.getString("Please enter string right of the incrementing number:",1);
// http://stackoverflow.com/questions/16378849/ultraedit-how-do-i-pad-out-a-string-with-leading-blanks
// Convert the highest number to a decimal string and get a copy
// of this string with every character replaced by character '0'.
// With last number being 39428 the created string is "00000".
var sLeadingZeros = nLastNumber.toString(10).replace(/./g,"0");
// Instead of writing the string with the incrementing number line
// by line to file which would be very slow and which would create
// lots of undo records, the lines are collected first in an array of
// strings whereby the number of strings in the array is determined
// by value of variable nBlockSize. The lines in the array are
// concatenated into user clipboard 9 and written as block to the
// file using paste command. That is much faster and produces just
// a few undo records even on very large files.
UltraEdit.selectClipboard(9);
UltraEdit.clearClipboard();
// Calculate number of lines per block which depends on the
// lengths of the 4 strings which build a line in the file.
var nLineLength = sBeforeNumber.length + sLeadingZeros.length +
sAfterNumber.length + sLineTerm.length;
var nRemainder = nBlockSize % nLineLength;
var nLinesPerBlock = (nBlockSize - nRemainder) / nLineLength;
var asLines = [];
var nCurrentNumber = nFirstNumber;
while (nLastNumber >= nCurrentNumber)
{
// Convert integer number to decimal string.
var sNumber = nCurrentNumber.toString(10);
// Has the decimal string of the current number less
// characters than the decimal string of the last number?
if (sNumber.length < sLeadingZeros.length)
{
// Build decimal string new with X zeros from the alignment string
// and concatenate this leading zero string with the number string.
sNumber = sLeadingZeros.substr(0,sLeadingZeros.length-sNumber.length) + sNumber;
}
asLines.push(sBeforeNumber + sNumber + sAfterNumber);
if (asLines.length >= nLinesPerBlock)
{
asLines.push(""); // Results in a line termination at block end.
UltraEdit.clipboardContent = asLines.join(sLineTerm);
UltraEdit.activeDocument.paste();
UltraEdit.clearClipboard();
asLines = [];
}
nCurrentNumber++;
}
// Output also the last block.
if (asLines.length)
{
asLines.push("");
UltraEdit.clipboardContent = asLines.join(sLineTerm);
UltraEdit.activeDocument.paste();
UltraEdit.clearClipboard();
}
// Reselect the system clipboard and move caret to top of new file.
UltraEdit.selectClipboard(0);
UltraEdit.activeDocument.top();
}
else if(UltraEdit.messageBox)
{
UltraEdit.messageBox("Sorry, but you need a newer version of UltraEdit/UEStudio for this script.");
}
else
{
UltraEdit.newFile();
UltraEdit.activeDocument.write("Sorry, but you need a newer version of UltraEdit/UEStudio for this script.");
}

Lua Nested Unpack Bug?

Question:
I'm trying to unpack an array into an array, but it only works if it's the last item unpacked, if there is anything after it only the first element is unpacked. The following is a very basic example of what I'm trying to do. Is there a better way to do this, or is this a bug I'll have to cope with? I don't want to use table.insert as this seems to be much more readable adding within the definition of the table with something like unpack.
Code:
print ("Error 1")
local table1 = { {1,1}, {2,2}, {3,3} }
local table2 = { {0,0}, unpack (table1), {4,4} }
for n,item in ipairs (table2) do print (unpack(item)) end
print ("Good")
table1 = { {1,1}, {2,2}, {3,3} }
table2 = { {0,0}, unpack (table1) }
for n,item in ipairs (table2) do print (unpack(item)) end
print ("Error 2")
table1 = { {1,1}, {2,2}, {3,3} }
table2 = { {0,0}, unpack (table1), unpack (table1) }
for n,item in ipairs (table2) do print (unpack(item)) end
Output:
Error 1
0 0
1 1 -- {2,2} & {3,3} cut off.
4 4
Good
0 0
1 1 -- All elements unpacked.
2 2
3 3
Error 2
0 0
1 1 -- {2,2} & {3,3} cut off.
1 1 -- All elements unpacked.
2 2
3 3
Note:
I'm running version 5.1.
This is not a bug. A function call that returns multiple values is adjusted to the first value if the call is not the last one. The manual says that at http://www.lua.org/manual/5.1/manual.html#2.5

Resources