Help required to Print Single quote into a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help required to Print Single quote into a file
# 1  
Old 08-05-2016
Help required to Print Single quote into a file

Hi,

I need help in printing string enclosed with single quotes to a file.

I am trying to write a shell script which when run will create another script below is the script logic.

Code:
cat create_script.sh
echo '#!/bin/sh' > append_flname.sh
echo 'for FILE in $*' >> append_flname.sh
echo 'do'	>> append_flname.sh
echo '/bin/awk ''{$0 = FILENAME "|" $0; print}'' $FILE' >> append_flname.sh
echo 'done' >> append_flname.sh

The problem is the script created (append_flname.sh) when create_script.sh is run does not have single quotes include for awk command.

The body has:
Code:
/bin/awk {$0 = FILENAME "|" $0; print} $FILE

but required is
The body has:
Code:
/bin/awk '{$0 = FILENAME "|" $0; print}' $FILE

I have tried many combination using \ or " but none of them succeeded. Please advise.

Thanks,
Imran.
# 2  
Old 08-05-2016
Try
Code:
echo "/bin/awk '{$0 = FILENAME \"|\" $0; print}' \$FILE"

or

Code:
echo '/bin/awk '\''{$0 = FILENAME "|" $0; print}'\'' $FILE'

This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-05-2016
Hello imrandec85,

Could you please try following and let me know if this helps.
Code:
cat script.ksh
awk -vs1="'" -vs2="\"" 'BEGIN{print "#!/bin/sh" ORS "for FILE in $*" ORS "do" ORS "awk  " s1 "{$0 = FILENAME " s2 "|" s2 "$0; print}" s1 " $FILE" ORS "done" >> "append_flname.sh"}' | sh
chmod 755 append_flname.sh
./append_flname.sh   Input_file1     Input_file2

EDIT: Adding a single awkscript/code which will do your task of creating the script and running it too as follows.
Code:
awk -vs1="'" -vs2="\""  -vs3="\\" 'BEGIN{print "awk -vs1=" s2 s1 s2 " -vs2=" s2 s3 s2 s2 OFS s1 "BEGIN{print " s2 "#!/bin/sh" s2 " ORS " s2 " for FILE in $*" s2 "ORS " s2 "do" s2 "ORS " s2 "awk  " s2 " s1 " s2 "{$0 = FILENAME " s2 "s2 " s2 "|" s2 " s2 " s2 "$0; print}"  s2 "s1 " s2 " $FILE" s2 " ORS " s2 "done" s2 ">> " s2 "append_flname.sh" s2 "}" s1 " | sh" ORS "chmod 755 append_flname.sh" ORS "./append_flname.sh  Input_file1  Input_file2"}' | sh

Also one liner command form of above awk command is as follows.
Code:
awk -vs1="'" -vs2="\""  -vs3="\\" 'BEGIN{
                                          print "awk -vs1=" s2 s1 s2 " -vs2=" \
                                          s2 s3 s2 s2 OFS s1 \
                                          "BEGIN{print " s2 "#!/bin/sh" s2 " ORS " s2 \
                                          " for FILE in $*"  \
                                          s2 "ORS " s2 "do" s2 "ORS " s2 "awk  " s2 " s1 " \
                                          s2 "{$0 = FILENAME " s2 "s2 " s2 "|" s2 " s2 " s2 "$0; print}"  \
                                          s2 "s1 " s2 " $FILE" s2 " ORS " s2 "done" s2 ">> " s2 "append_flname.sh"  \
                                          s2 "}" s1 " | sh" ORS "chmod 755 append_flname.sh" ORS "./append_flname.sh  Input_file1  Input_file2"
                                         }
                                   '  | sh

You need to provide Input_file names as above shown Input_file1 and Input_file2, like that you could give all Input_file names and could run the script. Please try this and let us know how it goes then.

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-05-2016 at 07:48 AM.. Reason: Added a more enhanced solution successfully too now.
# 4  
Old 08-05-2016
Try:-
Code:
echo '	/bin/awk '\''{$0 = FILENAME "|" $0; print}'\'' $FILE' >> /tmp/append_flname.sh

This User Gave Thanks to wisecracker For This Post:
# 5  
Old 08-05-2016
Thanks RudiC & wisecracker. Solution provided by both of you worked for me.

RavinderSingh13, I was facing some other issue using commands provided.
Problem: The cursor is moving to next line [>] expecting some other commands i guess, anyways thank you for the response.
# 6  
Old 08-05-2016
Quote:
Originally Posted by imrandec85
RavinderSingh13, I was facing some other issue using commands provided. Problem: The cursor is moving to next line [>] expecting some other commands i guess, anyways thank you for the response.
Hello imrandec85,

