Print last 4 columns (variable column #)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print last 4 columns (variable column #)
# 1  
Old 02-25-2004
Print last 4 columns (variable column #)

I have rows of data with variable number of columns.

Is there an easy way to print out the last 4 columns
or rather the 4th and 3rd last column?

data looks like this:
Code:
           24 20:51 N 9 10.00 Overcast OVC110 27 11 30.04 1017.7
         24 19:51 N 7 10.00 Mostly Cloudy BKN110 28 15 30.03 1017.2
      24 18:51 N 5 10.00 Partly Cloudy FEW080 SCT100 29 14 32 27 30.02 1017.0

I need:
Code:
27 11
28 15
14 32

# 2  
Old 02-25-2004
i am sure you could but useing awk could prove a bit difficult since your colums never line up (assumeing you had a space as a field seperator)

your looking at a full fledged program since each line could be differant.

if you could put a field seperator into your data it would be extreamly easy to get the parts you want.
# 3  
Old 02-25-2004
How about: awk '{print $(NF-3), $(NF-2)}'
# 4  
Old 02-25-2004
I can do that.
What if I delimit with a pipe?
Does that help?

lynx -dump "http://www.crh.noaa.gov/data/obhistory/KALB.html" | grep "24 " | awk {'print $1 "|" $2 "|" $3 "|" $4 "|" $5 "|" $6 "|" $7 "|" $8 "|" $9 "|" $10 "|" $11 "|" $12 "|" $13 "|" $14'}

Code:
24|23:51|N|9|10.00|Fair|CLR|21|5|30.09|1019.3|||
24|22:51|N|5|10.00|Fair|CLR|22|7|30.08|1019.0|||
24|21:51|N|8|10.00|Fair|CLR|24|9|30.06|1018.3|||
24|20:51|N|9|10.00|Overcast|OVC110|27|11|30.04|1017.7|||
24|19:51|N|7|10.00|Mostly|Cloudy|BKN110|28|15|30.03|1017.2||
24|18:51|N|5|10.00|Partly|Cloudy|FEW080|SCT100|29|14|32|27|30.02
24|17:51|NE|7|10.00|Overcast|OVC100|29|16|30.01|1016.6|||
24|16:51|E|6|10.00|Overcast|OVC100|31|18|30.01|1016.5|||
24|15:51|E|6|10.00|A|Few|Clouds|FEW080|31|16|30.01|1016.7|
24|14:51|Vrbl|3|10.00|Overcast|BKN075|OVC090|32|17|30.02|1017.1||
24|13:51|NE|3|10.00|Overcast|BKN080|OVC090|30|16|30.05|1018.0||
24|12:51|Calm|10.00|Mostly|Cloudy|BKN080|28|15|28|20|30.08|1019.1|
24|11:51|Vrbl|3|10.00|Fair|CLR|26|14|30.11|1019.9|||
24|10:51|N|7|10.00|Fair|CLR|25|13|30.12|1020.2|||
24|09:51|N|7|10.00|Mostly|Cloudy|BKN085|24|14|30.13|1020.6||
24|08:51|N|6|10.00|Mostly|Cloudy|BKN090|22|15|30.13|1020.8||
24|07:51|N|6|10.00|Overcast|OVC090|20|15|30.15|1021.4|||
24|06:51|N|5|10.00|Overcast|BKN100|OVC120|20|15|24|19|30.14|1021.2
24|05:51|N|5|10.00|A|Few|Clouds|FEW100|20|14|30.14|1021.2|
24|04:51|N|7|10.00|Fair|CLR|21|14|30.14|1021.1|||
24|03:51|N|3|10.00|A|Few|Clouds|FEW100|21|14|30.15|1021.3|
24|02:51|N|5|10.00|Fair|CLR|22|14|30.16|1021.6|||
24|01:51|N|5|10.00|Fair|CLR|22|14|30.17|1022.1|||
24|00:51|N|7|10.00|Fair|CLR|24|15|32|24|30.17|1021.9|

# 5  
Old 02-25-2004
If you're gonna do that, just print the one or two fields that you need. It's crazy to print all the fields, then look for a way to strip most of them off...
# 6  
Old 02-25-2004
Quote:
Originally posted by Perderabo
How about: awk '{print $(NF-3), $(NF-2)}'
Perderabo lays the smack down shows how easy it is for an old pro.

Code:
NF    number of fields in the current record

# 7  
Old 02-25-2004
Quote:
Originally posted by Perderabo
If you're gonna do that, just print the one or two fields that you need. It's crazy to print all the fields, then look for a way to strip most of them off...
This is what I am asking.
Due to the variable column number in each row
I am having trouble figuring out how to do this.

Basically I want the highest number from one column
and the lowest from the other.

I'll bold the two columns I am interested in.

Code:
24 20:51 N 9 10.00 Overcast OVC110 27 11 30.04 1017.7
24 19:51 N 7 10.00 Mostly Cloudy BKN110 28 15 30.03 1017.2
24 18:51 N 5 10.00 Partly Cloudy FEW080 SCT100 29 14 32 27 30.02 1017.0

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding columns from 2 files with variable number of columns

I have two files, file1 and file2 who have identical number of rows and columns. However, the script is supposed to be used for for different files and I cannot know the format in advance. Also, the number of columns changes within the file, some rows have more and some less columns (they are... (13 Replies)
Discussion started by: maya3
13 Replies

2. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

awk print columns and variable

Hi, Can anyone help with the below please? I have written some code which takes an input file, and and prints the contents out to a new file - it then loops round and prints the same columns, but increments the ID column by 1 each time. Input file; NAME,1,15-Dec-15, NAME,1,21-Dec-15,... (9 Replies)
Discussion started by: Ads89
9 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

6. Shell Programming and Scripting

Need to find a column from one file and print certain columns in second file

Hi, I need helping in finding some of the text in one file and some columns which have same column in file 1 EG cat file_1 aaaa bbbb cccc dddd eeee fffff gggg hhhh cat file_2 aaaa,abcd,effgh,ereref,name,age,sex,........... bbbb,efdfh,erere,afdafds,name,age,sex.............. (1 Reply)
Discussion started by: jpkumar10
1 Replies

7. Solaris

awk - Print variable number of colums from a starting column

Hi guys, I usualy am able to google awk stuff but I can't find it so far and there are so many awking gurus here that I will give it a shot. I want to print $1;$3;"$5 up to the $NF". In other words, I can have 1000 colums, but need to have $5 up to the end. I started with the idea of... (2 Replies)
Discussion started by: plmachiavel
2 Replies

8. UNIX for Dummies Questions & Answers

How to copy one columns and print to the last column in unix?

I want to copy column no 3 to the end of column example : alter table RECOVER_USR.MPULKIXD rename to alter table RECOVER_USR.CS_ADV_PROMO rename to alter table RECOVER_USR.BCH_HISTORY_TABLE rename to alter table BILLOPS.HISHAM_DATAPLUS_FINAL rename to alter table... (8 Replies)
Discussion started by: arifahel
8 Replies

9. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies

10. Shell Programming and Scripting

can awk print column using a variable ??

i want to print the column file using awk or cut in dynamic manner like trmp=2;temp1=1;temp3=2 awk 'BEGIN{OFS=IFS="\t"} {print $temp,$temp1,$temp3}' client_data.txt or cut -f $temp1,$temp2,$temp3 -d"\t" file_name . but it is showing error , In awk can i use variable as in printing... (36 Replies)
Discussion started by: jambesh
36 Replies
Login or Register to Ask a Question