Editing ascii file with gawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Editing ascii file with gawk
# 1  
Old 10-11-2011
Editing ascii file with gawk

Hi, I have an ascii file and i'm just wondering is it possible to edit the file using gawk. I just want to do something simple like print the second field $2, however I don't seem to be able to work out what field seperator to use.

I've tried some code I found in a google search that looks like:
Code:
 
gawk 'BEGIN{FS=sprintf("%c",0x7F)} {print $2}' OFS="," geo_met_checks_stope.asc

However I'm not sure what the 0x7F stands for in ascii. When I open up the ascii file using an editor like notepad or wordpad the fields look like they are seperated by a tab or spaces. However using code like:
Code:
 
gawk -F"[\t ]" '{print $2}' OFS="," geo_met_checks_stope.asc

doesn't work. Any ideas?Smilie
# 2  
Old 10-12-2011
any problem by this ?

Code:
awk '{print $2}' geo_met_checks_stope.txt

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 10-12-2011
Thanks rdcwayx, that works greatSmilie

Can you tell me why this code works
Code:
 
awk '{print $2","$3}' OFS="," geo_met_checks_stope.asc >> junk.csv

Yet when I want to do a print $0 all the fields are jumbled together into one column.
Code:
 
awk '{print $0}' OFS="," geo_met_checks_stope.asc >> junk.csv

What i'm really looking for is how to quickly make a .csv file from an ascii using a shell script rather than manually doing it in excel using the "text to columns" menu option.

Last edited by theflamingmoe; 10-12-2011 at 02:59 AM.. Reason: code needs tweaking.
# 4  
Old 10-12-2011
If you need export a CSV style file, you are correct to set OFS, but use it in wrong way. Try this:

Code:
awk '{print $2 OFS $3}' OFS="," geo_met_checks_stope.asc >> junk.csv

But if you export $0 directly, you need add $1=$1 before the code.

try this:

Code:
awk '$1=$1' OFS=, infile

This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 10-12-2011
Thanks again rdcwayx, much appreciatedSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Hex to Ascii in a Ascii file

Hi All, I have an ascii file in which few columns are having hex values which i need to convert into ascii. Kindly suggest me what command can be used in unix shell scripting? Thanks in Advance (2 Replies)
Discussion started by: HemaV
2 Replies

2. UNIX for Dummies Questions & Answers

After Ftp'ing file to destination how to check the file if it is in correct ASCII and not corrupted

Hi Folks, While transferring file from FTP software like Filezilla the files gets corrupted. Is there any way I can check if the recently transferred file is in ASCII and not corrupted. I have tried using file -i filename command which does tell if the file character set is ASCII or binary... (6 Replies)
Discussion started by: Khan28
6 Replies

3. Shell Programming and Scripting

Gawk - to extract values from multi lined file -I

Hi, Request your help in getting help with the below text formatting using awk. I am still learning awk and your help here is appreciated. Thanks in advance. Desireoutput ---------------- Virtual Pool Destination Profile Profile Profile Profile 1. virtual-1 pool-1 212.254.110.174:https... (2 Replies)
Discussion started by: pratheeshp
2 Replies

4. Shell Programming and Scripting

How to remove a temporary file inside gawk

Hi- How can I make the temporary file 0 byte , created inside gawk. I am using system("rm -f temp_orders"); It seems system command is deleting file permanently and I am not able to execute below statement. print ORD_HEAD_FULL >> cFILE; (cFile is temp_orders) (2 Replies)
Discussion started by: ashish_kaithi
2 Replies

5. Windows & DOS: Issues & Discussions

Gawk Script in Windows batch file - Help

Good morning all. I have been running into a problem running a simple gawk script that selects every third line from an input file and writes it to an output file. gawk "NR%3==0" FileIn > FileOut I am attempting to run this command from a batch file at the command line. I have several hundred... (6 Replies)
Discussion started by: 10000springs
6 Replies

6. UNIX for Dummies Questions & Answers

Joining lines of a text file using GAWK

sir... am having a data file of customer master., containing some important fields as a set one line after another., what i want is to have one set of these fields(rows) one after another in line.........then the second set... and so on... till the last set completed. ... (0 Replies)
Discussion started by: KANNI786
0 Replies

7. Shell Programming and Scripting

gawk help for inserting a field of a .txt file in the same file

i had the following type of data file vchrdump: Vouchers For Date :05/01/2009 * ... (4 Replies)
Discussion started by: KANNI786
4 Replies

8. Shell Programming and Scripting

convert ascii values into ascii characters

Hi gurus, I have a file in unix with ascii values. I need to convert all the ascii values in the file to ascii characters. File contains nearly 20000 records with ascii values. (10 Replies)
Discussion started by: sandeeppvk
10 Replies

9. Shell Programming and Scripting

gawk - multiple sorting within a file

Hi, I am new shell programming and trying work on the follwoing file: 1 rs1 1 rs12 1 rs3 1 rs8 2 rs2 2 rs15 2 rs3 3 rs5 3 rs9 3 rs4 I would like to sort my 2nd column within 1, 2, and 3 separately. The output will be: 1 rs1 1 rs3 1 rs8 1 rs12 (4 Replies)
Discussion started by: ezhil01
4 Replies

10. Shell Programming and Scripting

awk,gawk in bat file

Hi. I'm trying to convert bat file into shell script. Bat file invokes awk file in one section: c:\upg\exe\gawk -f c:\upg\awk\gen_sae.awk -v OP=C:\\upg\\lod\\... ...c:\upg\ref\saaxi.ref c:\upg\log\SAAEPWO.log c:\upg\ref\saaepref.log First of all I issued unix2dos command on that awk file.... (0 Replies)
Discussion started by: andrej
0 Replies
Login or Register to Ask a Question