Removing asterix (*) from flat files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing asterix (*) from flat files
# 1  
Old 10-18-2007
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 means a decimal (.) point.

For past few days, I have been getting 123456789123,12*
This occurs ONLY in one field.

I need a script that will help me remove this asterix from the affected accounts, ONLY in that field. If there are asterix in other fields, I dont want to remove them.

Can anyone help me?
# 2  
Old 10-18-2007
Hi,

You can use awk to replace the "*" only in the corresponding field (the 3rd one in the example bellow).

Example:

(I am assuming your input/output files will be TAB separated)

input:
a 123 123456789123,123 HA***
b 124 123456123456,12* HB123


Code:
awk 'BEGIN {FS="\t"; OFS="\t"} {gsub("\*","",$3); print $0}' input_file > output_file


output:
a 123 123456789123,123 HA***
b 124 123456123456,12 HB123

So, only the "*" characters in the 3rd field are removed. In the 4th field "*" are not removed, as desired.

Let me know if it worked.

Last edited by key; 10-20-2007 at 09:58 PM..
# 3  
Old 10-18-2007
Try

Code:
sed "s/\*//g" < input_file > output_file

# 4  
Old 10-18-2007
Quote:
Originally Posted by lorcan
Try

Code:
sed "s/\*//g" < input_file > output_file

Better yet, do NOT try that, because it will remove *any* asterisks instead of only the ones you want to remove.

First, you have to determine where in line the field is, where you want to remove the asterisk. As an example I will suppose the field is on column 20-30, if you have a delimited format instead of a fixed length format you will have to change accordingly.

To phrase the substitute-expression to only remove asterisks in the columns 5-15 you will have to:

cut out columns 5-15 from the line
run the substitution on it
put the changed part of the line back in

This could be done by sed alone, but would be rather complicated, involving the use of the hold space and in the end the whole script would be pretty counterintuitive (I'll provide the solution upon reqquest if anybody is interested, but it is nothing for the ones with poor nerves ;-) ). An easy way to do it is in a shell script though:

Code:
#!/bin/ksh

typeset line=""
typeset begin=""
typeset end=""
typeset middle=""

exec 3> file.changed

cat file | while read line ; do
     # chop off the first 4 chars from $line, storing them in $begin
     begin="${line%%${line##????}}" ; line="${line##${begin}}"
     # chop off the next 10 chars from $line, storing them in $middle
     # and put the remainder in $end
     middle="${line%%${line##??????????}}" ; end="${line##${middle}}"

     # here we change the cut out field
     middle="$( print - $middle | sed 's/\*//g')"

     # put everything together

     print -u3 "${begin}${middle}${end}"
done

exec 3>&-
exit 0

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting Multiline Files to Flat Files?

How to convert this: F1-R1 F1-R2 F1-R3 into a flat file for bash?? Each record F2-R1 F2-R2 F2-R3 F3-R1 F3-R2 F3-R3 F4-R1 F4-R2 F4-R3is on one line with all fields for that record, put into an output file. The output file should look like this when converted: F1-R1,F2-R1,F3-R1,F4-R1... (6 Replies)
Discussion started by: bud1738
6 Replies

2. Programming

Scripting using flat files

I need some help to create a script that can do the following: I have two flat files with one column that should link the two files' information into one record: 1st Flat File - 3 columns, multiple rows: orderid, Jobnumber, Ordernumber 2nd Flat File - 2 columns, multiple rows: ... (4 Replies)
Discussion started by: Lavelle
4 Replies

3. Shell Programming and Scripting

handling asterix in AWK

I have a file like below. 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 colA^col2^col3^col4^col5 aa^11^aaa^a1a^111^aa... (5 Replies)
Discussion started by: rinku11
5 Replies

4. Shell Programming and Scripting

Compare 2 flat files

Hi Frnds, I have a flat file with millions of records. . Now I on this. (I prefer for AWK as its gives good performance.) Old_file.txt ------------------ 1 gopi ase .... 2 arun pl ... 3 jack sutha .. 4 peter pm .. ... New_file.txt --------------- 4 peter pm .. .. ... (12 Replies)
Discussion started by: Gopal_Engg
12 Replies

5. Shell Programming and Scripting

Removing commas within semicolon in a flat file

i am recieving a flat file ( comma seperated ) with comma in between double quotes in any of the source fields . i need to remove the comma in double quotes and process the file thereafter fields in file ========= col1,col2,col3,col4 input can be any of the followng... (31 Replies)
Discussion started by: r_t_1601
31 Replies

6. Shell Programming and Scripting

Removing commas within semicolon in a flat file

Hi , Im relatively new to unix and have to process a comma serparated flat file . I recieve some of the fields in double quotes and i want to remove it .. INPUT ==== filed1,field2,field3,"fie,ld4" OUTPUT ===== field1,field2,field3,"field4" can anyone tell me how to achieve... (10 Replies)
Discussion started by: r_t_1601
10 Replies

7. UNIX for Dummies Questions & Answers

Removing trailer from a flat file!!!

Hi, I get some flat files with trailer which gives the totol records count and i want to remove the trailer from the file. i used the following command it works fine with a single file. cat file_name | grep -v 'Total records:' > file1 mv file file_name But i dont know how to remove the... (12 Replies)
Discussion started by: kumarsaravana_s
12 Replies

8. Shell Programming and Scripting

Compare 2 flat files

Hi Gurus, I searched the forum but didnt get much info. I want to compare 2 files. 1)Newfile comes today with 2)Old file of previous day. The files are same ,just the new files might have new records sometimes. So I want to capture these new records in another file. Can anyone help... (5 Replies)
Discussion started by: ganesh123
5 Replies

9. Shell Programming and Scripting

Help with Flat Files Please!! BASH (New User)

Hello All, I am brand new to the UNIX world and so far and very intrigued and enjoy scripting. This is just a new language for me. I would really like assistance with the below request. Any help would be greatly appreciated! I want to create a flat file in Vi that has a header field and... (0 Replies)
Discussion started by: cyberjax21
0 Replies

10. Shell Programming and Scripting

Flat Files

I have a flat file like this 0001 THER ULT HEAD & NECK VES 0002 THER ULTRASOUND OF HEART 0003 THER ULT PERIPHERAL VES 0009 OTHER THERAPEUTIC ULTSND 0010 IMPLANT CHEMOTHERA AGENT 0011 INFUS DROTRECOGIN ALFA 0012 ADM INHAL NITRIC OXIDE I need to conver this to a comma delimited flat file... (2 Replies)
Discussion started by: thumsup9
2 Replies
Login or Register to Ask a Question