awk printing: strange result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk printing: strange result
# 1  
Old 07-11-2009
Bug awk printing: strange result

Dear all,

I am using awk in a bash script to extract a list of x y z coordinates from a file such as:

%BEGIN 3D-SPACE COORDINATES
0.2085627338147950 0.2471306816410478 0.2085627338147950
0.1242549179185660 0.2755539793525220 0.4147884486606120
0.2030669560265720 0.3983372756773921 0.4596781191013200
0.7067131708643269 0.3305772813679979 0.4725877506177361
0.2145984149333280 0.3226923154644570 0.4418263501706269
0.6311637442761178 0.3184506694177131 0.4399444001080688
%END 3D-SPACE COORDINATES

my awk comand is:
awk '/%BEGIN 3D-SPACE COORDINATES/,/%END 3D-SPACE COORDINATES/' $name.geom | awk '{print" "$1" "$2" "$3" "}' | awk '!/COORDINATES/' > temp1

What I get is:

0.2308162774655759 0.1480521440577194 0.5021283247946412^M
0.1511096424690066 0.1233060439441793 0.4500675923279874^M
0.2023251655747262 0.2714463932173767 0.4821937835506821^M
0.4354815336547525 0.2936641560636020 0.4621607016307354^M
0.2240460471092144 0.2045465724198916 0.4711570022223424^M
0.3374757152666932 0.2634647746292220 0.4370902435765608^M
0.5903967673979990 0.4016591662102160 0.3766061929875720^M
0.6632378213716280 0.6461468540777490 0.3725764025724530^M
0.3531727567629250 0.3667579928368630 0.3740439462339640^M

As can be seen there is a "^M" at the end of the z coordinate.

My question is:
has anyone met such a problem before, and how may it be avoided.

Any help would be very welcome.

Thank you very much!!

Pauli.

Last edited by pauli; 07-11-2009 at 10:06 AM..
# 2  
Old 07-11-2009
The given input and output don't have any relationship with each other, post the output of the given input file and desired output.

Regards
# 3  
Old 07-11-2009
Bug awk printing help

Hi Frankin52,

I am extracting a data block, exactly as below, from a large file.


Code:
%BEGIN 3D-SPACE COORDINATES
0.2085627338147950 0.2471306816410478 0.2085627338147950
0.1242549179185660 0.2755539793525220 0.4147884486606120
0.2030669560265720 0.3983372756773921 0.4596781191013200
0.7067131708643269 0.3305772813679979 0.4725877506177361
0.2145984149333280 0.3226923154644570 0.4418263501706269
0.6311637442761178 0.3184506694177131 0.4399444001080688
%END 3D-SPACE COORDINATES

What I need to get is as follows:

Code:
0.2085627338147950 0.2471306816410478 0.2085627338147950
0.1242549179185660 0.2755539793525220 0.4147884486606120
0.2030669560265720 0.3983372756773921 0.4596781191013200
0.7067131708643269 0.3305772813679979 0.4725877506177361
0.2145984149333280 0.3226923154644570 0.4418263501706269
0.6311637442761178 0.3184506694177131 0.4399444001080688

However I get this “^M” at the end of the z coordinate:

Code:
0.2085627338147950 0.2471306816410478 0.2085627338147950^M
0.1242549179185660 0.2755539793525220 0.4147884486606120^M
0.2030669560265720 0.3983372756773921 0.4596781191013200^M
0.7067131708643269 0.3305772813679979 0.4725877506177361^M
0.2145984149333280 0.3226923154644570 0.4418263501706269^M
0.6311637442761178 0.3184506694177131 0.4399444001080688^M

The awk command that I am using is:

Code:
awk '/%BEGIN 3D-SPACE COORDINATES/,/%END 3D-SPACE COORDINATES/' input | awk '{print" "$1" "$2" "$3" "}' | awk '!/COORDINATES/' > output

I would be grateful for any assistance on this.

Thanks again,

Pauli.

Last edited by pauli; 07-11-2009 at 10:07 AM.. Reason: code tags, PLEASE!
# 4  
Old 07-11-2009
With your command I get the desired output however, it's needless to use awk twice, this command should be sufficient:

Code:
awk '/%BEGIN 3D-SPACE COORDINATES/,/%END 3D-SPACE COORDINATES/ {if(!/COORDINATES/){print $1,$2,$3}}'

Have you transfer the file from a windows environment?
# 5  
Old 07-12-2009
Bug

Hi Frankin52,

Thanks for your reply and your tip on awk. The files which I am extracting data from are created by windows softwear. And I am using WinSCP to transfer them to my Linux account in binary mode.

