print column that match reg expr


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print column that match reg expr
# 1  
Old 05-28-2009
print column that match reg expr

Hi all,

I want to cut a column which match the regular expression "beta", if I don't know the column number?

cat test

alpha;beta;gamma
11;22;33
44;55;66
77;88;99




should be

command ....

beta
22
55
88


I tried the following command, but unfortunately the output is only the regular expression not all the column rows!

awk -F";" 'match($0,"beta",a){print a[0]}' test

beta

Any ideas how can I solve this??
# 2  
Old 05-28-2009
use NR==1, then a for loop using NF as counter, loop through the fields one by one, checking for "beta". Get the value of the field number to a variable. After that print that value for every line.
# 3  
Old 05-28-2009
I will try to create the loop!

Thanks
# 4  
Old 05-29-2009
Code:
nawk -F";" '{
if(NR==1){
	for(i=1;i<=NF;i++){
		if($i=="beta"){
			n=i
			next
		}
	}
}
else{
	print $n
}
}' a.txt

# 5  
Old 05-29-2009
Thanks for you answer, but unfortunately I got the follow output:

Code:
~$ nawk -F";" '{if(NR==1){for(i=1;i<=NF;i++){if($i=="beta"){n=inext}}}else{print $n}}' test

11;22;33
44;55;66
77;88;99
# 6  
Old 05-29-2009
Code:
awk -F";" 'NR==1{for(i=1;i<=NF;i++)if ($i==...){n=i}}{print ....}' file

please familiarize yourself with the awk language by reading the gawk manual, available online.
# 7  
Old 05-29-2009
I solved this issue with the following script.

Code:
#!/bin/bash
#set -x

var=$(cat $2 | head -1 | sed 's/;/\n/g' | grep -n "$1" | cut -f1 -d":")

if [ -z $var ];then
echo "no string found"
else
cat $2 | cut -f$var -d";" 
fi


however ghostdog74 I going to take your advice about awk!

If you have any tutorials or tips regarding awk then please send it to me!

Thanks in advance!
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 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

2. Shell Programming and Scripting

awk strings search + print next column after match

Hi, I have a file filled with search strings which have a blank in between and look like this: S. g. Ehr. o. Jg. v. d. Chijs g. Ehr. Now i would like to search for the strings and it also shall return the next column after the match. awk -v FILE="search_strings.txt" 'BEGIN {... (10 Replies)
Discussion started by: sdf
10 Replies

3. Shell Programming and Scripting

print when column match with other file

Hello all, please help. There are two file like this: file1: 1197510.0 294777.7 9666973.0 21.6 1839.8 1197510.0 294777.7 9666973.0 413.2 2075.9 1197510.0 294777.7 9666973.0 689.3 2260.0 ... (1 Reply)
Discussion started by: attila
1 Replies

4. Programming

How to prevent incorrect string using reg expr in Java?

Hi All, I need your input on how to mask out / ignore a string that does not match a working regular expression (continually refining) pattern in Java. Below is the code snippet which is picking up all the lines with the correct regular expression string except one known so far: public... (0 Replies)
Discussion started by: gjackson123
0 Replies

5. Shell Programming and Scripting

Match and print particular column

Hi All, I am in need of help. I want to match a value of each row in “file 1” with the value of first row in “file 2” and print out only the columns that match. How can I do it in awk? Any help is greatly appreciated. for example, I have two files: Cat “File 1” ID 3 8 15 Cat “File... (2 Replies)
Discussion started by: newpro
2 Replies

6. Shell Programming and Scripting

perl: reg.expr: combine starting and ending removal in one exprecion

Hello, I am new in perl and in regular exprecion; so I am looking for help (or an experienced advise.) The target is a triming spaces from a string: i.e., remove spases from begining and from end of a string. One of main point of a searched solution is performance: for current task it is... (2 Replies)
Discussion started by: alex_5161
2 Replies

7. Shell Programming and Scripting

PERL: Simple reg expr validate 6 digits number

Hi there! I'm trying to validate a simple 6 digits number with reg expr. I ONLY want 6 digits so when i type 7 digits the script should no validate the number. I've write this code: #!/usr/bin/perl while(<STDIN>){ if($_=~/\d{6}/){ print "Bingo!\n"; ... (2 Replies)
Discussion started by: BufferExploder
2 Replies

8. UNIX for Dummies Questions & Answers

scipt dividing strings /reg expr

Hello! I've got txt-file containing lots of data in sentences like this: ;;BA;00:00:03:00;COM;CLOQUET-LAFOLLYE;SIMON; but sometime more than on in a line like this: ;;BA;00:00:03:00;COM;CLOQUET-LAFOLLYE;SIMON;;;BA;00:00:03:00;REA;RTL9;;;;BAC;:00;TIT;SEMAINE SPECIALE ~SSLOGAN~T DVD;; ... (3 Replies)
Discussion started by: maco_home
3 Replies

9. Shell Programming and Scripting

var substitution in a reg expr ?

In a shell script, how I can achieve substitution of shell script var to a regular expression, as shown below. var=`head -1 file1` awk '$0!~/$var/ {print $0}' file1 > file2 In the case above $var value literally considered for non-exists criteria. (3 Replies)
Discussion started by: videsh77
3 Replies

10. Shell Programming and Scripting

Text replace by position instead of reg expr.

Can we replace the contents the of the rows of file, from one position to another position by mentioning, some start position & the width? (4 Replies)
Discussion started by: videsh77
4 Replies
Login or Register to Ask a Question