How can a text file be automated converted like this using tools in linux? - linux

I have a text file with several lines.
I want to do the following:
1. Remove the first 14 characters. Leave the next 8 characters. Then delete everything on the line after that.
Then the file looks something like this.
20050013
AC040020
AC050024
At the beginning of each line, I want to add something like RAM[n] = 'h, where n keeps incrementing. I also want to add a ; at the end.
Then the file looks like this
RAM[0] = 'h20050013;
RAM[1] = 'hAC040020;
RAM[2] = 'hAC050024;
There has to be 10 entries in the file. So I add remaining entries and set them to 0. The file ends up like this:
RAM[0] = 'h20050013;
RAM[1] = 'hAC040020;
RAM[2] = 'hAC050024;
RAM[3] = 'h00000000;
RAM[4] = 'h00000000;
RAM[5] = 'h00000000;
RAM[6] = 'h00000000;
RAM[7] = 'h00000000;
RAM[8] = 'h00000000;
RAM[9] = 'h00000000;
I guess I could use a perl script or vi. How can this process be automated?

Have you read man sed?

Did you try doing it using awk ? awk has a lot of features but its a little tough to write as code the thing that you are exactly trying to do.
http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/
You can check this link for possible examples...

Use Awk.
{
printf("RAM[%d] = '%s;\n", NR - 1, $0)
}
(Put that in foo.awk, then run awk -f foo.awk on your data.)

This is very easy to do with AWK. Use substring and $NR

Related

How to use grep to find a specific string of numbers and move that to a new test file

I am new to using linux and grep and I am looking for some direction in how to use grep. I am trying to get two specific numbers from a text file. I will need to do this for thousands of files so I believe using grep or some equivalent to be best for my mental health.
The text file I am working with looks as follows:
*Average spectrum energy: 0.00100 MeV
Average sampled energy : 0.00100 MeV [ -0.0000%]
K/phi = <E*mu_tr/rho> = 6.529719E+02 10^-12 Gy cm^2 [ 0.0008%]
Kcol/phi = <E*mu_tr/rho>*(1-<g>) = 6.529719E+02 10^-12 Gy cm^2 [ 0.0008%]
<g> = 1.0000E-15 [ 0.4264%]
1-<g> = 1.000000 [ 0.0000%]
<mu_tr/rho> = <E*mu_tr/rho>/Eave = 4.075530E+03 cm^2/g [ 0.0008%]
<mu_en/rho> = <E*mu_tr/rho>*(1-<g>)/Eave = 4.075530E+03 cm^2/g [ 0.0008%]
<E*mu_en/rho> = 4.075530E+00 MeV cm^2/g
The values I am looking to extract from this are "0.00100" and "4.075530E+00".
At the moment I am using grep -iE "Average spectrum energy|<E*mu_en/rho>" * which is allowing me to see the full lines, but I am not quite sure how to refine the search to only show me the numbers instead of just the whole line. Is this possible using grep?
As for moving the numbers into a new file, I believe the command is > newdata.txt. My question is when using this with grep can you change how it writes the data to the new text file? I am looking for the format of the numbers to be like this:
0.00100001 3.4877754595352117
0.00100367 3.4665273232204363
0.00100735 3.4453747056004884
0.00101104 3.4243696230289187
0.00101474 3.4035147003587718
Again is that possble using the grep > newdata.txt?
I really appreciate any help or direction people can give me. Thank you.
I'm not quite sure why it was giving the 4.075530E+03 value.
That's because * has the special meaning of a repetition of the previous item any number of times (including zero), so the pattern <E*mu_en/rho> does not match the text <E*mu_en/rho>, but rather < any number of E mu_en/rho>, i. e. especially <mu_en/rho>. To escape this special meaning and match a literal *, prepend a backslash, i. e. <E\*mu_en/rho>.
I am not quite sure how to refine the search to only show me the numbers instead of just the whole line. Is this possible using grep?
It is if PCRE (grep -P) is available in the system. To only (-o) show the numbers, we can use the feature of Resetting the match start with \K. Your modified grep command is then:
grep -hioP "(Average spectrum energy: *|<E\*mu_en/rho> *= )\K\S*" *
(option -h drops the file names, pattern item \S means not a white space).
when using this with grep can you change how it writes the data to the new text file?
grep by itself cannot change the format of numbers (except maybe cutting digits off). If you want this, we need another tool. Now, since we need another tool, I'd consider using a tool which is capable of doing the whole job, e. g. awk:
awk '
/Average spectrum energy/ { printf "%.8f ", $4 }
/<E\*mu_en\/rho>/ { printf "%.16f\n", $3 }
' * >newdata.txt

Replacing a certain part of string with a pre-specified Value

