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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk problem: How to express the single quote(') by using awk print function
# 1  
Old 10-06-2009
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
file4.txt

I want all the *.txt do the same command like:
awk '{print $1"\t"}'

My desired output is got a script named as txt.sh look like this:
awk '{print $1"\t"}' file1.txt
awk '{print $1"\t"}' file2.txt
awk '{print $1"\t"}' file3.txt
awk '{print $1"\t"}' file4.txt

The way I do now is :
ls *.txt | awk '{print "awk \"{$_\"\\t\"}\"",$1}'

And the output I get is like this:
awk "{print $1"\t"}" file1.txt
awk "{print $1"\t"}" file2.txt
awk "{print $1"\t"}" file3.txt
awk "{print $1"\t"}" file4.txt

If I type something like:
ls *.txt | awk '{print "awk \'{$_\"\\t\"}\'",$1}'
It is not worked at all.

Thus I'm asking whether you know the way to express the single quote by awk print to get my desired output resultImage
thanks again for all of your suggestion
# 2  
Old 10-06-2009
Why would you want to make and execute txt.sh when

Code:
awk '{print $1"\t"}' `ls *.txt`

will do the same thing without all the system overhead?
# 3  
Old 10-06-2009
Try this:

Code:
awk '{print "awk \047{print $1 \042\\t\042}\047 " $0}' file

My output:

Code:
$ cat file
file1.txt
file2.txt
file3.txt
file4.txt
$
$ awk '{print "awk \047{print $1 \042\\t\042}\047 " $0}' file
awk '{print $1 "\t"}' file1.txt
awk '{print $1 "\t"}' file2.txt
awk '{print $1 "\t"}' file3.txt
awk '{print $1 "\t"}' file4.txt
$

# 4  
Old 10-06-2009
Quote:
Originally Posted by jp2542a
Why would you want to make and execute txt.sh when

Code:
awk '{print $1"\t"}' `ls *.txt`

will do the same thing without all the system overhead?
That command will fail if any of the filenames contain embedded newlines or other "pathological" characters.

And the ls command and the sub-shell are unnecessary anyway.

Use shell globbing instead:

Code:
awk '{ print $1 "\t" }' *.txt

# 5  
Old 10-06-2009
You're absolutely on target, Radoulov. Keeping sharp by peer review is why I'm here. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: single quote match in If

Hello, I'd like to print line if column 5th doesn't match with exm. But to reach there I have to make sure I match single quote. I'm struggling to match that. I've input file like: Warning: Variants 'exm480340' and '5:137534453:G:C' have the same position. Warning: Variants 'exm480345'... (9 Replies)
Discussion started by: genome
9 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. Homework & Coursework Questions

Problem with using using quote in awk script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: i have the output of a csh script piped to an awk script. In the awk script i was trying to assign a variable... (3 Replies)
Discussion started by: ymc1g11
3 Replies

4. Shell Programming and Scripting

How to use awk in inserting single quote

Hi Guys, Please someone help me to insert these numbers (enclosed with single quotes) to a statement using awk command. I'm having hard time of putting single quotes on these numbers. input file: 10214 68441 07205 80731 92234 55432 DESIRED OUTPUT: My ID Number='10214';... (1 Reply)
Discussion started by: pinpe
1 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Awk problem: How to express the backtick(')

For example: I got a list of file end at .txt. I want all of them do the same command like grep '^@' and attached it to a output .sh file. This is the command I type: ls *.txt | awk '{print "grep \' \^\@\' ",$1}' > txt.sh My desired output is when I type the command "more txt.sh " The... (4 Replies)
Discussion started by: patrick87
4 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

AWK handling of single quote

Hi, Can someone let me know how I can acheive the following. I have ~ delimited file and I need to convert into something like SQL insert statements. SrcFile : 1~sjdsdj~asasas~ 2~aaaaa~qwqwqwq~qwq ..... I tried AWK -F"~" '{print "INSERT INTO XX VALUES("$1 " ,\' "$2" \' , \' "$3 }'... (3 Replies)
Discussion started by: braindrain
3 Replies
Login or Register to Ask a Question