printing select fields in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printing select fields in awk
# 1  
Old 06-22-2007
printing select fields in awk

Hi,

I want to print certain fields from my data file depending on certain conditions. Somebody pls let me know how to send it to awk.

The command below is the one which I want to use in a shell script and this prints fine
cat ./datafile.dat | grep -i $SEARCH_STR | awk -F: '{ print $1 $2 $3 }'

Is there any way we can have a variable called DISP_STR and send it as follows:
DISP_STR="\$1 \$2 \$3"
cat ./datafile.dat | grep -i $SEARCH_STR | awk -F: '{ print "$DISP_STR" }'

I tried this but it fails.

Any other ways to get this accomplished is welcome. :-)

Thanks for any help provided!!
maverix
# 2  
Old 06-22-2007
With the ":' field separator:
Code:
mPrtList="$1 $2 $3"
egrep -i "$SEARCH_STR" ./datafile.dat | awk -F: "{ print $mPrtList }"

Without the ":" field separator:
Code:
mPrtList='$1 $2 $3'
egrep -i "$SEARCH_STR" ./datafile.dat | awk -F: "{ print $mPrtList }"

# 3  
Old 06-22-2007
Code:
DISP_STR="\$1 \$2 \$3"
SEARCH_STR=search_string
eval awk -F: \'/$SEARCH_STR/ { print $DISP_STR } \' inile

# 4  
Old 06-22-2007
Hello Shell Life and aigles,

Thanks to both of you for your time.

mPrtList="$1 $2 $3"
egrep -i "$SEARCH_STR" ./datafile.dat | awk -F: "{ print $mPrtList }"

I had tried this earlier but this seems to print all the fields in the selected records.

The one below seems to have worked Smilie
DISP_STR="\$1 \$3 \$5"
cat ./datafile.dat | grep -i $SEARCH_STR | eval awk -F: \'/$SEARCH_STR/ { print $DISP_STR } \'

This gives out only the columns asked for.

Thanks again for the response.
cheers
maverix
# 5  
Old 06-22-2007
Replace:
Code:
cat ./datafile.dat | grep -i $SEARCH_STR | eval awk -F: \'/$SEARCH_STR/ { print $DISP_STR } \'

by
Code:
eval awk -F: \'/$SEARCH_STR/ { print $DISP_STR } \' ./datafile.dat

# 6  
Old 06-22-2007
Maverix,
Use the single quotes version:
Code:
mPrtList='$1 $2 $3'
egrep -i "$SEARCH_STR" ./datafile.dat | awk -F: "{ print $mPrtList }"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to clean up input file, printing both fields

In the f1 file below I am trying to clean it up removing lines the have _tn_ in them. Next, removing the characters in $2 before the ninth /. Then I remove the ID_(digit- always 4). Finally, the charcters after and including the first _. It is curently doing most of it but the cut is removing $1... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

awk move select fields to match file prefix in two directories

In the awk below I am trying to use the file1 as a match to file2. In file2 the contents of $5,&6,and $7 (always tab-delimited) and are copied to the output under the header Quality metrics. The below executes but the output is empty. I have added comments to help and show my thinking. Thank you... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. UNIX for Beginners Questions & Answers

Continued trouble matching fields in different files and selective field printing ([g]awk)

I apologize in advance, but I continue to have trouble searching for matches between two files and then printing portions of each to output in awk and would very much appreciate some help. I have data as follows: File1 PS012,002 PRQ 0 1 1 17 1 0 -1 3 2 1 2 -1 ... (7 Replies)
Discussion started by: jvoot
7 Replies

4. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

5. Shell Programming and Scripting

awk Selective printing of fields

Hi Gurus, I have following input file. I tried multiple awk combinations to print selected columns without success. HEX ID Name ver FLRGT Start Time Total Shared End Date ----- -------- --- ------ ------------------------ -------------- -------... (4 Replies)
Discussion started by: shunya
4 Replies

6. Shell Programming and Scripting

Select records and fields

Hi All I would like to modify a file like this: >antax gioq21 tris notes abcdefghij klmnopqrs >betax gion32 ter notes2 tuvzabcdef ahgskslsooin this: >tris abcdefghij klmnopqrs >ter tuvzabcdef ahgskslsoo So, I would like to remove the first two fields(and output field 3) in record... (4 Replies)
Discussion started by: giuliangiuseppe
4 Replies

7. Shell Programming and Scripting

select lines with certain values on certain fields with awk

I need a awk command to select from a log-file only the lines that have on the 2nd field (considering "|" separator) one of the values 10.216.22.XX or 10.216.22.YY or 10.216.22.ZZ and on the 4th field only values that contain strictly digits. I want the command to work parsing the file only once (I... (2 Replies)
Discussion started by: black_fender
2 Replies

8. Shell Programming and Scripting

awk with fields select?

If i have a log file record.txt, with 10 fields - First field is datetime - 7th field is status - 8th filed is name - The last field (10th) is epoch time of the first field 02/17/2012 1:47 PM||||||In Use|chicken||1329515230 02/17/2012 2:53 PM||||||Available|chicken||1329519195 02/17/2012... (4 Replies)
Discussion started by: sabercats
4 Replies

9. Shell Programming and Scripting

need help with awk in printing the fields in a file

hi all i get a file from a server and i do not know how many fields that file will contain. i need to cut from the second column of the file to the last, irrespective of how many fields are there. is there any way to make the awk command dynamic for fetching from the second to the last... (4 Replies)
Discussion started by: sais
4 Replies

10. Shell Programming and Scripting

AWK - printing certain fields when field order changes in data file

I'm hoping someone can help me on this. I have a data file that greatly simplified might look like this: sec;src;dst;proto 421;10.10.10.1;10.10.10.2;tcp 426;10.10.10.3;10.10.10.4;udp 442;10.10.10.5;10.10.10.6;tcp sec;src;fac;dst;proto 521;10.10.10.1;ab;10.10.10.2;tcp... (3 Replies)
Discussion started by: eric4
3 Replies
Login or Register to Ask a Question