I am fairly new to Puppet and Ruby. Most likely this question has been asked before but I am not able to find any relevant information.
In my puppet code I will have a string variable retrieved from the fact hostname.
$n="$facts['hostname'].ex-ample.com"
I am expecting to get the values like these
DEV-123456-02B.ex-ample.com,
SCC-123456-02A.ex-ample.com,
DEV-123456-03B.ex-ample.com,
SCC-999999-04A.ex-ample.com
I want to perform the following action. Change the string to lowercase and then replace the
-02, -03 or -04 to -01.
So my output would be like
dev-123456-01b.ex-ample.com,
scc-123456-01a.ex-ample.com,
dev-123456-01b.ex-ample.com,
scc-999999-01a.ex-ample.com
I figured I would need to use .downcase on $n to make everything lowercase. But I am not sure how to replace the digits. I was thinking of .gsub or split but not sure how. I would prefer to make this happen in a oneline code.
If you really want a one-liner, you could run this against each string:
str
.downcase
.split('-')
.map
.with_index { |substr, i| i == 2 ? substr.gsub(/0[0-9]/, '01') : substr }
.join('-')
Without knowing what format your input list is taking, I'm not sure how to advise on how to iterate through it, but maybe you have that covered already. Hope it helps.
Note that Puppet and Ruby are entirely different languages and the other answers are for Ruby and won't work in Puppet.
What you need is:
$h = downcase(regsubst($facts['hostname'], '..(.)$', '01\1'))
$n = "${h}.ex-ample.com"
notice($n)
Note:
The downcase and regsubst functions come from stdlib.
I do a regex search and replace using the regsubst function and replace ..(.)$ - 2 characters followed by another one that I capture at the end of the string and replace that with 01 and the captured string.
All of that is then downcased.
If the -01--04 part is always on the same string index you could use that to replace the content.
original = 'DEV-123456-02B.ex-ample.com'
# 11 -^
string = original.downcase # creates a new downcased string
string[11, 2] = '01' # replace from index 11, 2 characters
string #=> "dev-123456-01b.ex-ample.com"

How to extract word from a sentence in Linux Shell?

I have a shell file with the below SQL statements in it:
SELECT distinct vpi.pin_id_e
FROM MSSINT.V_DSLAMS vd,
MSSINT.v_pin_inventory_old vpi
where vd.dslam like '%#%'
and vd.dslam_id = vpi.dslam_id ;
select pa.circuit_design_id,pa.node_address,c.exchange_carrier_circuit_id,c.type,c.rate_code,c.status
from ASAP.port_address pa,
asap.circuit c
where pa.equipment_id = 4561233 and pa.circuit_design_id is not null
and pa.circuit_design_id = c.circuit_design_id;
In the above content of my shell file, I have to extract the table or view names alone (those between from and where keywords).
I have seen a lot of suggestions to get words based on position, but I don't want those since they will not work like between operators.
awk 'toupper($0) ~ /^FROM/ { getline;flag=1 } toupper($0) ~ /^WHERE/ { flag=0 }flag' filename
With awk, convert the string to upper case and then pattern match against FROM at the beginning of the line. If this exists, read in the next line and set flag to one. When WHERE is encountered at the beginning of the line, set the flag equal to 0. The complete line will then only print when flag is set to one i.e. between the from and where lines

How can I implement the java hashmap by using PHP?

I use PHP to process following input:
sam
99912222
tom
11122222
harry
12299933
sam
edward
harry
the 1st to 6th line are name and phone numbe. And the last three lines is the search query, if the name is not in the list(not have phone number,print not found), otherwise output the data. My code as follow:
<?php
$_fp = fopen("php://stdin", "r");
$list = array();
for($i = 0;$i<3;$i++){
$name = strtolower(fgets($_fp));
$phone = fgets($_fp);
$list["$name"] = $phone;
}
for($i = 0;$i<3;$i++){
$name = fgets($_fp);
if(array_key_exists($name,$list)){
echo "$name".'='."$list[$name]"."\n";
}else{
echo 'Not found'."\n";
}
?>
Excepted output should be sam = 99912222 Not found harry = 12299933
The output is sam = 99912222 Not found Not found. why these function doesn't work?
This is a problem from hackerrank.
I know if I use hashmap in java is easy to solve. But how can I solve this problem in PHP?
Many thanks
First, trim off whitespace by using trim(fgets($_fp)) everywhere instead of just fgets($_fp) -- that fixes things on my end at least.
Second, the code you pasted is missing the closing curly bracket on your second for loop.
Third, have fun with 30 Days of Code :-) (once you get the above straightened out you also need to have your code read in the number of entries at the beginning, and "Read the queries until end-of-file" at the end).

Split string in multiples lines swift

I'm trying to split a string in multiple lines (such as the text of a song); I'm working in the localizable.strings.
I'd like to do something like this:
"TITLE_SONG01" = "It's my life";
"SUBTITLE_SONG01" = "";
"TEXT_SONG01" = "It's my life" +
"is now or never";
but it doesn't work because data isn't in the correct format. Can anyone help me?
You can do that by inserting "\n" where you want the new line to start. For example:
print("Hello\nWorld")
Which will output the Hello on one line and World on another like this:
Hello
World

Resources