Print last 4 columns (variable column #)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print last 4 columns (variable column #)
# 15  
Old 02-26-2004
$1 is the argument the the script...not some stuff read from standard input. Move the backtick date expression into the awk statement wher you need it.
# 16  
Old 02-26-2004
I get no result this way.
Code:
> awk -v f1=`date +'%d0454'` '/^f1/ {getline; print $0}' test.txt
*DEV1-/home/oracle
>

the only way so far that I could figure out how to do it was
to echo the statement as I want it into a file and then
run the file as a shell script.

I am sure I am missing some elementary syntax here but examples
I have found are not helping
# 17  
Old 02-27-2004
The syntax is tricky. You can't use a variable within "//". So to match lines beginning with f1 use...
Code:
awk -v f1=`date +'%d0454'` '$0~"^" f1 {getline; print $0}' test.txt

Note: to match lines where f1 matches the first field the syntax is simpler...
Code:
awk -v f1=`date +'%d0454'` '$1==f1 {getline; print $0}' test.txt

# 18  
Old 02-27-2004
How about just
awk '/^'`date +'%d0454'`'/ {getline; print $0}' test.txt'
# 19  
Old 02-27-2004
Ygor: 3
Perderabo: 1
Duck: -5

Ygor wins! Both lines work very well.
I swear I tried almost everything.

Why did I think $1==f1 should be in parens? ($1==f1)
I think I read that somewhere ...

what if I just want to match any 1st column that begins with
`date +'%d04'`?

Perderabo - thanks for the help. your last line hangs inside awk.
Possibly too many single quotes?

Last edited by Da_Duck; 02-27-2004 at 10:29 AM..
# 20  
Old 02-27-2004
answered my own question:
Code:
awk -v f1=`date +'%d04'` '$1~f1 {getline; print $0}' test.txt


thanks for all the patience and help!
 
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