f



add "" to only the line containing "()" just after "/" and at the end of line

How to write a program to add "" to only the line containing "()" just
after "/" and at the end of line in a text file?
Thanks for helping.
e.g.
input file is:
java -jar test.jar Testing/Object_Info.txt
java -jar test.jar Testing/3_Encryption(NoStored).txt
java -jar test.jar Testing/4_Decryption(NoStored).txt
java -jar test.jar Testing/ENC_Decrypt.txt
java -jar test.jar Testing/ENC_Encrypt.txt
java -jar test.jar Testing/Verify(Stored).txt

output file should be:
java -jar test.jar Testing/Object_Info.txt
java -jar test.jar Testing/"3_Encryption(NoStored).txt"
java -jar test.jar Testing/"4_Decryption(NoStored).txt"
java -jar test.jar Testing/ENC_Decrypt.txt
java -jar test.jar Testing/ENC_Encrypt.txt
java -jar test.jar Testing/"Verify(Stored).txt"
0
powah
12/11/2008 3:27:07 PM
comp.unix.shell 15484 articles. 2 followers. Post Follow

8 Replies
581 Views

Similar Articles

[PageSpeed] 44

powah wrote:
> How to write a program to add "" to only the line containing "()" just
> after "/" and at the end of line in a text file?
> Thanks for helping.
> e.g.
> input file is:
> java -jar test.jar Testing/Object_Info.txt
> java -jar test.jar Testing/3_Encryption(NoStored).txt
> java -jar test.jar Testing/4_Decryption(NoStored).txt
> java -jar test.jar Testing/ENC_Decrypt.txt
> java -jar test.jar Testing/ENC_Encrypt.txt
> java -jar test.jar Testing/Verify(Stored).txt
> 
> output file should be:
> java -jar test.jar Testing/Object_Info.txt
> java -jar test.jar Testing/"3_Encryption(NoStored).txt"
> java -jar test.jar Testing/"4_Decryption(NoStored).txt"
> java -jar test.jar Testing/ENC_Decrypt.txt
> java -jar test.jar Testing/ENC_Encrypt.txt
> java -jar test.jar Testing/"Verify(Stored).txt"

Try this:

sed 's|/\(.*(.*).*\)$|/"\1"|' file

Note that the above assumes that only one / appears in a line.
Otherwise it's probably easier to use awk:

awk -F'/' -v OFS='/' -v q='"' '$NF~/\(.*\)/{$NF=q $NF q}1' file

Also note that both solutions just check for the presence of ( and ) in that
order, and do not attempt to determine how many of them are there nor
whether they are balanced.

