I want to use system command in Fortran 90 for executing folowing command:
command = awk '{print "C"NR,$1,$2,$3}' filename1 > filename2
call system(trim(command))
here my filename1 and filename2 are variables in a Fortran 90 program.
But the problem is that any character can be assigned to a variable which are enclosed between apostrophes and my variable should also be consisting of apostrophes. I don't know how to type it in Fortran 90.
Just use two apostrophes in a row inside the string
command = 'awk ''{print "C"NR,$1,$2,$3}'' filename1 > filename2'
Additionally, because I did not notice filename1 and filename2 are variables, you must append them as chw21 shows:
// trim(filename1)//' > '//trim(filename2)
You can try to use a parameter for single quotes, like this:
character, parameter :: sq = "'"
Then you can chain things together like this:
command = 'awk '//sq//'{print "C"NR,$1,$2,$3}'//sq//' '// &
trim(filename1)//' > '//trim(filename2)
Or, you can swap between single- and double quoted strings:
command = "awk '" // '{print "C"NR,$1,$2,$3}' // "' " // &
trim(filename1) // ' > ' // trim(filename2)
What you shouldn't do at all is using the Hollerith format instruction:
write(command, 100) trim(f1), trim(f2)
100 FORMAT(29Hawk '{print "C"NR,$1,$2,$3}' , A, " > ", A)
That's why I'm not even telling you. Oh.
Related
How to print a triangle coordinates (1,2) (3,4) (5,6) using puts? I am getting errors for the quotes.
puts "triangle just added is" "( " $ax "," $ay ") " "( " $bx "," $by ") " "( " $cx "," $cy ") "
puts takes one argument (A string to write to standard output) or two (A channel to write to and the string). Well, and the optional -nonewline option, so really 2 or 3 arguments. You're giving it a lot more than that, hence errors.
Like many scripting languages, tcl will expand variables inside double-quoted strings:
puts "triangle just added is ($ax,$ay) ($bx,$by) ($cx,$cy)"
The problem relates to a macro I'm trying to implement in Excel, specifically the shell function in the code. What the code does is that it executes chrome.exe opening a PDF file in a specific page of the document, the code is not mine it is from this post:
(Open a PDF from Excel with VBA in Google Chrome on a specific page)
and this is where I have the problem:
Dim arch As String
arch = "file:///C:\Users\user\Downloads\Trabajo.pdf#page=6"
If Not chromePath = "" Then
Shell ("""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" ""arch""")
End If
what i really need it to do is that the shell function opens chrome.exe and go to the path that is stored in the variable arch.
in the code that was posted it works this way:
Shell ("""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" ""file:///C:\Users\user\Downloads\Trabajo.pdf#page=6""")
I turned the PDF path into a variable since it will change on each PC I deliver the folder containing the excel document and the PDF
i have already managed how to get the PDF path to be read an put into the variable arch.
I feel a bit embarrassed because this might be a really silly question. Thank you very much in advance.
Surrounding your variable within quotes turns it into a string literal instead. You can tell just by looking at it within half a second that your variable won't work, because you aren't even concatenating the variable with the string literal (such as "MyString" & MyVar).
Also, you do not need to enclose your Shell argument within parenthesis since you are not using it to return a value, doing so is generally not good practice in VBA.
Anyways, there are a couple of methods I will show you here. First would be your style of surrounding with multiple double-quotes:
Shell """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" " & """" & arch & """"
Notice I had to combine the variable to the string with &.
And the easier-to-read using the Chr$() function:
Shell Chr$(34) & "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe " & _
Chr$(34) & arch & Chr$(34)
Chr$(34) is the character code for a double-quote character. This can make it easier to surround strings that contain actual double-quote characters within them.
Something to remember when using double-quotes:
A single " will open or close a string literal
Double "" will represent an 'Empty' string (x = "")
Now within a string literal, any quotes you want to be part of the string needs to be escaped with another double-quotation character.
Let's look closer at point # 3
MsgBox """This is a string"""
' ^^ ^ ^
' ||_ | |_ This closes (terminates) the string literal
' | | |
' | | |_This one is acting as an escape to the next "
' | |
' | |_This is escaping the next character
' |
' |_This is the start of the string literal
This can be validated because you would be able to change the above string by adding a space after the first " and before the last, such as:
MsgBox " ""This is a string"" "
While this wouldn't work without giving you a syntax error:
MsgBox "" "This is a string" ""
This is because the first opens the string the 2nd is the escape character. But it's escaping nothing but a space (same in reverse is true for the end).
Then you get to using 4 """", as with & """" & arch & """"
& """" & arch & """"
' ^^ ^
' || |_ Ends the string
' ||
' ||_ Escapes the next "
' |
' |_ Starts the string
Now you can visually see why it takes 4 " just to put a single " into a string literal by itself.
I want to convert the output from cppclean into cppcheck-like xml sections, such that:
./bit_limits.cpp:25: static data 'bit_limits::max_name_length'
becomes:
<error id="static data" msg="bit_limits::max_name_length">
<location file="./bit_limits.cpp" line="25"/>
</error>
I started with some awk:
test code:
echo "./bit_limits.cpp:25: static data 'bit_limits::max_name_length'" > test
cat test.out | awk -F ":" '{print "<error id=\""$3"\""}
{print "msg=\""}{for(i=4;i<=NF;++i)print ":"$i}{print "\">"}
{print "<location file=\""$1"\" line=\""$2"\"/>"}
{print "</error>"}'
Note: to run this you need to put the cat command back into one line - I printed it over multi-lines for ease of reading.
Explanation:
I am using awk and delimiting by colon ":" - which splits the line into useful chunks which I try to construct into the XML:
{print "<error id=\""$3"\""} - Extract the error ID part
{print "msg=\""}{for(i=4;i<=NF;++i)print ":"$i}{print "\">"} - extract the message (replacing the missing colons, this is all the remaining sections
{print "<location file=\""$1"\" line=\""$2"\"/>"} - extract the file and line, this part is easy since the colons line up nicely
{print "</error>"} - finally print the end tag
This is close, but not quite right, it produces:
<error id=" static data 'bit_limits"
msg="
:
:max_name_length'
">
<location file="./bit_limits.cpp" line="25"/>
</error>
The id field should just be "static data" and the msg field should be "'bit_limits::max_name_length'", but other then that it is ok (I don't mind it being split of multi-lines at the moment - though I would prefer that awk did not print a new line each time.
Update
As #charlesduffy pointed out - for context - I want to do this in bash because I want to embed this code into a makefile (or just a normal bash script) for maximum portability (i.e. no need for python or other tools).
With bash and a regex:
x="./bit_limits.cpp:25: static data 'bit_limits::max_name_length'"
[[ $x =~ (.+):([0-9]+):\ (.+)\ \'(.+)\' ]]
declare -p BASH_REMATCH
Output:
declare -ar BASH_REMATCH='([0]="./bit_limits.cpp:25: static data '\''bit_limits::max_name_length'\''" [1]="./bit_limits.cpp" [2]="25" [3]="static data" [4]="bit_limits::max_name_length")'
The elements 1 to 4 in array BASH_REMATCH contain the searched strings.
From man bash:
BASH_REMATCH: An array variable whose members are assigned by the =~ binary operator to the [[ conditional command. The element with index 0 is the portion of the string matching the entire regular expression. The element with index n is the portion of the string matching the nth parenthesized subexpression. This variable is read-only.
Probably more complex than it needs to be:
awk '{
split($1, file_line, ":")
field = 2
while(substr($field, 1, 1) != "'\''") {
id = id " " $field
++field
}
id = substr(id, 2)
while(field <= NF) {
msg = msg " " $field
++field
}
msg = substr(msg, 3, length(msg) - 1)
printf("<error id=\"%s\" msg=\"%s\">\n", id, msg)
printf(" <location file=\"%s\" line=\"%s\">\n", file_line[1], file_line[2])
print "</error>"
}' test.out
I have this text
" File: 'space folder' "
I want to replace this with only this
" space folder "
using sed or awk?
But when i try to do with it using sed it's not taking the command!
Does anyone have solution for this.
If I get your intent correctly, you need all text between single quotes; you can use this:
$ sed -r "s/^.*'([^']*)'.*$/\"\1\"/g" <<< "\" File: 'space folder' \""
"space folder"
$
Edit1: explanation
command <<< string => <<< indicates here string that is you pass a string to the command.
Our final string is this:
$ echo -e "\" File: 'space folder' \""
" File: 'space folder' "
$
since our string contains single quotes we use double quotes for sed command:
-r switch enables extended regular expression
"s/^.*'([^']*)'.*$/\"\1\"/g"
the above command substitutes the whole line with text present between single quotes.
Regular expression breakdown:
^ matches start of line
.* matches 0 or more characters
' matches a literal single quote
([^']*) matches 0 or more characters that are not single quote
and remembers it as a captured group with backreference number \1
' matches literal single quote
.* matches 0 or more chars
$ matches end of line
Like I have a text file with,
+
Code here
+
Code here +
Code+ here
+
Code here
And I want to grep this file and only show the line with a + character only? What regex should I construct?
Please advise
Many thanks
If I understand correctly, you want the line with only a +, no whitespace. To do that,
use ^ and $ to match the beginning and end of the line, respectively.
grep '^+$' filename
If you want a line with nothing but the + character, use:
grep '^+$' inputFile
This uses the start and end markers to ensure it only has that one character.
However, if you want lines with only a + character, possibly surrounded by spaces (as seems to be indicated by your title), you would use something like:
grep '^ *+ *$' inputFile
The sections are:
"^", start of line marker.
" *", zero or more spaces.
"+", your plus sign.
" *", zero or more spaces.
"$", end of line marker.
The following transcript shows this in action:
pax> echo ' +
Code here
+
Code here +
Code+ here
+
Code here' | grep '^ *+ *$' | sed -e 's/^/OUTPUT:>/' -e 's/$/</'
OUTPUT:> +<
OUTPUT:> +<
OUTPUT:> + <
The input data has been slightly modified, and a sed filter has been added, to show how it handles spacing on either side.
And if you want general white space rather than just spaces, you can change the space terms from the current " *" into "\s*" for example.