Visit Our UNIX and Linux User Community


Print rows, having pattern in specific column...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print rows, having pattern in specific column...
# 22  
Old 10-06-2009
Quote:
Originally Posted by Scrutinizer
[..] But even then it causes unnecessary processing.
Yes,
it executes the second action for every record in the first non empty input file also:



Code:
zsh-4.3.10[t]% print -l {1..1000}>pattern
zsh-4.3.10[t]% print -l A\ {1..1000}>input  
zsh-4.3.10[t]% head -3 pattern input 
==> pattern <==
1
2
3

==> input <==
A 1
A 2
A 3
zsh-4.3.10[t]% wc -l input pattern 
1000 input
1000 pattern
2000 total
zsh-4.3.10[t]% pgawk>/dev/null 'NR==FNR{_[$1]}$2 in _' pattern input 
zsh-4.3.10[t]% cat awkprof.out 
    # gawk profile, created Tue Oct  6 09:18:58 2009

    # Rule(s)

  2000  NR == FNR    { # 1000
  1000      _[$1]
    }

  2000  $2 in _    { # 1000
  1000      print $0
    }
zsh-4.3.10[t]% pgawk>/dev/null 'NR==FNR{_[$1];next}$2 in _' pattern input
zsh-4.3.10[t]% cat awkprof.out 
    # gawk profile, created Tue Oct  6 09:22:42 2009

    # Rule(s)

  2000  NR == FNR    { # 1000
  1000      _[$1]
  1000      next
    }

  1000  $2 in _    { # 1000
  1000      print $0
    }

# 23  
Old 10-06-2009
Thanks ya, radoulov. I fully understand about it now Smilie
In between, can I know how to print out the backstick(') by using awk?
Actually I want to express the backticks in command and attach it to a file.

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 desired output is like this:
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.
# 24  
Old 10-06-2009
Hi Patrick.

There's a number of ways to display single quotes in AWK (these aren't backticks as you ask).

One is:
bash code:
  1. echo blah | awk '{print SQ $1 SQ}' SQ="'"
  2. 'blah'

Another is
bash code:
  1. echo blah | awk '{print "'"'"'" $1 "'"'"'" }'
  2. 'blah'
Yuk!
# 25  
Old 10-06-2009
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 Smilie
thanks again for your suggestion
# 26  
Old 10-06-2009
Quote:
Actually I got a list of file end with *.txt
I want to use the same command apply to all the *.txt
Why don't you just apply that command directly?

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

You don't need awk for this (assuming the filenames do not contain embedded new lines):

Code:
(
  IFS='
'
printf "awk '{print \$1\"\\\t\"}' %s\n" $(<infile)
  )

And, as I already said, you can achieve the same with:

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

... or use xargs if you have too many files.

Quote:
Thus I'm asking whether you know the way to express the single quote by awk print
thanks again for your suggestion
Code:
awk -vq=\' 'BEGIN {
  for (f=1; f<ARGC; f++)
    print "awk", q "{ print $1 \"\\t\" }" q, ARGV[f]
    }' *txt

For example:

Code:
% ls
file1.txt  file2.txt  file3.txt  file4.txt
% awk -vq=\' 'BEGIN {
  for (f=1; f<ARGC; f++)
    print "awk", q "{ print $1 \"\\t\" }" q, ARGV[f]
}' *txt
awk '{ print $1 "\t" }' file1.txt
awk '{ print $1 "\t" }' file2.txt
awk '{ print $1 "\t" }' file3.txt
awk '{ print $1 "\t" }' file4.txt


Previous Thread | Next Thread
Test Your Knowledge in Computers #881
Difficulty: Medium
One many threat vectors to a modern SCADA system is the threat of unauthorized access to the control software,
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

2. Shell Programming and Scripting

How to print multiple specific column after a specific word?

Hello.... Pls help me (and sorry my english) :) So I have a file (test.txt) with 1 long line.... for example: isgc jsfh udgf osff 8462 error iwzr 653 idchisfb isfbisfb sihfjfeb isfhsi gcz eifh How to print after the "error" word the 2nd 4th 5th and 7th word?? output well be: 653 isfbisfb... (2 Replies)
Discussion started by: marvinandco
2 Replies

3. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

4. UNIX for Dummies Questions & Answers

Deleting rows where the value in a specific column match

Hi, I have a tab delimited text file where I want to delete all rows that have the same string for column 1. How do I go about doing that? Thanks! Example Input: aa 1 aa 2 aa 3 bb 4 bc 5 bb 6 cd 8 Output: bc 5 cd 8 (4 Replies)
Discussion started by: evelibertine
4 Replies

5. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. UNIX for Dummies Questions & Answers

How to Detect Specific Pattern and Print the Specific String after It?

I'm still beginner and maybe someone can help me. I have this input: the great warrior a, b, c and what i want to know is, with awk, how can i detect the string with 'warrior' string on it and print the a, b, and c seperately, become like this : Warrior Type a b c Im still very... (3 Replies)
Discussion started by: radynaraya
3 Replies

7. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

8. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

9. Shell Programming and Scripting

Print out specific pattern column data

Input file: adc_0123 haa_1000 bcc_520 adc_0150 bcc_290 adc_0112 haa_8000 adc_0139 haa_7000 Output file: adc_0123 adc_0123 haa_1000 bcc_520 adc_0150 adc_0150 bcc_290 (3 Replies)
Discussion started by: patrick87
3 Replies

10. Shell Programming and Scripting

Question about sort specific column and print other column at the same time !

Hi, This is my input file: ali 5 usa abc abu 4 uk bca alan 6 brazil bac pinky 10 utah sdc My desired output: pinky 10 utah sdc alan 6 brazil bac ali 5 usa abc abu 4 uk bca Based on the column two, I want to do the descending order and print out other related column at the... (3 Replies)
Discussion started by: patrick87
3 Replies

Featured Tech Videos