I have used following one-liner solution and it worked for me.
Code:
awk -vs1="'" -vs2="\""  -vs3="\\" 'BEGIN{
                                          print "awk -vs1=" s2 s1 s2 " -vs2=" \
                                          s2 s3 s2 s2 OFS s1 \
                                          "BEGIN{print " s2 "#!/bin/sh" s2 " ORS " s2 \
                                          " for FILE in $*"  \
                                          s2 "ORS " s2 "do" s2 "ORS " s2 "awk  " s2 " s1 " \
                                          s2 "{$0 = FILENAME " s2 "s2 " s2 "|" s2 " s2 " s2 "$0; print}"  \
                                          s2 "s1 " s2 " $FILE" s2 " ORS " s2 "done" s2 ">> " s2 "append_flname.sh"  \
                                          s2 "}" s1 " | sh" ORS "chmod 755 append_flname.sh" ORS "./append_flname.sh  Input_file1  Input_fil2"
                                         }
                                   '  | sh

EDIT: Checked non-one liner in my previous post and both solutions are working as expected as follows.
Code:
awk -vs1="'" -vs2="\""  -vs3="\\" 'BEGIN{print "awk -vs1=" s2 s1 s2 " -vs2=" s2 s3 s2 s2 OFS s1 "BEGIN{print " s2 "#!/bin/sh" s2 " ORS " s2 " for FILE in $*" s2 "ORS " s2 "do" s2 "ORS " s2 "awk  " s2 " s1 " s2 "{$0 = FILENAME " s2 "s2 " s2 "|" s2 " s2 " s2 "$0; print}"  s2 "s1 " s2 " $FILE" s2 " ORS " s2 "done" s2 ">> " s2 "append_flname.sh" s2 "}" s1 " | sh" ORS "chmod 755 append_flname.sh" ORS "./append_flname.sh Input_file1  Input_file2"}' | sh

Output came as follows.
Code:
Input_file1|                      03/08/2016 09:40-09:45            4       0.0
Input_file1|*********                        ------------ ---------
Input_file2|test|chumma_test
Input_file2|test|chumma_test11

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-05-2016 at 08:01 AM..
# 7  
Old 08-07-2016
For a literal transfer of text from a script to a file, I think the best method would be the quoted here-document:
Code:
cat << "EOF" >> append_flname.sh
#!/bin/sh
for FILE in $*
do
  /bin/awk '{$0 = FILENAME "|" $0; print}' "$FILE"
done
EOF

Which literally puts the content in the file, without worries of quotes, escapes, globbing or variable expansions..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Single quote _error_.

Hi all... (This is Don's domain.) I have come across an anomaly in sh and dash compared to bash. It involves echoing a character set to a file in sh and dash compared to bash. It is probably easier to show the code and results first. #!/usr/local/bin/dash #!/bin/sh #!/bin/bash echo... (4 Replies)
Discussion started by: wisecracker
4 Replies

2. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

3. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

4. Shell Programming and Scripting

How do you print a single quote character in AWK

How do you print out a single quote character in AWK? Using the escape character does not seem to work. {printf "%1$s %2$s%3$s%2$s\n" , "INCLUDE", " \' ", "THIS" } does not work. Any suggestions? (6 Replies)
Discussion started by: cold_Que
6 Replies

5. Shell Programming and Scripting

awk print: howto single quote not interpreted!?

cat a | awk -F";" '{print "update db set column=' "$2" ' where column1=\""$1"\";"}' > ip-add.sql Hi! I'm a new user! i need to use single quote in the double quotes print string The apex between che "$2" should not be interpreted, but....how?! I'm trying to use \ but don't work correctly! ... (4 Replies)
Discussion started by: Re DeL SiLeNziO
4 Replies

6. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

7. Shell Programming and Scripting

Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi, I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file: Name = "abc" The regex I'm using to match the same is: egrep -l '(^) *= *" ** *"$' /PATH_TO_SEARCH... (6 Replies)
Discussion started by: NanJ
6 Replies

8. Shell Programming and Scripting

want to print single quote using awk

i want to print ' symbol using awk i tried: awk '{print " ' "}' awk '{print "\' "}' both not work please help me. (2 Replies)
Discussion started by: RahulJoshi
2 Replies

9. UNIX for Dummies Questions & Answers

how to print single quote in awk

Hi all, It is a very stupid problem but I am not able to find a solution to it. I am using awk to get a column from a file and I want to get the output field in between single quotes. For example, Input.txt 123 abc 321 ddff 433 dfg ........ I want output file to be as ... (6 Replies)
Discussion started by: gauravgoel
6 Replies

10. Shell Programming and Scripting

single quote

Hi I have a shell script with many lines as below: comment on column dcases.proj_seq_num is dcases_1sq; .... .... I want the above script to be as below: comment on column dcases.proj_seq_num is 'dcases_1sq'; I want to have single quotes like that as above for the entire shell... (2 Replies)
Discussion started by: dreams5617
2 Replies
Login or Register to Ask a Question