Printing the column that begins with certain words/numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing the column that begins with certain words/numbers
# 1  
Old 05-21-2009
Printing the column that begins with certain words/numbers

Hi guys,

I have got a file which doesn't have the same number of columns in each line. I would like to print the second column and the one that begins with 33= and has some numbers after '33='

Can you please help me asap?

Cheers
# 2  
Old 05-21-2009
If you want the line to be printed where second column begins with 33
try this
awk ' { if (substr($2,1,2)==33) print $0 } ' filename

if you want to print only that column say print $2

A space/tab is assumed as field separator.
it it is something else
then
awk ' BEGIN {FS="whatever field separator"} {
if (substr($2,1,2)==33) print $0 } ' filename
# 3  
Old 05-21-2009
Thanks, but the thing is that this column that begins with '33=' is not the second column, its number is changing. That's why I have problems with printing it.

Last edited by alexandra_ola; 05-21-2009 at 07:49 AM..
# 4  
Old 05-21-2009
Maybe I didn't make it clear. The thing is that each column means answer in questionaire. Each column begins with number of question. There are some questions that could have more than one answer, than there are more columns. I'm intrested in question 1 (column 1) and question 33 (which column number is changing) that is why I would like to print the columns that begin with '33='
# 5  
Old 05-21-2009
why don't you show an example and show your output.
# 6  
Old 05-21-2009
Here's an example:

10049623031 0=43 1=0 2=3 3=3 4=3 4=6 4=7 4=9 5=0 5=1 5=2 5=3 5=5 5=6 5=7 5=12 5=13 5=15 5=18 7=7 8=7 9=7 10=3 11=0 12=0 13=7 14=0 15=0 16=7 17=7 18=4 19=0 20=0 21=1 22=4 23=2 24=0 25=15 26=2 27=2 28=5 29=0 30=0 31=2 32=56 33=6500 34=1 34=2 34=3 34=7 34=8 35=1 35=11 35=16 35=17 1000000=0 1000001=3 1000002=2 1000003=2 1000005=2

108839092 0=33 1=0 2=1 3=4 4=25 5=19 7=9 8=9 9=9 10=9 11=9 12=9 13=9 14=9 15=9 16=9 17=9 18=9 19=0 20=0 21=2 22=5 23=3 24=3 25=22 26=1 27=3 28=12 29=5 30=3 31=0 32=0 33=2300 34=17 35=20 1000000=0 1000001=2 1000002=3 1000003=3 1000005=0

I would like my output to be
10049623031 33=6500
108839092 33=2300
# 7  
Old 05-21-2009
so its not the second column but the first. you said wrongly in your first post.
Code:
awk '{printf "%s ",$1;for(i=2;i<=NF;i++){if ($i~/33=/){print $i}}}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding numbers matching with words

Hi All, I have a file which looks like this: abc 1 abc 2 abc 3 abc 4 abc 5 bcd 1 bcd 3 bcd 3 bcd 5 cde 7 This file is just a miniature version of what I really have. Original file is some 1 million lines long. I have tried to come up with the code for what I wish to accomplish... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

2. Shell Programming and Scripting

Put numbers against the words

Hi All, I tried to solve this but the result gives me all zeros for one file. I failed to do for all 500 files. I have some 500 files with the extension .dat I have another set of files; 500 in number with extension .dic I created these .dic files by using sort -u from the actual .dat files.... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

3. UNIX for Dummies Questions & Answers

Trying to sort words and numbers associated with them.

Hi. I have a file containing words and numbers associated with them as follows - c 2 b 5 c 5 b 6 a 10 b 16 c 18 a 19 b 21 c 27 a 28 b 33 a 76 a 115 c 199 c 251 a 567 a 1909 (4 Replies)
Discussion started by: maq
4 Replies

4. Shell Programming and Scripting

Difference between words and not between numbers

Hi, Sorry in advance for propably a silly question, but I am a bit lost. On some of the linux job flow I have the following check: if ($file != 1500) then echo ERROR It works ok, all times $file is not equal to 1500 I have the error message. I try to do something similar... (7 Replies)
Discussion started by: essemario
7 Replies

5. Shell Programming and Scripting

printing words based on column match

pls help Input: file1 word1 text1 word2 text2 word3 text3 file2 word1 text11 word3 text13 can u pls help in getting the same output: file1 text1 text2 text3 (1 Reply)
Discussion started by: bha148
1 Replies

6. UNIX for Dummies Questions & Answers

Split words from 2 files and printing in csv

Hello Everyone, (1 Reply)
Discussion started by: thankful123
1 Replies

7. Shell Programming and Scripting

how to avoid few words to be printing.

Hi all. Am using the below command to get the database size for past two days.. grep total * |awk '{print $9/1024}' 560.746 1260.336 Now i want to see only the size in GB's which means i need to avoid the words from (.dot) and the output should come like below: 560 1260 (3 Replies)
Discussion started by: suri.tyson
3 Replies

8. Shell Programming and Scripting

Query to print numbers in words

Hi, I have to write a shell script that converts numbers in to words below is what i wrote.My script is not running. ----------------------------------- echo -n "Enter number : " read n len= echo $n | wc -c echo " number in words : " for ( i=1; i<len; i++ ) do num=echo $n... (5 Replies)
Discussion started by: bab123
5 Replies

9. Shell Programming and Scripting

Extract numbers below words with awk

Hi all, Please some help over here. I have a Sales.txt file containing info in blocks for every sold product in the pattern showed below (only for 2 products). NEW BLOCK SALE DATA PRODUCT SERIAL 79833269999 146701011945004 .Some other data .Some... (17 Replies)
Discussion started by: cgkmal
17 Replies

10. Web Development

Query to print numbers in words

Hi, If i give a number say "1234" the output of mysql query should be: one thousand and twenty four How to write mysql query for this? With regards Vanitha (5 Replies)
Discussion started by: vanitham
5 Replies
Login or Register to Ask a Question