-- 
echo 0|sed 's909=mO#3u)o19;s0#0co*)].O0;s()(0bu}=(;s#}#m1$"?0^2{#;
s)")9v2@3%"9$);s[%[o]x(.$e#![;sz(z^+.z;su+ur!z"au;sxzxd?_{g)/x;:b;
s/\(\(.\).\)\(\(..\)*\)\(\(.\).\)\(\(..\)*#.*\6.*\2.*\)/\5\3\1\7/;
tb'|awk '{while((i+=2)<=length($1)-24)a=a substr($1,i,1);print a}'
0
Dave
12/11/2008 3:45:12 PM
At 2008-12-11 10:27AM, "powah" wrote:
>  How to write a program to add "" to only the line containing "()" just
>  after "/" and at the end of line in a text file?
>  Thanks for helping.
>  e.g.
>  input file is:
>  java -jar test.jar Testing/Object_Info.txt
>  java -jar test.jar Testing/3_Encryption(NoStored).txt
>  java -jar test.jar Testing/4_Decryption(NoStored).txt
>  java -jar test.jar Testing/ENC_Decrypt.txt
>  java -jar test.jar Testing/ENC_Encrypt.txt
>  java -jar test.jar Testing/Verify(Stored).txt
>  
>  output file should be:
>  java -jar test.jar Testing/Object_Info.txt
>  java -jar test.jar Testing/"3_Encryption(NoStored).txt"
>  java -jar test.jar Testing/"4_Decryption(NoStored).txt"
>  java -jar test.jar Testing/ENC_Decrypt.txt
>  java -jar test.jar Testing/ENC_Encrypt.txt
>  java -jar test.jar Testing/"Verify(Stored).txt"


    sed -e 's#/\([^/]\+([^/]\+)[^/]\+\)#/"\1"#' input_file > output_file

-- 
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous
0
Glenn
12/11/2008 3:45:23 PM
powah wrote:
> How to write a program to add "" to only the line containing "()" just
> after "/" and at the end of line in a text file?
> Thanks for helping.

You already got some solutions. If there are no spaces in your filename
I'd probably just take a uniform approach and put quotes around all the
arguments whether it contains brackets or not...

   awk '{ $NF = "\"" $NF "\"" } 1'
or
   awk -v Q=\" '{ $NF = Q $NF Q } 1'
or
   sed 's,/,/",;s,$,",'    # if always just one slash on the line

Otherwise substitute just on the lines containing brackets...

   sed '/[()]/s,/,/",;/[()]/s,$,",'


Janis

> e.g.
> input file is:
> java -jar test.jar Testing/Object_Info.txt
> java -jar test.jar Testing/3_Encryption(NoStored).txt
> java -jar test.jar Testing/4_Decryption(NoStored).txt
> java -jar test.jar Testing/ENC_Decrypt.txt
> java -jar test.jar Testing/ENC_Encrypt.txt
> java -jar test.jar Testing/Verify(Stored).txt
> 
> output file should be:
> java -jar test.jar Testing/Object_Info.txt
> java -jar test.jar Testing/"3_Encryption(NoStored).txt"
> java -jar test.jar Testing/"4_Decryption(NoStored).txt"
> java -jar test.jar Testing/ENC_Decrypt.txt
> java -jar test.jar Testing/ENC_Encrypt.txt
> java -jar test.jar Testing/"Verify(Stored).txt"
0
Janis
12/11/2008 4:39:28 PM
On Dec 11, 10:45=A0am, Dave B <da...@addr.invalid> wrote:
> powah wrote:
> > How to write a program to add "" to only the line containing "()" just
> > after "/" and at the end of line in a text file?
> > Thanks for helping.
> > e.g.
> > input file is:
> > java -jar test.jar Testing/Object_Info.txt
> > java -jar test.jar Testing/3_Encryption(NoStored).txt
> > java -jar test.jar Testing/4_Decryption(NoStored).txt
> > java -jar test.jar Testing/ENC_Decrypt.txt
> > java -jar test.jar Testing/ENC_Encrypt.txt
> > java -jar test.jar Testing/Verify(Stored).txt
>
> > output file should be:
> > java -jar test.jar Testing/Object_Info.txt
> > java -jar test.jar Testing/"3_Encryption(NoStored).txt"
> > java -jar test.jar Testing/"4_Decryption(NoStored).txt"
> > java -jar test.jar Testing/ENC_Decrypt.txt
> > java -jar test.jar Testing/ENC_Encrypt.txt
> > java -jar test.jar Testing/"Verify(Stored).txt"
>
> Try this:
>
> sed 's|/\(.*(.*).*\)$|/"\1"|' file
>
> Note that the above assumes that only one / appears in a line.
> Otherwise it's probably easier to use awk:
>
> awk -F'/' -v OFS=3D'/' -v q=3D'"' '$NF~/\(.*\)/{$NF=3Dq $NF q}1' file
>
> Also note that both solutions just check for the presence of ( and ) in t=
hat
> order, and do not attempt to determine how many of them are there nor
> whether they are balanced.
>
> --
> echo 0|sed 's909=3DmO#3u)o19;s0#0co*)].O0;s()(0bu}=3D(;s#}#m1$"?0^2{#;
> s)")9v2@3%"9$);s[%[o]x(.$e#![;sz(z^+.z;su+ur!z"au;sxzxd?_{g)/x;:b;
> s/\(\(.\).\)\(\(..\)*\)\(\(.\).\)\(\(..\)*#.*\6.*\2.*\)/\5\3\1\7/;
> tb'|awk '{while((i+=3D2)<=3Dlength($1)-24)a=3Da substr($1,i,1);print a}'

Both sed and awk commands work.  Thanks!
Would you mind explaining
 q=3D'"' '$NF~/\(.*\)/{$NF=3Dq $NF q}1'
in your command
awk -F'/' -v OFS=3D'/' -v q=3D'"' '$NF~/\(.*\)/{$NF=3Dq $NF q}1' file  ?

I understand that NF means "Number of fields in current record" and
you assign something to the variable q.
0
powah
12/11/2008 4:40:43 PM
powah wrote:
> [...]
> Would you mind explaining
>  q='"' '$NF~/\(.*\)/{$NF=q $NF q}1'

   -v q='"'    defines an awk variable named q qith value " (a quote)

   $NF         the value of the last field (the one with number NF)
   ~  /.../    match operator   and   patern to match
   q $NF q     concatenate the three pieces, quote + last field + quote

   1           abbreviation for  {print $0}  print the substituted line

> in your command
> awk -F'/' -v OFS='/' -v q='"' '$NF~/\(.*\)/{$NF=q $NF q}1' file  ?
> 
> I understand that NF means "Number of fields in current record" and
> you assign something to the variable q.

Janis
0
Janis
12/11/2008 5:00:43 PM
powah wrote:

>> Try this:
>>
>> sed 's|/\(.*(.*).*\)$|/"\1"|' file
>>
>> Note that the above assumes that only one / appears in a line.
>> Otherwise it's probably easier to use awk:
>>
>> awk -F'/' -v OFS='/' -v q='"' '$NF~/\(.*\)/{$NF=q $NF q}1' file
>>
>> Also note that both solutions just check for the presence of ( and ) in that
>> order, and do not attempt to determine how many of them are there nor
>> whether they are balanced.
>>
> Both sed and awk commands work.  Thanks!
> Would you mind explaining
>  q='"' '$NF~/\(.*\)/{$NF=q $NF q}1'
> in your command
> awk -F'/' -v OFS='/' -v q='"' '$NF~/\(.*\)/{$NF=q $NF q}1' file  ?
> 
> I understand that NF means "Number of fields in current record" and
> you assign something to the variable q.

What the program does is take the last field in the line (where fields are
separated by "/", as indicated by -F '/', and if it contains a ( and a ) in
that order, puts the field between double quotes.
The variable q is assigned the value '"' (a double quote), to avoid having
weird escapes inside the awk program (otherwise it would have been something
like eg awk '....{$NF=\" $NF \"}..'.
The "1" at the end prints all lines, regardless of whether a substitution
has been made. Since output fields are separated by a space by default, we
need to assign the same value "/" to the special variable OFS (output field
separator), so that the / is preserved in the output.

-- 
echo 0|sed 's909=mO#3u)o19;s0#0co*)].O0;s()(0bu}=(;s#}#m1$"?0^2{#;
s)")9v2@3%"9$);s[%[o]x(.$e#![;sz(z^+.z;su+ur!z"au;sxzxd?_{g)/x;:b;
s/\(\(.\).\)\(\(..\)*\)\(\(.\).\)\(\(..\)*#.*\6.*\2.*\)/\5\3\1\7/;
tb'|awk '{while((i+=2)<=length($1)-24)a=a substr($1,i,1);print a}'
0
Dave
12/11/2008 5:02:59 PM
Dave B wrote:
> powah wrote:
[...]
>> q='"' '$NF~/\(.*\)/{$NF=q $NF q}1'
>>in your command
>>awk -F'/' -v OFS='/' -v q='"' '$NF~/\(.*\)/{$NF=q $NF q}1' file  ?
[...]
> The variable q is assigned the value '"' (a double quote), to avoid having
> weird escapes inside the awk program (otherwise it would have been something
> like eg awk '....{$NF=\" $NF \"}..'.

....rather worse    {$NF="\"" $NF "\""}

Janis

> [...]
0
Janis
12/11/2008 5:03:48 PM
Janis Papanagnou wrote:
> Dave B wrote:
>> powah wrote:
> [...]
>>> q='"' '$NF~/\(.*\)/{$NF=q $NF q}1'
>>> in your command
>>> awk -F'/' -v OFS='/' -v q='"' '$NF~/\(.*\)/{$NF=q $NF q}1' file  ?
> [...]
>> The variable q is assigned the value '"' (a double quote), to avoid having
>> weird escapes inside the awk program (otherwise it would have been something
>> like eg awk '....{$NF=\" $NF \"}..'.
> 
> ...rather worse    {$NF="\"" $NF "\""}

Right, I wasn't thinking. Thanks for the correction, that would be even weirder.

-- 
echo 0|sed 's909=mO#3u)o19;s0#0co*)].O0;s()(0bu}=(;s#}#m1$"?0^2{#;
s)")9v2@3%"9$);s[%[o]x(.$e#![;sz(z^+.z;su+ur!z"au;sxzxd?_{g)/x;:b;
s/\(\(.\).\)\(\(..\)*\)\(\(.\).\)\(\(..\)*#.*\6.*\2.*\)/\5\3\1\7/;
tb'|awk '{while((i+=2)<=length($1)-24)a=a substr($1,i,1);print a}'
0
Dave
12/11/2008 5:11:55 PM
Reply:

Similar Artilces:

""""""""""""""""""""""ADD ME""""""""""""""""""""
Hi , Hope you are doing great. Please let me take this opportunity to introduce myself, Iam Karthik working with BhanInfo Inc, a NY based company. We have consultants on our bench on various technologies, my request is to add me to your distribution list and kindly do send me the requirements. i have the below list available 1. Mainframe 2. Java 3.. Financial Analyst 4. Data Architect If there is any vendor ship agreement which has to be signed then I would like to take an opportunity to represent my company and expect your cooperation... We look forward to build a ve...

"""""""""ADD ME""""""""""
Hi , Hope you are doing great. Please let me take this opportunity to introduce myself, Iam Karthik working with BhanInfoi Inc, a NY based company. We have consultants on our bench on various technologies, my request is to add me to your distribution list and kindly do send me the requirements. i have the below list available 1. Mainframe 2. Java 3.. Financial Analyst 4. Data Architect If there is any vendor ship agreement which has to be signed then I would like to take an opportunity to represent my company and expect your cooperation... ...

Urgent Requirement in """""""""""""NEW YORK""""""""""""""""
Hello Partners, Please find the requirement below. Please send the updated resume along with rate and contact no. REQ#1: Title : Java Developer ( Rating Project) Duration : 6 months Rate : open Location : NY strong java, WebLogic 9.2, Web Services, Oracle REQ#2: Title : Java Developer Duration : 4 months Rate : open Location : NY Strong java, SQL REQ#3: Title : VB.Net Consultant Location : NY Duration : 4 months Rate : open Primarily looking at someone who has Excel, VB.net a...

about "++" and "--"
why this program snippet display "8,7,7,8,-7,-8" the program is: main() { int i=8; printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,-i++,-i--); } > why this program snippet display "8,7,7,8,-7,-8" Ask your compiler-vendor because this result is IMHO implementation-defined. Check this out: http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15 http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.16 Regards, Irina Marudina fxc123@gmail.com wrote: > why this program snippet display "8,7,7,8,-7,-8&q...

"or" and "and"
Hi, I'm just getting to discover ruby, but I find it very nice programming language. I just still don't understand how the "or" and "and" in ruby... I was playing with ruby and for example made a def to print Stem and Leaf plot (for those who didn't have a statistics course or slept on it, e.g. http://cnx.org/content/m10157/latest/) Here is the Beta version of it: class Array def n ; self.size ; end def stem_and_leaf(st = 1) # if st != (2 or 5 or 10) then ; st = 1 ; end k = Hash.new(0) self.each {|x| k[x.to_f] += 1 } k = k.sort{|a, b| a[0].to_f <=&g...

why "::", not "."
Why does the method of modules use a dot, and the constants a double colon? e.g. Math::PI and Math.cos -- Posted via http://www.ruby-forum.com/. On Oct 26, 2010, at 01:48 , Oleg Igor wrote: > Why does the method of modules use a dot, and the constants a double > colon? > e.g. > Math::PI and Math.cos For the same reason why inner-classes/modules use double colon, because = they're constants and that's how you look up via constant namespace. Math::PI and ActiveRecord::Base are the same type of lookup... it is = just that Base is a module and PI is a float....

"If then; if then;" and "If then; if;"
I have a raw data set which is a hierarchical file: H 321 s. main st P Mary E 21 F P william m 23 M P Susan K 3 F H 324 S. Main St I use the folowing code to read the data to creat one observation per detail(P) record including hearder record(H): data test; infile 'C:\Documents and Settings\retain.txt'; retain Address; input type $1. @; if type='H' then input @3 Address $12.; if type='P' then input @3 Name $10. @13 Age 3. @16 Gender $1.; run; but the output is not what I want: 1 321 s. main H 2 321 s. main P Mary E 21 F 3 321 s...

"out" and "in out"
Hi i found the following explaination: In Ada, "in" parameters are similar to C++ const parameters. They are effectively read-only within the scope of the called subprogram. Ada "in out" parameters have a reliable initial value (that passed in from the calling subprogram) and may be modified within the scope of the called procedure. Ada "out" parameters have no reliable initial value, but are expected to be assigned a value within the called procedure. What does "have no reliable initial value" mean when considering the "out" parameter? By c...

"/a" is not "/a" ?
Hi everybody, while testing a module today I stumbled on something that I can work around but I don't quite understand. >>> a = "a" >>> b = "a" >>> a == b True >>> a is b True >>> c = "/a" >>> d = "/a" >>> c == d True # all good so far >>> c is d False # eeeeek! Why c and d point to two different objects with an identical string content rather than the same object? Manu Emanuele D'Arrigo wrote: >>>> c = "/a" >>>&...

"my" and "our"
Hi, while testing a program, I erroneously declared the same variable twice within a block, the first time with "my", the second time with "our": { my $fz = 'VTX_Link'; .... ( around 200 lines of code, all in the same block) our $fz = 'VTX_Linkset'; ... } So the initial contents of the $fz declared with "my" is lost, because "our" creates a lexical alias for the global $fz, thus overwriting the previous "my" declaration. It was my error, no question. But I wonder why Perl doesn't mention this - even with "use s...

Urgent Requirement for """""""""""""""INFORMATICA DEVELOPER"""""""""""""
Hello Partners, How are you ? Please find the requirements below. Title: Database/ETL Developer Duration: 6 months Location: NY Exp: 7+ Locals preferred Database/ETL requirements (Mandatory) Candidate must have worked with financial instruments, preferably Mutual Funds but, Equities are also ok. PL/SQL - packages, Stored procs, Functions, Aggregate functions, Pipelined Functions Informatica 8.6 - especially complex mappings, complex maplets, complex workflows, transformations Oracle 10g/11g Unix/Linux shell scripting ...

Urgent need """""""""""INFORMATICA DEVELOPER"""""""""""""
Hello Partners, How are you ? Please find the requirements below. Title: Database/ETL Developer Duration: 6 months Location: NY Exp: 7+ Locals preferred Database/ETL requirements (Mandatory) Candidate must have worked with financial instruments, preferably Mutual Funds but, Equities are also ok. PL/SQL - packages, Stored procs, Functions, Aggregate functions, Pipelined Functions Informatica 8.6 - especially complex mappings, complex maplets, complex workflows, transformations Oracle 10g/11g Unix/Linux shell scripting Database/ETL requirements (Optional) ...

A problem about "[ ]" "( )" "="
I want to read several images saved in a director,and give them to I1,I2 ,I3....,using the following codes: filelist=dir(['c:\MATLAB701\work\...\*.jpg']); for i=1 :length(filelist) I=imread(fullfile('c:\MATLAB701\work\...',filelist(i).name)); end; but failed. Then I used I(i)=imread... ,still failed. How could I do? "John" <mailofww@126.com> wrote in message news:ef19e12.-1@webx.raydaftYaTP... >I want to read several images saved in a director,and give them to > I1,I2 ,I3....,using the following codes: > filelist=dir(['c:\MATLAB701\work\.....

"In" "Out" and "Trash"
I just bought a new computer and I re-installed Eudora Light on my new computer. But when I open Eudora, the "In", "Out" and "Trash" links are not on the left side of the screen the way they were on my old computer. How can I get these links back on the left side of the screen? Thank you. On 25 Mar 2007 09:49:22 -0700, "abx" <abfunex@yahoo.com> wrote: >I just bought a new computer and I re-installed Eudora Light on my new >computer. But when I open Eudora, the "In", "Out" and "Trash" links >are ...

Web resources about - add "" to only the line containing "()" just after "/" and at the end of line - comp.unix.shell

Resources last updated: 2/12/2016 5:08:30 PM