awk spitting columns


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk spitting columns
# 1  
Old 04-10-2017
awk spitting columns

if i have +2, i want this to be + 2 as separate columns. Doing this for a large column so I cannot do it manually.
# 2  
Old 04-10-2017
What is the reason for doing this?

Do you want to do this only when + is followed by a single digit 2 in a column; or anytime + is followed by a string whose 1st character is 2.

What have you tried to solve this on your own?

What operating system and shell are you using?

What is the format of your "column" and the file in which it is located?

Please show us a sample input file (in CODE tags) and the corresponding output file (in CODE tags) that you want to produce from that sample input.
# 3  
Old 04-10-2017
Code:
cat maser_neg_test.txt | awk '{print NR, $0}' | awk '{print $1, $2, ((15 * $3) + ((1/4) * $4) + ((1/240) * $5)), (($6)+ ($7/60) + ($8/3600) ,$9}' | awk '{printf "%s %-15s %-10s %-10s %-6s\n", $1, $2, $3, $4 , $5}' > maser_neg_test2.txt

is my code, which transforms

Code:
RXSJ00001+0523   00 00 11.78  +05 23 17.4  11992  2016-02-12   51.3   3  10.9  10631  13365
    KUG2358+330   00 00 58.10  +33 20 38.0  12921  2012-11-17   36.5   8   4.0  11461  14395
0001233+4733537   00 01 23.30  +47 33 53.7   5237  2010-11-02   39.5  10   3.6   3848   6639   3.5   6358   9196
       NGC-7805   00 01 26.76  +31 26 01.4   4850  2006-01-05   43.8   5   6.0   3464   6248   5.6   5968   8799

into

Code:
1 RXSJ00001+0523      0.04908      5.38817    11992 
2 KUG2358+330     	     0.24208     33.3439    12921 
3 0001233+4733537   0.34708      47.5649     5237  
4 NGC-7805        	     0.36150     31.4337     4850

but my research advisor noted that in my conversion of

Code:
dec:
1*(hr) = degree_1
(1/60) * (min) = degree_2
(1/3600) * (sec) = degree_3

Code:
degree_1 + degree_2 + degree_3 = dec (degrees)

which is the data +05 23 17.4 as hr min sec, that just adding them when the sign is negative does not combine these right. So i'm trying to pull out the sign before doing my calculations and then re-apply it

Last edited by Don Cragun; 04-10-2017 at 07:45 PM.. Reason: Add CODE and ICODE tags.
# 4  
Old 04-10-2017
There is no need for cat in this pipeline and there is no need for two of your three invocations of awk.

There is no occurrence of the string +2 in your sample input and there is nothing wrong with your calculation as long as the leading character in hour field in your input file is a plus sign. But, your calculation would be wrong if the leading character in that field was a minus sign (which does not appear in your sample input). If we assume that the 1st character in the hour field will ALWAYS either be a plus sign (+) or a minus sign (-), the following seems to do what I would guess you were trying to do:
Code:
awk '{	printf("%s %-15s %-10s %-10s %-6s\n",
	    NR, $1, $2 * 15 + $3 / 4 + $4 / 240,
	    (substr($5, 1, 1) == "-" ? -1 : 1) * \
		(substr($5, 2) + $6 / 60 + $7 / 3600),
	    $8)
}' maser_neg_test.txt > maser_neg_test2.txt

If we add a couple of timestamps with negative hours to your sample maser_neg_test.txt for testing:
Code:
RXSJ00001+0523   00 00 11.78  +05 23 17.4  11992  2016-02-12   51.3   3  10.9  10631  13365
    KUG2358+330   00 00 58.10  +33 20 38.0  12921  2012-11-17   36.5   8   4.0  11461  14395
0001233+4733537   00 01 23.30  +47 33 53.7   5237  2010-11-02   39.5  10   3.6   3848   6639   3.5   6358   9196
       NGC-7805   00 01 26.76  +31 26 01.4   4850  2006-01-05   43.8   5   6.0   3464   6248   5.6   5968   8799
extra	2 3 4 -05 59 60 8 9 10 11 12 13 14 15 16 17
extra2	2 3 4 -10 30 00 8 9 10 11 12 13 14 15 16 17

