Execute a command using popen - c++

I have a C++ program in which I want to execute the following command:
cmd = "(diff <(perl -ne 's/^\\S+\\s//; if ((/aaa/ .. /bbb/) && /ccc/)"
" { print \"$_\"}' file1)"
"<(perl -ne 's/^\\S+\\s//; if ((/aaa/ .. /bbb/) && /ccc/)"
" { print \"$_\"} file2)) ";
I get this error when I want to execute this command:
Search pattern not terminated at -e line 1.
I've noticed that the following commands work like this:
cmd = "diff <(echo aa) <(echo bb)"
string strCall = "bash -c \"( " + cmd + " ) 2>&1\"";
stream = popen(strCall.c_str(),"r"); // doesn't work popen(**str**.c_str(),"r")
and an example perl command containing '"' works like this:
cmd = "perl -ne '{print \"$1\"}' file"
stream = popen(str.c_str(),"r"); // doesn't work popen(**strCall**.c_str(),"r");
but if the perl command doesn't contains '"', it works both ways:
cmd = "perl -ne '{print $1}' file"
string strCall = "bash -c \"( " + cmd + " ) 2>&1\"";
stream = popen(str.c_str(),"r"); // also works popen(**strCall**.c_str(),"r");
How can I do to use both diff and perl in the same command. I assume I have to use strCall.
I've tried also to escape the perl cmd like this, but it doesn't work:
cmd = "perl -ne '{print \\\"$1\\\"}' file" // one '/' for '/', one for "'" and one for '"'.
Also it didn't worked this, but I am however not allowed to use R("str"):
cmd = R"(perl -ne '{print \"$1\"}' file)"
string strCall = "bash -c \"( " + cmd + " ) 2>&1\"";
stream = popen(strCall.c_str(),"r")
Thanks.

I know I am not answering your question, but a common solution once you reach this many levels of quoting is to write a simple shell script and then call that from popen.
E.g., popen("/path/diffscript.sh", "r");

Related

how to extract a part of a line after delimiter using awk programming and print it to output?

I want to extract 911116683 and USCAFARES and also display the name of input file which I am processing using awk. the string is: '/split/911116683/1Y/USCAFARES' .
My awk program is :
awk -F" " 'BEGIN { OFS="|"; FILE_NAME="NULL"; WU_SG="NULL"; SRC_FTP_LOG_FILENAME="NULL"; } /Src file:/ { FILE_NAME1=$3; WU_SG1=$3; } END{ FILE_NAME=substr(FILE_NAME1,22,9); WU_SG=substr(FILE_NAME1,9,9); SRC_FTP_LOG_FILENAME=FILENAME; print(FILE_NAME,WU_SG,SRC_FTP_LOG_FILENAME); }' $File_to_be_processed
but I am getting syntax error for echo statement, I guess it can't be used. how to extract the fields here inside awk and display using variables?
my input file i.e File_to_be_processed contains this line:
Src file: '/split/911116683/1Y/USCAFARES' on host 'Local'
my output desired is:
USCAFARES|911116683|SYSOUT_0ranv_00007
where SYSOUT_0ranv_00007 is the name of the input file or File_to_be_processed
Works for me. All I did with your code was add whitespace, remove the needless variable instantiations, and lowercase the awk variable names to make it less shouty.
File_to_be_processed=SYSOUT_0ranv_00007
cat >$File_to_be_processed <<'END'
Src file: '/split/911116683/1Y/USCAFARES' on host 'Local'
END
awk -F" " '
BEGIN { OFS = "|" }
/Src file:/ { file_name1 = $3; wu_sg1 = $3; }
END {
file_name = substr(file_name1,22,9)
wu_sg = substr(file_name1,9,9)
src_ftp_log_filename = FILENAME
print(file_name,wu_sg,src_ftp_log_filename)
}
' $File_to_be_processed
I would use a different FS to make it easier to grab the bits you want:
awk -F "[/']" -v OFS="|" '/Src file:/ {print $6, $4, FILENAME; exit}' $File_to_be_processed

Tokenize and capture with sed

