Sponsored Content
Full Discussion: awk spitting columns
Top Forums UNIX for Beginners Questions & Answers awk spitting columns Post 302995696 by Don Cragun on Monday 10th of April 2017 08:59:40 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
COLUMN(1)						    BSD General Commands Manual 						 COLUMN(1)

NAME
column -- columnate lists SYNOPSIS
column [-tx] [-c columns] [-s sep] [file ...] DESCRIPTION
The column utility formats its input into multiple columns. Rows are filled before columns. Input is taken from file operands, or, by default, from the standard input. Empty lines are ignored. The options are as follows: -c Output is formatted for a display columns wide. -s Specify a set of characters to be used to delimit columns for the -t option. -t Determine the number of columns the input contains and create a table. Columns are delimited with whitespace, by default, or with the characters supplied using the -s option. Useful for pretty-printing displays. -x Fill columns before filling rows. column exits 0 on success, >0 if an error occurred. ENVIRONMENT
COLUMNS The environment variable COLUMNS is used to determine the size of the screen if no other information is available. EXAMPLES
(echo "PERM LINKS OWNER GROUP SIZE MONTH DAY HH:MM/YEAR NAME"; ls -l | sed 1d) | column -t SEE ALSO
colrm(1), ls(1), paste(1), sort(1) HISTORY
The column command appeared in 4.3BSD-Reno. BSD
March 9, 2008 BSD
All times are GMT -4. The time now is 03:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy