Visit Our UNIX and Linux User Community


awk regexp to print repetitive pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk regexp to print repetitive pattern
# 1  
Old 07-15-2016
awk regexp to print repetitive pattern

How to use regexp to print out repetitive pattern in awk?
Code:
$ awk '{print $0, "-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-"}'

output:
Code:
 -    -    -    -    -    -    -    -    -    -    -    -

I tried following which does not give what I want, of course.
Code:
awk '{print $0, "-\t{11}-"}'

output:
Code:
  -    {11}-

Any help, please? Thanks in advance!
# 2  
Old 07-15-2016
one way:
Code:
awk 'BEGIN{$11=OFS="-\t";print}'</dev/null

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 07-15-2016
Thanks!
Can you elaborate your script, please?

Last edited by yifangt; 07-15-2016 at 02:50 PM..
# 4  
Old 07-15-2016
It is a trick; setting the last field forces a reformatting with the OFS. (The previous fields are empty.) The order matters, OFS must be set first. Therefore I think the following is cleaner
Code:
awk 'BEGIN{OFS="-\t"; $12="-"; print}' </dev/null

Perl has the x operator
Code:
perl -e '{print "-\t"x11, "-\n"}'

In bash and zsh:
Code:
{ for i in {1..11}; do printf -- "-\t"; done; printf -- "-\n"; }


Last edited by MadeInGermany; 07-15-2016 at 04:18 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 07-15-2016
Quote:
It is a trick; setting the last field forces a reformatting with the OFS. (The previous fields are empty.) The order matters, OFS must be set first.
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!
# 6  
Old 07-15-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!
No. You can say that a string argument in an awk print statement is a string; not a regular expression. If you want an ERE to select lines containing exactly 11 copies of "-\t" followed by a final "-" and only print those lines, you could try:
Code:
awk 'BEGIN{print "start"; OFS="-\t"; $12="-"; print; print "end"}'|awk '/^(-\t){11}-$/'

which uses a regex in the 2nd awk script to print just that matched line out of the three lines printed by the 1st awk script

Note that redirecting the input from /dev/null won't hurt anything, but it isn't needed in an awk script that only contains one or more BEGIN clauses.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 07-15-2016
Thanks Don and all!
What I was doing is to combined two files (each half million lines) by a matching column, if no match (0.01%), make up the missing fields with the "-", which triggered me asking if there is regex for my purpose:
Code:
$ cat file2
XLOC_000001 TCONS_00000001 LOC_Os02g39790.2 47.273 55 24 2 3855 4016 335 385 3.60e-04  
XLOC_000001 TCONS_00000002 LOC_Os02g39790.2 47.273 55 24 2 2368 2529 335 385 2.35e-04  
XLOC_000001 TCONS_00000007 LOC_Os02g39790.2 47.273 55 24 2 3553 3714 335 385 3.09e-04  
XLOC_000001 TCONS_00000009 LOC_Os02g39790.2 47.273 55 24 2 5083 5244 335 385 2.83e-04  
XLOC_000001 TCONS_00000011 LOC_Os02g39790.2 47.273 55 24 2 2200 2361 335 385 2.28e-04   
$ cat file1
TCONS_00000001    W5GKA3_WHEAT
TCONS_00000002    W5GKA3_WHEAT
TCONS_00000011    I1IBH3_BRADI
TCONS_00000009    W5GKA3_WHEAT
TCONS_00000005    I1IBH3_BRADI
TCONS_00000006    I1IBH3_BRADI
TCONS_00000007    W5GKA3_WHEAT

Code:
$ awk 'NR==FNR {A[$2]=$0; next}; {if (A[$1]) print $0, A[$1]; else print $0, "-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-" }' 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

Typing that long string "-\t" repetitively looks dull, and I felt dizzy counting the number of tabs while typing. I made mistakes and need re-run a couple of times to get it correct! So I thought if there is regex I could avoid the mistake.
Thanks again!

Last edited by yifangt; 07-15-2016 at 08:32 PM.. Reason: typos

Previous Thread | Next Thread
Test Your Knowledge in Computers #40
Difficulty: Easy
HTML stands for 'Hyper Technology Transfer Protocol'.
True or False?

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

Featured Tech Videos