Suppose we have a string like
"dir1|file1|dir2|file2"
and would like to turn it into
"-f dir1/file1 -f dir2/file2"
Is there an elegant way to do this with sed or awk for a general case of n > 2?
My attempt was to try
echo "dir1|file1|dir2|file2" | sed 's/\(\([^|]\)|\)*/-f \2\/\4 -f \6\/\8/'
An awk solution:
awk -F'|' '{ for (i=1;i<=NF;i+=2) printf "-f %s/%s%s", $i, $(i+1), ((i==NF-1) ? "\n" : " ") }' \
<<<"dir1|file1|dir2|file2"
-F'|' splits the input into fields by |
for (i=1;i<=NF;i+=2) loops over the field indices in increments of 2
printf "-f %s/%s%s", $i, $(i+1), ((i==NF-1) ? "\n" : " ") prints pairs of consecutive fields joined with / and prefixed with -f<space>
((i==NF-1) ? "\n" : " ") terminates each field-pair either with a space, if more fields follow, or a \n to terminate the overall output.
In a comment, the OP suggests a shorter variation, which may be of interest if you don't need/want the output to be \n-terminated:
awk -F'|' '{ for (i=1;i<=NF;++i) printf "%s", (i%2 ? " -f " $i : "/" $i ) }' \
<<<"dir1|file1|dir2|file2"
This might work for you (GNU sed):
sed 's/\([^|]*\)|\([^|]*\)|\?/-f \1\/\2 /g;s/ $//' file
This will work for dir1|file1|dir2|file2|dirn|filen type strings
The regexp forms two back references (\1,\2 used in the replacement part of the substitution command s/pattern/replacement/), the first is all non-|'s, then a |, the second is all non-|'s then an optional | i.e. for the first application of the substitution (N.B. the g flag is implemented and so the substitutions may be multiple) dir1 becomes \1 and file1 becomes \2. All that remains is to prepend -f and replace the first | by / and the second | by a space. The last space is not needed at the end of the line and is removed in the second substitution command.
$ awk -v RS='|' 'NR%2{p=$0;next} {printf " -f %s/%s", p, $0}' <<< 'dir1|file1|dir2|file2'
-f dir1/file1 -f dir2/file2
A gnu-awk solution:
s="dir1|file1|dir2|file2"
awk 'BEGIN{ FPAT="[^|]+\\|[^|]+" } {
for (i=1; i<=NF; i++) {
sub(/\|/, "/", $i);
if (i>1)
printf " ";
printf "-f " $i
};
print ""
}' <<< "$s"
-f dir1/file1 -f dir2/file2
FPAT is used for grabbing dir1|file2 into single field.

Escaping special characters with sed

I have a script to generate char arrays from strings:
#!/bin/bash
while [ -n "$1" ]
do
echo -n "{" && echo -n "$1" | sed -r "s/((\\\\x[0-9a-fA-F]+)|(\\\\[0-7]{1,3})|(\\\\?.))/'\1',/g" && echo "0}"
shift
done
It works great as is:
$ wchar 'test\n' 'test\\n' 'test\123' 'test\1234' 'test\x12345'
{'t','e','s','t','\n',0}
{'t','e','s','t','\\','n',0}
{'t','e','s','t','\123',0}
{'t','e','s','t','\123','4',0}
{'t','e','s','t','\x12345',0}
But because sed considers each new line to be a brand new thing it doesn't handle actual newlines:
$ wchar 'test
> test'
{'t','e','s','t',
't','e','s','t',0}
How can I replace special characters (Tabs, newlines etc) with their escaped versions so that the output would be like so:
$ wchar 'test
> test'
{'t','e','s','t','\n','t','e','s','t',0}
Edit: Some ideas that almost work:
echo -n "{" && echo -n "$1" | sed -r ":a;N;;s/\\n/\\\\n/;$!ba;s/((\\\\x[0-9a-fA-F]+)|(\\\\[0-7]{1,3})|(\\\\?.))/'\1',/g" && echo "0}"
Produces:
$ wchar 'test\n\\n\1234\x1234abg
test
test'
{test\n\\n\1234\x1234abg\ntest\ntest0}
While removing the !:
echo -n "{" && echo -n "$1" | sed -r ":a;N;;s/\\n/\\\\n/;$ba;s/((\\\\x[0-9a-fA-F]+)|(\\\\[0-7]{1,3})|(\\\\?.))/'\1',/g" && echo "0}"
Produces:
$ wchar 'test\n\\n\1234\x1234abg
test
test'
{'t','e','s','t','\n','\\','n','\123','4','\x1234ab','g','\n','t','e','s','t',
test0}
This is close...
The first isn't performing the final replacement, and the second isn't correctly adding the last line
You can pre-filter before passing to sed. Perl will do:
$ set -- 'test1
> test2'
$ echo -n "$1" | perl -0777 -pe 's/\n/\\n/g'
test1\ntest2
This is a very convoluted solution, but might work for your needs. GNU awk 4.1
#!/usr/bin/awk -f
#include "join"
#include "ord"
BEGIN {
RS = "\\\\(n|x..)"
FS = ""
}
{
for (z=1; z<=NF; z++)
y[++x] = ord($z)<0x20 ? sprintf("\\x%02x",ord($z)) : $z
y[++x] = RT
}
END {
y[++x] = "\\0"
for (w in y)
y[w] = "'" y[w] "'"
printf "{%s}", join(y, 1, x, ",")
}
Result
$ cat file
a
b\nc\x0a
$ ./foo.awk file
{'a','\x0a','b','\n','c','\x0a','\0'}

filtering some text from line using sed linux

