awk regexp to print repetitive pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk regexp to print repetitive pattern
# 8  
Old 07-15-2016
I'm a little confused since the code you showed us is printing tabs between the hyphens in the output, but the sample output you provided from that command has four spaces at those spots instead of a singe tab. And, the output you showed us should have output lines in the order of the lines in file1, but it doesn't. It also seems strange that there are three trailing spaces on each line in your sample file2, but there are zero, two, or three spaces on the corresponding lines in your sample output. I don't know if that matters for the output you hope to produce, but it makes it hard to guess at what you are really trying to do???

So, with lots of unsupported guesswork, the following (combining earlier suggestions with your code to merge files and making the wild assumption that if you want tabs between hyphens, you might also want tab to be your output field separator) might or might not come close to producing the output you want:
Code:
awk '
NR==FNR {
	A[$2] = $0
	if(FNR == 1) {
		NFA = NF
		$0 = ""
		OFS = "-\t"
		$NFA = "-"
		dashes = $0
		OFS = "\t"
	}
	next
}
{	print $0, (($1 in A) ? A[$1] : dashes)
}' file2 file1

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 07-16-2016
Quote:
Originally Posted by yifangt
Can I say there is no regexp for this case in awk, similar to your perl script ?perl -e '{print "-\t"x11, "-\n"}'
Thanks a lot!
You seem to be caught in an error: regexes exist for searching/matching purposes ONLY. You can't print them. For printing a repetitive pattern, you need to type it as is, use a loop, or fall back to tricks as posted before.
This User Gave Thanks to RudiC For This Post:
# 10  
Old 07-17-2016
Adding a varying number of fields to an incomplete line might be achieved like
Code:
awk '
NR == FNR       {A[$2] = $0
                  next
                }
!A[$1]          {A[$1] = sprintf ("%*s", 14-NF, "-")
                 gsub (/ /, "-\t", A[$1])
                }
                {print $0, A[$1]
                }
' file2 file1 
TCONS_00000001    W5GKA3_WHEAT XLOC_000001 TCONS_00000001 LOC_Os02g39790.2 47.273 55 24 2 3855 4016 335 385 3.60e-04  
TCONS_00000002    W5GKA3_WHEAT XLOC_000001 TCONS_00000002 LOC_Os02g39790.2 47.273 55 24 2 2368 2529 335 385 2.35e-04  
TCONS_00000011    I1IBH3_BRADI XLOC_000001 TCONS_00000011 LOC_Os02g39790.2 47.273 55 24 2 2200 2361 335 385 2.28e-04   
TCONS_00000009    W5GKA3_WHEAT XLOC_000001 TCONS_00000009 LOC_Os02g39790.2 47.273 55 24 2 5083 5244 335 385 2.83e-04  
TCONS_00000005    I1IBH3_BRADI -	-	-	-	-	-	-	-	-	-	-	-
TCONS_00000006    I1IBH3_BRADI -	-	-	-	-	-	-	-	-	-	-	-
TCONS_00000007    W5GKA3_WHEAT XLOC_000001 TCONS_00000007 LOC_Os02g39790.2 47.273 55 24 2 3553 3714 335 385 3.09e-04


EDIT: Or just use a for loop...




.

Last edited by RudiC; 07-17-2016 at 07:18 AM..
This User Gave Thanks to RudiC For This Post:
# 11  
Old 07-18-2016
It started to deviate from my original question, and before it goes too far, I accept the answer by RudiC
Quote:
Can I say there is no regexp for this case in awk, similar to your perl script ?
Code:
perl -e '{print "-\t"x11, "-\n"}'

You seem to be caught in an error: regexes exist for searching/matching purposes ONLY. You can't print them. For printing a repetitive pattern, you need to type it as is, use a loop, or fall back to tricks as posted before.
Thanks again for all your replies.

Last edited by yifangt; 07-18-2016 at 01:21 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies

2. Shell Programming and Scripting

Help with using awk to print pattern/occurence

Hi, Do anybody know how to use awk to count the pattern at specific column? Input file M2A928K 419 ath-miR159a,gma-miR159a-3p,ptc-miR159a 60 miR235a . . Output file M2A928K 419 ath-miR159a,gma-miR159a-3p,ptc-miR159a 60 miR235a 3 . . I plan to count how many "miR" in column 3... (2 Replies)
Discussion started by: cpp_beginner
2 Replies

3. Shell Programming and Scripting

awk, sed or perl regexp to print values from file

Hello all According to the following file (orignal one contains 200x times the same structure...) I was wondering if someone could help me to print <byte>??</byte> values example, running this script/command like ./script.sh xxapp I would expect as output: 102 116 112 ./script.sh xxapp2... (2 Replies)
Discussion started by: cabrao
2 Replies

4. Shell Programming and Scripting

AWK: Grep Pattern and print help

I wanted to get outcome from a big file with pattern quoted: Line FSP LSP SR RL Test1 100 300 4 4000 Test2 1 300 2 300 Any help is greatly appreciated. Thank you. (15 Replies)
Discussion started by: rtsiahaan
15 Replies

5. Shell Programming and Scripting

print pattern between two variables awk sed

I am trying to print text between two variables in a file I have tried the following things but none seem to work: awk ' /'$a'/ {flag=1;next} /'$b'/{flag=0} flag { print }' file and also sed "/$a/,/$b/p" file But none seem to work Any Ideas? Thanks in Advance (5 Replies)
Discussion started by: forumbaba
5 Replies

6. Shell Programming and Scripting

Print lines between two repetitive patterns

Hi users I have one file which has number of occurrence of one pattern examples Adjustmenttype,11 xyz 10 dwe 9 abd 13 def 14 Adjustmenttype,11 xyz 24 dwe 34 abd 35 def 11 nmb 12 Adjustmenttype, not eleven .... ... ... (2 Replies)
Discussion started by: eranmoh
2 Replies

7. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

8. Shell Programming and Scripting

Perl Repetitive Pattern Matching

Problem: GIVEN ======= my $sql="INSERT INTO table_nm(a, b, b, d, e, f , g) VALUES (2046, TODAY, 'Change Subscription Name', '00000000000002000000000000000000000000000000000000', '00000000000001000000000000000000000000000000000000', '00000000000000000000000000000000000000000000000000', 1);... (2 Replies)
Discussion started by: Niroj
2 Replies

9. UNIX for Dummies Questions & Answers

print the line immediately after a regexp; but regexp is a sentence

Good Day, Im new to scripting especially awk and sed. I just would like to ask help from you guys about a sed command that prints the line immediately after a regexp, but not the line containing the regexp. sed -n '/regexp/{n;p;}' filename What if my regexp is 3 word or a sentence. Im... (3 Replies)
Discussion started by: ownins
3 Replies

10. Shell Programming and Scripting

awk how to print if the search pattern contains speace

the data file is as below: > cat master.cnf /usr| location for usr|5 /src/ver1| version 1 |10 /src/ver2/log| ver 2 log |25 /src/sys/apps/log| Application log for sys|36 /src/sys/apps/conf| configuration location for app|45 /src/sys/apps/bin| binary location app|55my script is as below: ... (1 Reply)
Discussion started by: McLan
1 Replies
Login or Register to Ask a Question