handling asterix in AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting handling asterix in AWK
# 1  
Old 02-03-2011
handling asterix in AWK

I have a file like below.
Code:
colA^col2^col3^col4^col5
aa^11^aaa^a1a^111^aa*
bb*^22^bbb*^bb2^222^bb
cc^33^ccc*^3cc^333^ccc
dd^44^d*dd*^d4d^444^ddd
ee^55^e*ee^e5e*^555^e*e

NOTE: '^' is the field separator.

I need to get the output as
Code:
colA^col2^col3^col4^col5
aa^11^aaa^a1a^111^aa
bb^22^bbb*^bb2^222^bb
cc^33^ccc*^3cc^333^ccc
dd^44^ddd*^d4d^444^ddd
ee^55^eee^e5e^555^ee

Explanation:
Basically I need to look at the column3. I need to retain only the asteric (*) present as last character. Any other asteric in any other fields should be trimmed. Even if the asteric is present in somewhere in between the 3rd field and also as last character in 3rd field. I only have to retain the last one and trim the middle one.

eg. d*dd* to ddd*

Summerizing, asteric is only allowed as last character in 3rd field. Any other asteric should be trimmed.

Thanks in advance for help.

Last edited by Scott; 02-03-2011 at 12:33 PM.. Reason: Code tags, please...
# 2  
Old 02-03-2011
Does it have to be AWK?
Code:
perl -laF"\^" -pe '$F[2]=~s/\*(?!$)//g;$x=$F[2];map s/\*//g, @F;$F[2]=$x;$_=join "\^",@F' file

# 3  
Old 02-03-2011
Or sed:
Code:
sed 's/**^/@&/3;s/@^/^/;s/*//g;s/@^/*^/' infile

# 4  
Old 02-03-2011
try:
Code:
awk 'BEGIN{FS="^"}{for(i=1;i<=NF;i++) {if(i!=3) {gsub("\*","",$i);printf (i==NF?$i:$i"^")} else{if($i~/\*/ && i==3) {gsub("\*","",$i); printf $i"*^"}}}print ""}' urfile

# 5  
Old 02-03-2011
Code:
awk '{t=($3~/*$/)?"*":X;gsub(/*/,"");$3=$3 t}1' FS="^" OFS="^"  infile

# 6  
Old 02-04-2011
Thanks to all for the respose. It really helped. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Handling 2 files simultaneously with awk

Hello, Is it possible to handle data from two different files at once in awk (latest version and platform is Fedora). I found on the net that we cannot nest awk. My requirement is that I have two similar files : File 1: Name: abc Val = 58 Name: cdf Val = 1; .................. File... (7 Replies)
Discussion started by: fifteate
7 Replies

2. UNIX for Dummies Questions & Answers

Awk - Handling different types of newlines

Hi. We have some data that's generated from a webpage. Part is pretty well-formatted, but part of it preserves newlines in a way that breaks the record separating in awk. Here's 2 records, filtered through cat -e: Jones,Bob,20,Q: What is your favorite ice cream?$ A: Butter Pecan$ Q: Do you... (6 Replies)
Discussion started by: treesloth
6 Replies

3. Shell Programming and Scripting

Data handling using AWK

Hi, I have requirement to fetch lines with a particular character in one column e.g. 2.5M asdsad 3.5M sadsadas 12323M ssdss i tried following so far #echo 2.3M asdsad | nawk -F " " '{print substr($1,length($1))}' M So far i have tried following # echo 2.3M asdsad | nawk... (4 Replies)
Discussion started by: mtomar
4 Replies

4. UNIX for Advanced & Expert Users

awk function in handling quotes

Hi all, I have input lines like below empno,ename,sal,description ---------------------------- 311,"jone,abc",2000,manager 301,david,200,"president,ac" I need to sum the salary of them i.e. 2000+200 anything suggested Thanks, Shahnaz. Use code tags. (5 Replies)
Discussion started by: shahnazurs
5 Replies

5. Shell Programming and Scripting

handling arrays with awk

Hi, I have an issue that I am trying to resolve using arrays in awk. I have two files, the first one is a dictionary with this format: FILE 1 (dictionary) 'Abrir' 'Open' 'Aceptar' 'Accept' Every line has two fields, a word in two languages. The second file is a simple list of... (3 Replies)
Discussion started by: gmartinez
3 Replies

6. Shell Programming and Scripting

Handling regular expressions in awk

Script is: accept filename as argument(also handle CTRL+C).to check whether th file exist in the current directory,it it then using awk find the employees who are either born in 1990 or drawing a salary greater than 25000. In my database file 1st field is of id ,2nd field is name,5th field is of... (5 Replies)
Discussion started by: Priyanka Bhati
5 Replies

7. Shell Programming and Scripting

column handling in awk

Dear Scripting experts, I have a problem which i cannot get my head around and wondered if anyone can help me. I have two files "file1" and "file2" and i want to replace column one from file 1 with column one with file2.(where file two has many columns). see example.. ive tried to use cut and... (4 Replies)
Discussion started by: Mish_99
4 Replies

8. Shell Programming and Scripting

Handling special characters using awk

Hi all, How do I extract a value without special characters? I need to extract the value of %Used from below and if its greater than 80, need to send a notification. I am doing this right now..Its giving 17%..Is there a way to extract the value and assign it to a variable in one step? df |grep... (3 Replies)
Discussion started by: sam_78_nyc
3 Replies

9. Shell Programming and Scripting

Removing asterix (*) from flat files

Hi all, I have to generate a tab-delimited flat file. Since few days, I have been getting * in random accounts. For example, an account supposed to have the value 123456789123,123 Now this is supposed to be in a 12,3 format. Please note that this being a German field, the comma (,) here... (3 Replies)
Discussion started by: ctrl_alt_del
3 Replies

10. Shell Programming and Scripting

AWK handling of single quote

Hi, Can someone let me know how I can acheive the following. I have ~ delimited file and I need to convert into something like SQL insert statements. SrcFile : 1~sjdsdj~asasas~ 2~aaaaa~qwqwqwq~qwq ..... I tried AWK -F"~" '{print "INSERT INTO XX VALUES("$1 " ,\' "$2" \' , \' "$3 }'... (3 Replies)
Discussion started by: braindrain
3 Replies
Login or Register to Ask a Question