I have a following content in the file:
NAME=ALARMCARDSLOT137 TYPE=2 CLASS=116 SYSPORT=2629 STATE=U ALARM=M APPL=" " CRMPLINK=CHASSIS131 DYNDATA="GL:1,15 ADMN:1 OPER:2 USAG:2 STBY:0 AVAL:0 PROC:0 UKNN:0 INH:0 ALM:20063;1406718801,"
I just want to filter out NAME , SYSPORT and ALM field using sed
Try the below sed command to filter out NAME,SYSPORT,ALM fields ,
$ sed 's/.*\(NAME=[^ ]*\).*\(SYSPORT=[^ ]*\).*\(ALM:[^;]*\).*/\1 \2 \3/g' file
NAME=ALARMCARDSLOT137 SYSPORT=2629 ALM:20063
why not using grep?
grep -oE 'NAME=\S*|SYSPORT=\S*|ALM:[^;]*'
test with your text:
kent$ echo 'NAME=ALARMCARDSLOT137 TYPE=2 CLASS=116 SYSPORT=2629 STATE=U ALARM=M APPL=" " CRMPLINK=CHASSIS131 DYNDATA="GL:1,15 ADMN:1 OPER:2 USAG:2 STBY:0 AVAL:0 PROC:0 UKNN:0 INH:0 ALM:20063;1406718801,"'|grep -oE 'NAME=\S*|SYSPORT=\S*|ALM:[^;]*'
NAME=ALARMCARDSLOT137
SYSPORT=2629
ALM:20063
Here is another awk
awk -F" |;" -v RS=" " '/NAME|SYSPORT|ALM/ {print $1}'
NAME=ALARMCARDSLOT137
SYSPORT=2629
ALM:20063
Whenever there are name=value pairs in input files, I find it best to first create an array mapping the names to the values and then operating on the array using the names of the fields you care about. For example:
$ cat tst.awk
function bldN2Varrs( i, fldarr, fldnr, subarr, subnr, tmp ) {
for (i=2;i<=NF;i+=2) { gsub(/ /,RS,$i) }
split($0,fldarr,/[[:blank:]]+/)
for (fldnr in fldarr) {
split(fldarr[fldnr],tmp,/=/)
gsub(RS," ",tmp[2])
gsub(/^"|"$/,"",tmp[2])
name2value[tmp[1]] = tmp[2]
split(tmp[2],subarr,/ /)
for (subnr in subarr) {
split(subarr[subnr],tmp,/:/)
subName2value[tmp[1]] = tmp[2]
}
}
}
function prt( fld, subfld ) {
if (subfld) print fld "/" subfld "=" subName2value[subfld]
else print fld "=" name2value[fld]
}
BEGIN { FS=OFS="\"" }
{
bldN2Varrs()
prt("NAME")
prt("SYSPORT")
prt("DYNDATA","ALM")
}
.
$ awk -f tst.awk file
NAME=ALARMCARDSLOT137
SYSPORT=2629
DYNDATA/ALM=20063;1406718801,
and if 20063;1406718801, isn't the desired value for the ALM field and you just want some subsection of that, simply tweak the array construction function to suit whatever your criteria is.

Error executing a perl command using popen in C++

In my c++ program I want to execute a perl comand and read the output returned by the execution. I use popen for that, but I get an error when executing my command:
Command:
string cmd = "perl -ne 's/^\\S+\\s//; if ((/" +
pattern1+ " START/ .. /" + pattern2+ " END/) && /find/)"
" { print \"$_\"}' file";
stream = popen(cmd.c_str(),"r");
If I execute this command in the command line it works, but in C++ i get this error:
Search pattern not terminated at -e line 1.
The command that works in command line is, in C++ I already escaped the '\' and '"':
perl -ne 's/^\\S+\\s//; if ((/aaa START/ .. /bbb END/) && /find/) { print "$_"}' file
If I execute this command, it works: "perl -ne print $_ file".
But my initial command doesn't.
What I am doing wrong. Thanks.
It's your escape characters \. You'll have to double them up in the C++ string as \\ gets turned into \. Then the shell does it's processing as you see on the command line. i.e. another round of \\ turned into \.
You need to escape your backslashes (by adding more backslashes!).
std::string cmd = "perl -ne 's/^\\\\S+\\\\s//; if ((/" +
pattern1 + " START/ .. /" +
pattern2+ " END/) && /find/)"
" { print \"$_\"}' file";
In C++0x you can use raw R"(strings)" to avoid adding slashes. Compile with GCC like
g++ -std=c++0x -Wall popen.cpp
example:
std::string cmd_raw = R"(perl -ne 's/^\\S+\\s//; if ((/)" +
pattern1 + R"( START/ .. /)" +
pattern2 + R"( END/) && /find/))"
R"( { print \"$_\"}' file)";
This worked:
cmd = "perl -ne 's/^\\\\S+\\\\s//; if ((/" +
pattern1+ " START/ .. /" + pattern2+ " END/) && /find/)"
" { print \"$_\"}' file";
stream = popen(cmd.c_str(),"r");