Awk problem: How to express the backtick(')


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk problem: How to express the backtick(')
# 1  
Old 10-06-2009
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:
Code:
ls *.txt | awk '{print "grep \' \^\@\' ",$1}' > txt.sh

My desired output is when I type the command "more txt.sh "
The desired output is like this:
Code:
sample1.txt | grep '^@'
sample2.txt | grep '^@'
sample3.txt | grep '^@'
sample4.txt | grep '^@'

I got the problem when trying to show out the ' (backtick) at the txt.sh file.
I used the \' to show out the ' (backticks).
Is't because I use wrong?
Thanks a lot for your advice and suggestion.

Last edited by Franklin52; 10-06-2009 at 05:32 AM.. Reason: Please use code tags!
# 2  
Old 10-06-2009
Why so complicated?
Code:
grep '^@' *.txt

Or, if you really want to write a file:
Code:
for file in "*.txt"; do echo "grep '^@' $file" >> txt.sh; done

# 3  
Old 10-06-2009
Hi,

After I try this command that you suggested,
Code:
for file in "*.txt"; do echo "grep '^@' $file" >> txt.sh; done

It comes out something like :
Code:
grep '^@' *.out

But if I try
Code:
for file in *.txt; do echo "grep '^@' $file" >> txt.sh; done

It comes out something like:
Code:
grep '^@' )' sample1.txt
grep '^@' sample1.txt
grep '^@' )' sample2.txt
grep '^@' sample2.txt
 grep '^@' )' sample3.txt
grep '^@' sample3.txt
 grep '^@' )' sample4.txt
grep '^@' sample4.txt

Can I know why will got four extra file coming out?
Actually my desired output is look like this only:
Code:
grep '^@' sample1.txt
grep '^@' sample2.txt
grep '^@' sample3.txt
grep '^@' sample4.txt

Thanks again for your advance Smilie
Quote:
Originally Posted by pludi
Why so complicated?
Code:
grep '^@' *.txt

Or, if you really want to write a file:
Code:
for file in "*.txt"; do echo "grep '^@' $file" >> txt.sh; done


Last edited by pludi; 10-06-2009 at 09:47 AM.. Reason: code tags!!!
# 4  
Old 10-06-2009
if you want it the below format
Code:
grep '^@' sample1.txt
grep '^@' sample2.txt
grep '^@' sample3.txt
grep '^@' sample4.txt

This should do

Code:
ls *.txt|awk '{print "grep \47\^\@\47 " $0}'

# 5  
Old 10-06-2009
Can't see why it wouldn't work:
Code:
$ ls -1
sample1.txt
sample2.txt
sample3.txt
sample4.txt
$ for file in *.txt; do echo "grep '^@' $file" >> txt.sh; done
$ ls -1
sample1.txt
sample2.txt
sample3.txt
sample4.txt
txt.sh
$ cat txt.sh
grep '^@' sample1.txt
grep '^@' sample2.txt
grep '^@' sample3.txt
grep '^@' sample4.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Grep reg. express problem

When in the vi editor, I can find where the value of variables in COBOL programs (everything is uppercase) get changed with / *ACCT-BALANCE and it will find the last two characters of reserved words such as ACCEPT, FROM, TO, and COMPUTE followed by at least one space before the variable I'm... (2 Replies)
Discussion started by: wbport
2 Replies

2. Shell Programming and Scripting

[Solved] Backtick and escapes

Hello all, I have a problem with a bash script. It contains an MySQL query, which when I run it 'as is', executes without a problem. When, however, I try to get it to assign its output to a variable, using the backtick, I get errors. So .. /usr/bin/mysql -N -B mydatabase -e 'SELECT... (3 Replies)
Discussion started by: davidm123SED
3 Replies

3. Shell Programming and Scripting

use backtick inside awk

Hello, Can't we use backtick operator inside awk. nawk ' BEGIN { RS=""; FS="\</input\>" } { for(i=1;i<=NF;i++) { if ($i~/\"\"/) { print `grep XYZ $i`; print $i } } } ' test In the following code, I need to print $i and some... (8 Replies)
Discussion started by: shekhar2010us
8 Replies

4. Shell Programming and Scripting

Perl: Backtick Errors

When trying to use backticks for system commands, is there a way to read the error messages if a command doesn't execute properly? I have no problem getting the results if the command is properly executed. Ex. my @result = `dir`; foreach my $line (@result) { print "Result = $line";... (2 Replies)
Discussion started by: kooshi
2 Replies

5. Shell Programming and Scripting

Perl: combine Backtick & system() I/O operation?

Hi - Within perl I want to execute a system command. I want to re-direct all the output from the command to a file (@result = `$cmd`;), but I ALSO want the results to be displayed on the screen (system("$cmd"); The reason is this - if the command completes, I want to process the output. If the... (6 Replies)
Discussion started by: jeffw_00
6 Replies

6. Shell Programming and Scripting

Problem with awk awk: program limit exceeded: sprintf buffer size=1020

Hi I have many problems with a script. I have a script that formats a text file but always prints the same error when i try to execute it The code is that: { if (NF==17){ print $0 }else{ fields=NF; all=$0; while... (2 Replies)
Discussion started by: fate
2 Replies

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

8. Shell Programming and Scripting

By using awk, how to '(backtick)?

Can I know how to express the '(backtick) in awk?! By typing \' ??? (8 Replies)
Discussion started by: patrick87
8 Replies

9. UNIX for Advanced & Expert Users

Help with GNU screen: Backtick and Caption.

I'm trying to get GNU screen to show the output of "uptime" for the host being accessed in the current window, but unfortunately, no matter what window I go in, it shows the uptime for the host I originally launched screen in ("adminhost"). Does anyone know how to get this to update from the... (0 Replies)
Discussion started by: akbar
0 Replies
Login or Register to Ask a Question