awk to print column number while ignoring alpha characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to print column number while ignoring alpha characters
# 1  
Old 05-12-2016
awk to print column number while ignoring alpha characters

I have the following script that will print column 4 ("25") when column 1 contains "123". However, I need to ignore the alpha characters that are contained in the input file. If I were to ignore the characters my output would be column 3.

What is the best way to print my column of interest while ignoring the alpha characters?

Code:
 awk '$1 ~/123/ {print $1, $4}' input

INPUT
Code:
W000123    0T  W   25  W    5  W   13  W    0  W    0T W    0  W    0  W    0  W    0  W    0T W    0T W   66  W   48  W    0  W
W000345   13  W   25  W    0T W   18  W    0T W    0  W  178  W    0  W   43  W    0  W    3  W   13  W    0  W    0  W    0  W

EXPECTED OUTPUT
Code:
W000123    25

# 2  
Old 05-12-2016
I do not see your logic clearly. But. What if we remove all alphas and go from there.
Code:
 awk '{
    a=$0
      gsub(/[A-Z]/, " ", a)
      split(a,arr," ")
      }
      arr[1] ~ /123/ i{print $1 , arr[3]}
     ' Somefile > newfile

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 05-12-2016
Quote:
Originally Posted by ncwxpanther
I have the following script that will print column 4 ("25") when column 1 contains "123". However, I need to ignore the alpha characters that are contained in the input file. If I were to ignore the characters my output would be column 3.

What is the best way to print my column of interest while ignoring the alpha characters?

Code:
 awk '$1 ~/123/ {print $1, $4}' input

INPUT
Code:
W000123    0T  W   25  W    5  W   13  W    0  W    0T W    0  W    0  W    0  W    0  W    0T W    0T W   66  W   48  W    0  W
W000345   13  W   25  W    0T W   18  W    0T W    0  W  178  W    0  W   43  W    0  W    3  W   13  W    0  W    0  W    0  W

EXPECTED OUTPUT
Code:
W000123    25

If you're just trying to print fields 1 and 4 if field 1 contains the string 123 anywhere in the field, I don't see why your current code above would not work.

If what you want is to print fields 1 and 4 only if field 1 starts with one or more uppercase alphabetic characters followed by zero or more zeros followed by the string 123 at the end of the field, you could try something more like:
Code:
awk '$1 ~ /^[[:upper:]]+0*123$/{print $1, $4}' input

or if you want a tab between fields in the output (which seems closer to the output you said you want):
Code:
awk '$1 ~ /^[[:upper:]]+0*123$/{print $1 "\t" $4}' input

# 4  
Old 05-13-2016
Thanks for the responses guys.

To be more clear:

I need to be able to print the contents of a specified column, while ignoring the alpha characters. In this case the "W" and "T". However, for those instances where "T" occurs, there is a numeric character which needs to be preserved. So the "0" needs to stay.

When we count the columns from left to right, we are not counting the columns with "W". In my example, the line that contains "123" would have 16 columns. I need to print just column 3. So my desired output would be 25.

I could make this a 2 step process, eliminating the alpha characters first, then count the columns, but was hoping for a simple 1 liner.

Thanks

---------- Post updated at 02:17 PM ---------- Previous update was at 07:37 AM ----------

I was able to utilize this script to extract out a single column of data. But what if i wanted multiple columns? Say columns 3, 4 and 5

Script
Code:
awk '{ a=$0 gsub(/[A-Z]/, " ", a)split(a,arr," ");print arr[1] , arr[3]} ' input.txt | awk '$1 ~/201604/'

Input
Code:
00013743201604  13  W   25  W    0T W   18  W    0T W    0  W  178  W    0  W   43  W    0  W    3  W   13  W    0  W    0  W    0  W    0  W    0  W    0

Output

Code:
00013743201604 25


Desired Output for Multiple Columns
Code:
00013743201604 25
00013743201604 0
00013743201604 18

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - Print column number that return value comes from

I have the following awk script that I am using to find the max value in the file and print results. awk 'BEGIN {MAX=-1E100} {for (x=2; x<=NF; x++) if ($x>MAX) {MAX = $x; C1 = $1}} END {print substr(C1,1,11), substr(C1,13,4), substr(C1,18,2), MAX}' ABC* Input (ABC*) ... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

2. Shell Programming and Scripting

Help with compare two column and print out column with smallest number

Input file : 5 20 500 2 20 41 41 0 23 1 Desired output : 5 2 20 0 1 By comparing column 1 and 2 in each line, I hope can print out the column with smallest number. I did try the following code, but it don't look good :( (2 Replies)
Discussion started by: perl_beginner
2 Replies

3. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

Find number of characters in a column and replace

Hi all, I want to count total no. of characters in a column. and if no. of charaters are more than 3 then it must replace it by splitted string. ie, it must place a space after 3 characters. Ex: 21 435g asd3dd jklfjwe wer column number 3 has 4 alphanumeric character, so it must be splitted... (3 Replies)
Discussion started by: CAch
3 Replies

5. Shell Programming and Scripting

How to print the first 7 characters of each column

Hello all, I have a data like this: X:04252 X:05524 X:04176 X:05509 X:05524 X:04674- X:1662912 X:10181 X:16491 X:05506 X:05216- X:05488 X:46872 X:08471 X:04834 X:30170 The except result is like this: X:04252 X:05524 X:04176 X:05509 X:05524 X:04674 X:16629 X:10181... (3 Replies)
Discussion started by: yhkoo
3 Replies

6. Shell Programming and Scripting

Count number of characters in particular column

Hi i have data like abchd 124 ldskc aattggcc each separated by tab space i want to count number of characters in 4th column and print it in new column with tabspace for every line can anyone help me how to do it. Thanks. (3 Replies)
Discussion started by: bhargavpbk88
3 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Using awk to obtain minimum of each column (ignoring zeros)

Hi, I have a wide and long dataset which looks as follows: 0 3 4 2 3 0 2 2 ... 3 2 4 0 2 2 2 3 ... 0 3 4 2 0 4 4 4 ... 3 0 4 2 2 4 2 4 ... .... I would like to obtain the minimum of each column (ignoring zero values) so the output would look like: 3 2 4 2 2 2 2 2 I have the... (3 Replies)
Discussion started by: kasan0
3 Replies

8. Solaris

awk - Print variable number of colums from a starting column

Hi guys, I usualy am able to google awk stuff but I can't find it so far and there are so many awking gurus here that I will give it a shot. I want to print $1;$3;"$5 up to the $NF". In other words, I can have 1000 colums, but need to have $5 up to the end. I started with the idea of... (2 Replies)
Discussion started by: plmachiavel
2 Replies

9. UNIX for Dummies Questions & Answers

reducing the number of characters in a column

Hi, I would like to take the first column of a bunch of lines and take only the 6th through 15th characters. The first column are not regular. gbAY277147.1Ptv3.T1469 CTTGAACAT gbAY277149.1Ptro3.T1469 CTTGAACAT gbAY287891.1Hs3.T1469 CTTGAACATTTGC into 7147.1Ptv3 CTTGAACAT... (4 Replies)
Discussion started by: mikey11415
4 Replies

10. UNIX for Dummies Questions & Answers

How to ignore characters and print only number using unix?

say D45H E67H G779K F8888U T66Y Y333U output shud be like 45 67 779 8888 66 333 (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question