Pauli
# 6  
Old 07-12-2009
Quote:
Originally Posted by pauli
Hi Frankin52,

Thanks for your reply and your tip on awk. The files which I am extracting data from are created by windows softwear. And I am using WinSCP to transfer them to my Linux account in binary mode.

Pauli
Use this command to convert the dos/windows file to a unix format:

Code:
 tr -d '\r' < dosfile > unixfile

Regards
# 7  
Old 07-13-2009
Bug

Hi Frankin52,

Thanks for your help on this issue. That seems to have done the trick!!

No more crazy "^M"

Cheers,

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Duplication | awk | result

Dear forum members, I want the script to count ALA as one (an example in quotes) and return an integer as 1 and not return 5 as an integer as it does now (look bash script). So how can I upgrade my script that it first checks or after finding all instances of ALA checks whether it is the same... (25 Replies)
Discussion started by: Aurimas
25 Replies

2. UNIX for Dummies Questions & Answers

Strange result using find command.

I created a file with the permissions of 776. When I ran the command find /root/Desktop -perm -644 -type f The created file shows up as part of the results. Doesn't -perm -mode mean that for global, only 4(read) and 2(write) can be accepted ? (2 Replies)
Discussion started by: Hijanoqu
2 Replies

3. Shell Programming and Scripting

Awk: Comparing arguments with in line values of file and printing the result

I need to develop a script where I will take two date arguments as parameter date1 and date2 which will in format YYYYMM. Below is the input file say sample.txt. sample.txt will have certain blocks starting with P1. Each block will have a value 118,1:TIMESTAMP. I need to compare the... (7 Replies)
Discussion started by: garvit184
7 Replies

4. Shell Programming and Scripting

awk Strange behaviour in AIX

Can someone please explain the strange behaviour.. I was just trying a few things to learn awk.. in the below code when I start the braces in the same line, the output is as expected, when I start at next line, output is displayed twice. Please see the file, code I tried and output below. ... (2 Replies)
Discussion started by: Kulasekar
2 Replies

5. Shell Programming and Scripting

Strange behaviour of arrays in awk

Imagine 2 files f1 f2: file1_l1_c1 code_to_find file1_l1_c3 file1_l2_c1 file1_code2 file1_l2_c3 file1_l3_c1 file1_code3 file1_l3_c3 file2_l1_c1 file2_l1_c2 code_to_find file2_l2_c1 file2_l2_c2 file2_code5 file2_l3_c1 file2_l3_c2 file2_code3 Say we want to print lines from f2 having... (5 Replies)
Discussion started by: ripat
5 Replies

6. Shell Programming and Scripting

Strange result

Hi, I have following codes which looks ok: $ string1="123456789 abc2" $ string2="abc" $ position_of_string2=`expr index "$string1" "$string2"` $ echo $position_of_string2 $ 11however, when string2="abc2", it gives me the following result: $ string1="123456789 abc2" $... (5 Replies)
Discussion started by: littlewenwen
5 Replies

7. Shell Programming and Scripting

awk filelist containing strange characters

I've written a script: find -depth | awk ‘ { if ( substr($1,length($0)-2,3) == “/1.” ) { print $1 } { system(“awk -f test1.awk “ $1 ) } } ‘ The idea is that it trundles through a large directory structure looking for files which are named '1.' and then... (3 Replies)
Discussion started by: nashcom
3 Replies

8. Shell Programming and Scripting

Strange result of eval, how does eval really work with ssh?

Hi all, some small script with eval turned me to crazy. my OS is linux Linux s10-1310 2.6.16.53-0.8.PTF.434477.3.TDC.0-smp #1 SMP Fri Aug 31 06:07:27 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux below script works well #!/bin/bash eval ssh remotehost date eval ssh remotehost ls below... (1 Reply)
Discussion started by: summer_cherry
1 Replies

9. Shell Programming and Scripting

Strange variable comparison result in awk

So, I'm making a little awk script that generates a range-based histogram of a set of numbers. I've stumbled onto a strange thing. Toward the end of the process, I have this test: if ( bindex < s ) "bindex" is the "index" of my "bin" (the array element that gets incremented whenever a... (2 Replies)
Discussion started by: treesloth
2 Replies

10. Shell Programming and Scripting

printing in certain column based on some result

hii every one can anybody help me writing shell script with this, i have a file ... amit arun vivek and i want to read something from the user and print next to amit or arun in certain column.. like amit 23-wall street 2000 arun 34343 vivek 4758 is... (6 Replies)
Discussion started by: kumar_amit
6 Replies
Login or Register to Ask a Question