Reading a file line by line using bash, extracting some data. How? - parsing

I want to read a file a extract information from it based on certain tag. For example :
SCRIPT_NAME:mySimpleShell.sh
This is a simple shell. I would like to have this as
Description. I also want to create a txt file our of this.
SCRIPT_NAME:myComplexShell.sh
This is a complex shell. I would like to have this as
Description. I also want to create a txt file our of this.
So when I pass in this file to my shell script, my shell will read it line by line and
when it gets to SCRIPT_NAME, It extract it and save it in $FILE_NAME, then starts writing
the description to a file on disk with $FILE_NAME.txt name. And It does it until It reaches the end of file. If there is 3 SCRIPT_NAME tag, then it creates 3 description file.
Thanks for helping me in advance :)

Read the lines using a while loop. Use a regex to check if a line has SCRIPT_NAME and if so, extract the filename. This is shown below:
#! /bin/bash
while IFS= read -r line
do
if [[ $line =~ SCRIPT_NAME:(.*$) ]]
then
FILENAME="${BASH_REMATCH[1]}"
echo "Writing to $FILENAME.txt"
else
echo "$line" >> "$FILENAME.txt"
fi
done < inputFile

#!/bin/sh
awk '/^SCRIPT_NAME:/ { split( $0, a, ":" ); name=a[2]; next }
name { print > name ".txt" }' ${1?No input file specified}

Related

DIR but select specific parts of file names

I have the habit for years to use this very easy and convenient .BAT file to create a simple TXT file of all my files in a directory:
dir *.* > LIST.TXT
but the result includes many pieces of information that I don't need
Now I have many files in a directory in this way:
file 01 [shYddYJQIGfffY].xyz
second file 00008 [shYddYJQIfGf11dfzrffY].exe
filex [shGffY].sys
file that i don't need.txt
many other files that i don't need.bat
1/ which command line can I use with DIR (or anything else) to have in the final LIST.txt files only those information? only the value between [ and ]
Expected result:
shYddYJQIGfffY
shYddYJQIfGf11dfzrffY
shGffY
2/ how to edit the .BAT file to add the string MYRESULT before each of those results, like:
MYRESULT shYddYJQIGfffY
MYRESULT shYddYJQIfGf11dfzrffY
MYRESULT shGffY
Use a for loop (see for /?):
for /f "tokens=2 delims=[]" %%a in ('dir /b *[*].*') do echo MYRESULT %%a
This for /f loop takes the output of dir /b *[*].* (filtering for relevant files, bare format) line per line and splits it into three parts, first token the part before the first [, token2 the part between [ and ]. and a third part after that. You need the second token only. Then just echo the string together with the extracted substring, done.

zsh script to encode full file path

I want to be able to encode a path for use as a url i.e change spaces to %20. I found this function which does the encoding:
urlencode() {
setopt localoptions extendedglob
input=( ${(s::)1} )
print ${(j::)input/(#b)([^A-Za-z0-9_.\!~*\'\(\)- ])/%${(l:2::0:)$(([##16]#match))}}
}
and want to be able to pass the results of this:
print -l $PWD/* | tail -1
to the function.i.e get the last full path in the file list and encode it.
I thought that something like this:
print -l $PWD/* | tail -1 | urlencode
or
print -l $PWD/* | tail -1 > urlencode
would work but they don't.
Does anyone know how to accomplish it?
Many Thanks
You need to get your input from stdin rather than from the first argument.
Here is one way to adapt the function to do this
urlencode() {
setopt localoptions extendedglob
stdin=`while read line; do echo $line ;done`
input=( ${(s::)stdin} )
print ${(j::)input/(#b)([^A-Za-z0-9_.\!~*\'\(\)- ])/%${(l:2::0:)$(([##16]#match))}}
}
I tested it on my terminal, it works

How do I create a batch file that reads a file and writes a substring into another file?

I currently have an exported text file (output.txt) from a Clear-Case command that I need to parse. It looks like this:
Comparing the following:
R1.PROD.V1#\VOB_pvob
R2.PROD.V1#\VOB_pvob
Differences:
>> M:\ACME_PROD\src\ACME##
>> M:\ACME_PROD\src\ACME\file 2.txt##
>> M:\ACME_PROD\src\ACME\file 1.txt##
>> M:\ACME_PROD\src\ACME\file 3.txt##
What I would like to do is use the findstr command to filter the strings that are contained between the ">> " and "##" strings to get an output file that looks like this (with quotes if possible:
"M:\ACME_PROD\src\ACME"
"M:\ACME_PROD\src\ACME\file 2.txt"
"M:\ACME_PROD\src\ACME\file 1.txt"
"M:\ACME_PROD\src\ACME\file 3.txt"
I am new to writing batch files and so I don't exactly know where to start. I have managed to find code that can loop through the lines of a text file and separate code for the findstr command, but I get stuck trying to put it all together!
Best regards,
Andrew
Here you go
setlocal enabledelayedexpansion
for /f "skip=1 tokens=* delims=> " %%a in ('"findstr /r [\w^>*] output.txt"') do (
set line=%%a
set line=!line:#=!
echo "!line!" >>new.txt
)
The filtered strings will be outputted into new.txt.

get and save path to php.ini file

Is it possible to get the path to the php.ini file with a php script and save this path to a variable? I know I can call phpinfo() to find out the path, but it prints a lot of info and I only need the path to php.ini file and no output at all. Thank you in advance.
Sure, there are two functions related to what you would like to do. The first one is exactly what you're looking for, the second one shows the bigger picture that there can be more than one ini file:
php_ini_loaded_fileDocs - Retrieve a path to the loaded php.ini file.
php_ini_scanned_filesDocs - Return a list of .ini files parsed from the additional ini dir.
Next to that, mind the gap with .user.ini files, they don't show up in php_ini_scanned_files nor phpinfo.
You could exec("php -i | grep php.ini"), and grab that output.
Or you could use outputbuffering (ob_start()), run phpinfo(), get the contents of the outputbuffer (ob_get_contents()) and search trough that (preg_match) to find the php.ini file...
This works on the command line, might also work for the CGI:
ob_start(); phpinfo();
if ( preg_match( '#>(.+)/php.ini#', ob_get_clean(), $matches ) ) {
echo 'php.ini location: ' . trim( $matches[1] ) . '/php.ini';
}

Ruby script to read output from alias

I have setup an alias in /etc/aliases so that each time an email comes in to a specific address, the text of the email is sent to a Ruby script. Like so:
example: |/etc/smrsh/my_script.rb
I need to know how to read the piped data in my Ruby script..
I have written a simple Perl script that can read the data.. just can't figure out how to do it in Ruby.
Here is the relevant lines in the Perl script:
my $fout = "/tmp/email.out";
open( EM, ">$fout" );
while( <> ) {
chomp;
print EM "$_\n";
}
You can use STDIN to read your pided data. The equivalent of your Perl code would be something like:
out = File.open("/tmp/email.out", "a+")
STDIN.each do |line|
out.puts line
end

Resources