Print a specific length using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print a specific length using awk
# 1  
Old 01-04-2015
Print a specific length using awk

Hello, I have the following input data:

Code:
A C
AA GG
A G
CG AG

the code I tried:
Code:
awk -F" " '{ if ((length($1==1) && (length($2==1))) input_file

is not working, I need the output to be (such that only the columns containing one letter are printed)
Code:
A C
A G

Any help would be appreciated!

Last edited by Rabu; 01-04-2015 at 08:34 PM..
# 2  
Old 01-04-2015
Code:
awk -F" " '{ if ((length($1==1) && (length($2==1))) input_file

was close, but has mismatched quotes, parentheses, and braces; misplaced parentheses; and does not specify an action to be performed if the condition in the if statement evaluates to true. This should work:
Code:
awk '{ if (length($1)==1 && length($2)==1) print}' input_file

or, more simply, (using the default action for a given condition):
Code:
awk 'length($1)==1 && length($2)==1' input_file

# 3  
Old 01-05-2015
try also:
Code:
awk '!/[^ ][^ ]/ && /./' infile

# 4  
Old 01-05-2015
Quote:
Originally Posted by rdrtx1
try also:
Code:
awk '!/[^ ][^ ]/ && /./' infile

If infilecontains:
Code:
a 
 b

the above script will print both lines even though neither one meets the specified criteria. However, if the input never contains more than two fields and both fields always contain at least one character, it will do what is wanted.
# 5  
Old 01-05-2015
Quote:
...the output to be (such that only the columns containing one letter are printed)
The lines that contain only one letter columns are printed?
# 6  
Old 01-05-2015
Quote:
Originally Posted by rdrtx1
The lines that contain only one letter columns are printed?
You're right. The specification is ambiguous as to whether lines containing any single character field are to be printed or only lines with the 1st two fields each being a single character are to be printed. The incomplete awk code provided seemed to be looking for fields 1 and 2 being single characters and allowing any number of other fields containing anything. But, the English description can be interpreted several ways including that just the single character values in the 1st two fields (not entire lines) should be printed whenever one or both of the 1st two fields are single characters. And, the sample input doesn't provide any guidance.

If one of us didn't guess correctly at the intent, Rabu will have to provide a better description of exactly what is wanted.
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 string if tag is specific value

In the below awk I am trying to print expName only if another tag planExecuted is true. In addition to the expName I am also printing planShortID. For some reason the word experiment gets printed so I remove it with sed. I have attached the complete index.html as well as included a sample of it... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. UNIX for Dummies Questions & Answers

How to print specific range using awk?

I want to print specific range of rows and then its columns using awk command. lets say if a file contain 15 line then i need to print line 5 to 9. awk '{print;for( NR>=5&&NR<=9); do; print "<tr>\n<td>"$1"</td><td>"$2"</td><td>"$3"</td>\n</tr" done}' xyz.csv not sure what's wrong... (9 Replies)
Discussion started by: amyt1234
9 Replies

3. Shell Programming and Scripting

How to print with awk specific field different from specific character?

Hello, i need help with awk. I have this file: cat number DirB port 67 er_enc_out 0 er_bad_os 0 DirB port 71 er_enc_out 56 er_bad_os 0 DirB port 74 er_enc_out 0 er_bad_os 0 DirB port 75 ... (4 Replies)
Discussion started by: elilmal
4 Replies

4. Shell Programming and Scripting

awk to print fixed length columns to right side

Hi, I am in a situation to print the message on a column, where the each line starting position should be same. For example code: HOSTNAME1="1.2.3.4.5.6.7" TARGET_DIR="/tmp" echo "HOSTNAME1:" "$HOSTNAME1" | awk -v var="Everyone" '{len=55-length;printf("%s%*s\n",$0,len,var)}' echo... (4 Replies)
Discussion started by: tprabhaker
4 Replies

5. Shell Programming and Scripting

Find and print specific date with awk

hi all I would like to help me find the problem with this script to find and print to the screen a specific date of a log file that I have on my server, the date it is received as the first argument in the script $ 1 Here I show you a few lines that made ​​the idea of ​​my log file: ****... (4 Replies)
Discussion started by: gilmore666
4 Replies

6. Shell Programming and Scripting

How to print Specific keyword, by using awk?

How to print Specific keyword, by using awk.? prime:root:I want output. 78 1457 10000 10000 5985 307 10000 10000 10000 10000 3760 692 6656 157 696 (4 Replies)
Discussion started by: ooilinlove
4 Replies

7. Shell Programming and Scripting

Problems to print specific lines with awk and grep...HELP!

Hi all I have data like this: model: 1, misfit value: 0.74987 1 1.182 1.735 2.056 1.867 2 0.503 1.843 2.018 1.888 3 2.706 2.952 2.979 1.882 4 8.015 3.414 3.675 1.874 ... (1 Reply)
Discussion started by: fedora2011
1 Replies

8. Shell Programming and Scripting

To print a specific line in Shell or awk.

Hi, I want to echo the 15th line from a file named as abc.txt, also i want to echo only the values in that line not the line number. Thanks in advance:) (4 Replies)
Discussion started by: tushar_tus
4 Replies

9. Shell Programming and Scripting

How to print specific lines with awk

Hi! How can I print out a specific range of rows, like "cat file | awk NR==5,NR==9", but in the END-statement? I have a small awk-script that finds specific rows in a file and saves the line number in an array, like this: awk ' BEGIN { count=0} /ZZZZ/ { list=NR ... (10 Replies)
Discussion started by: Bugenhagen
10 Replies

10. Shell Programming and Scripting

Specific Length awk Results

I am awk-ing a line, and only want to get the results of the five chrs. following my field seperator. the line of prose is: Unit=01982 (PICO RIVERA) Unit=I1001 (INTERMOUNTAIN AREA) Unit=E1000 (INTERMOUNTAIN WEST DIVISION) failing in using an awk of: awk -F= '/Unit/{print $2 length(5)}'... (3 Replies)
Discussion started by: gozer13
3 Replies
Login or Register to Ask a Question