awk if condition match and print all


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk if condition match and print all
# 1  
Old 02-06-2015
awk if condition match and print all

Hi, I am trying to do something like this ...
I use awk to match a pattern, and then print out all col.
My code is :
Code:
awk '{if ($1 ==300) print $1,$2-'$sbin7',$3}' tmp.txt

output=
Code:
300 2

whereby sbin7=2,

The thing is, I want to print all col and row, not just the matched line/row only, but I still want to make the change once pattern match.
I want to print out something like this:
Code:
10 6
300 2
303 1


any chance ? Smilie

tmp.txt content:
Code:
10 6
300 4
303 1


Last edited by Franklin52; 02-06-2015 at 07:55 AM.. Reason: Please use code tags
# 2  
Old 02-06-2015
Can you add code tags?

Code:
awk '{ print $1,$2-($1 == 300 ? '$sbin7' : 0),$3}' tmp.txt

This User Gave Thanks to anbu23 For This Post:
# 3  
Old 02-06-2015
Code:
 awk -vsbin7="$sbin7"  '{ print $1,$2-($1 == 300 ? sbin7 : 0),$3}'  tmp.txt

OR
Code:
 awk '{ print $1,$2-($1 == 300 ? sbin7 : 0),$3}' sbin7="$sbin7" tmp.txt

This User Gave Thanks to Akshay Hegde For This Post:
# 4  
Old 02-06-2015
Code:
awk '$1 == 300 {$2-=sbin7} 1' sbin7="$sbin7" file
10 6
300 2
303 1

This User Gave Thanks to RudiC For This Post:
# 5  
Old 02-08-2015
Hi All,
I am trying out the above example, found that all is useful Smilie
At the end I use something like this
Code:
istwo=2
awk '$1 == 300 {$2-='$istwo'} 1' file

But my output become like this, which the line format is out from original format:
Code:
      100     2    0.54
      200     1    0.32
300 2 0.15
      405     4    0.24

original file:
Code:
      100     2    0.54
      200     1    0.32
      300     4    0.15
      405     4    0.24

So, wonder how to retain the original format or close to it after apply the awk command above ? thks ! :>

Last edited by horsepower; 02-08-2015 at 11:13 PM.. Reason: wrong content
# 6  
Old 02-08-2015
any reason why you're not following the suggested solutions? What is it that din't work exactly as suggested?
# 7  
Old 02-08-2015
I follow exactly using one of the example above.
Some examples generate even more out of format output from the file, although it is successfully matching and did the job. thks !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. UNIX for Beginners Questions & Answers

awk - print when condition is met

I have a file.txt containing the following: Query= HWI-ST863:386:C5Y8UACXX:3:2302:16454:89688 1:N:0:ACACGAAT Length=100 Score E Sequences producing significant alignments: (Bits) Value ... (2 Replies)
Discussion started by: tons92
2 Replies

3. Shell Programming and Scripting

awk Match and Print

I have the following script in place that will print the values of FileB when the first column matches File A's first column. awk 'NR == FNR {A=$2;next};$1 in A {print $1,$NF,$2,$3,A}' FileA FileB Input FileA 3013 4 FileB 3013 2009 03 JUNK 43 Output 3013 43 2009 03 (2 Replies)
Discussion started by: ncwxpanther
2 Replies

4. Shell Programming and Scripting

Using awk for match and print

I have the need to match up the lat / lon from a fileA with the lat / lon and value from fileB. fileA is a small subset of fileB I have the following awk script but it prints out all the contents from fileB. I only need the matches. awk 'FNR==NR {A=$NF; next} {A=$NF} END{for(i in A) printf... (10 Replies)
Discussion started by: ncwxpanther
10 Replies

5. Shell Programming and Scripting

awk if condition match and fix print decimal place

Hi All, I have problem in the middle of implementing to users, whereby the complaint is all about the decimal place which is too long. I need two decimal places only, but the outcome from command is always fixed to 6. See the sample : before: Sort Total Site Sort SortName Parts ... (3 Replies)
Discussion started by: horsepower
3 Replies

6. Shell Programming and Scripting

awk question: How to print condition of NR & NF together.

Experts: LINE1 :This is line one The FIRST line of the file. LINE2 :This is line two LINE3 :This is line three with 8 fileds LINE4 :This is line four LINE5 :This is line five LINE6 :This is line six with 8 fileds I want to delete line 1, and then process the file and want to print lines... (2 Replies)
Discussion started by: rveri
2 Replies

7. Shell Programming and Scripting

[AWK script]Counting the character in record and print them in condition

.......... (1 Reply)
Discussion started by: Antonlee
1 Replies

8. Shell Programming and Scripting

AWK match and print

I have thousands of tables compiled in a single txt document that I'm parsing with AWK. Scattered throughout the document in random sections I would like to parse out the sections that look like this: 1 Seq. Descrição do bem Tipo do bem Valor do bem (R$) 2 1 LOCALIZADO ANA RUA PESSEGO N 96... (3 Replies)
Discussion started by: daveyabe
3 Replies

9. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

10. Shell Programming and Scripting

Awk to print on condition in input file

I need only those records which has $2 equal to "DEF" independent of case (i.e upper or lower) nawk -F"," '$2 ~ //{print $0}' file This returns 3rd record also which i dont want I tried this but this doesnt work as expected. nawk -F"," '$2 == ""{print $0}' file i dont... (3 Replies)
Discussion started by: pinnacle
3 Replies
Login or Register to Ask a Question

Featured Tech Videos