the above code produces the following output in maser_neg_test2.txt:
Code:
1 RXSJ00001+0523  0.0490833  5.38817    11992 
2 KUG2358+330     0.242083   33.3439    12921 
3 0001233+4733537 0.347083   47.5649    5237  
4 NGC-7805        0.3615     31.4337    4850  
5 extra           30.7667    -6         8     
6 extra2          30.7667    -10.5      8

which I assume is appropriate output for that input.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. Shell Programming and Scripting

Sum of columns using awk

Hello everyone I am a beginner in Shell scripting. Need your help to achieve desired result. I have a file (sample format below) 001g8aX0007jxLz xxxxxxxxxxxxxxx 9213974926411 CO-COMM-133 CO-L001-DLY 7769995578239 44938 1 1 ... (1 Reply)
Discussion started by: Rohit Mallah
1 Replies

3. Shell Programming and Scripting

Gnuplot spitting junk to CL, saying file not found

Hi, I am making a shell script in bash which uses gnuplot to plot the number of road accidents on a certain day, on a certain month. I believe I have the data correct. An example of my data file is here: cat Day1Accidents.txt 01 13 02 5 03 17 04 8 05 16 06 18 07 12 08 7 09 23 10 12... (2 Replies)
Discussion started by: 06s23
2 Replies

4. Shell Programming and Scripting

Rearranging into new columns (awk?)

Hi experts, I've used several solutions from this forum to delete nonsense and rearrange data in the project file I'm working on. I'm hoping you guys can give me some tips on further rearranging the data (I've seen a few solutions by searching, but one specific item has me stumped, which is only... (5 Replies)
Discussion started by: coryvp
5 Replies

5. Shell Programming and Scripting

Removing columns using awk

HI , I have a comma delimiter file, in which I want to remove 8th and 9th column. I tried removing those columns using the below code awk 'BEGIN { FS=","; OFS="," } {$8=$9="";gsub(",+",",",$0)}1' infile But the problem is 8th and 9th columns are user entered fields, theyvhave carriage... (1 Reply)
Discussion started by: mora
1 Replies

6. Shell Programming and Scripting

Awk - New Line between columns

I have a data file with 4 columns, of the format: A1 A2 A3 A4 B1 B2 B3 B4 C1 C2 C3 C4 etc.. I would like to insert to put column 2,3,4 on a new line so my new format would be: A1 A2 A3 A4 B1 B2 B3 B4 C1 C2 C3 C4 etc. but am new at using AWK and am not sure how to do it. (5 Replies)
Discussion started by: mikeyd
5 Replies

7. UNIX for Dummies Questions & Answers

Using awk to get columns from different files

How can I use awk to create a new file that has 2 columns, each colums comes form a different file. example: I need column 3 from file1 and column 5 from file2 to make file3. (3 Replies)
Discussion started by: cosmologist
3 Replies

8. Shell Programming and Scripting

how to AWK columns from $1 to $5 without $2 $3 $4

hello cant find a way to make something like: awk '{print $1 - $5}' somefile which is printing $1 $2 $3 $4 $5 should make an array or something? i just dont wanna write $1 $2 $3 $4 $5 to awk input i need to use from $1 to $5 and print them all and then i need to swith to: from $6 to $10 (3 Replies)
Discussion started by: tip78
3 Replies

9. Shell Programming and Scripting

How to format columns using AWK

Hi, How to format something like this: John Roberts 324-141-984 Acct Jack Williams 159-555-555 Acct William Jackson 949-911-888 Acct Mark J Walton 145-852-252 Acct Fred P Milton 483-244-390 Acct Bill P J Miller 404-050-223 Acct into... (12 Replies)
Discussion started by: ora_umair
12 Replies

10. Shell Programming and Scripting

awk sum columns

can anyone help me how do i add the colums using awk seperated by character @. for eg i have 3@4 2@9 5@1 the result should be 10 14 i tried using { sum+= $1 } END { print sum } but it just gives the result 10. can anyone help me with this one thank you and best regards (7 Replies)
Discussion started by: phone_book
7 Replies
Login or Register to Ask a Question