awk print columns and variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print columns and variable
# 8  
Old 01-29-2016
There's a lot of threads in these fora dealing with exactly this problem. Try searching these fora, and come back with any problems that might arise.
# 9  
Old 01-29-2016
If field two in your input file is always 1, the following might be simpler:
Code:
awk '{for(i=1; i<=CNT; i++){print; sub("," i ",", "," i+1 ",")}}' CNT=3 TestCashflow.csv | sort -t, -k2,2n > test.csv

or:
Code:
awk  -F, '{for(i=1; i<=CNT; i++){$2=i; print}}' OFS=, CNT=3 TestCashflow.csv | sort -t, -k2,2n > test.csv

or, if maintaining the order of the rows in the input file is important (the example input happens to be sorted in the same sequence that the default sort utility will use for printing records with the same 2nd field; so it doesn't matter for the given example):
Code:
awk '{l[NR]=$0} END{for(i=1; i<=CNT; i++) for(j=1; j<= NR; j++){print l[j]; sub("," i ",", "," i+1 ",", l[j])}}' CNT=3 TestCashflow.csv > test.csv

Even if maintaining the input line order in the output doesn't matter, the last example above will be faster than the other two because it only needs one process to do the work instead of needing to invoke both awk and sort.

As always, if someone wants to try any of these examples on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
# 10  
Old 01-30-2016
Two more awks without sorting:
Code:
awk '{A[NR]=$0} END{for(i=1; i<=n; i++) for(j=1; j<=NR; j++) {$0=A[j]; $2=i; print}}' n=3 FS=, OFS=, file

And this should also work in most awks and RS= must be set equal to an arbitrary character that does not occur in the file..
Code:
awk '{for(i=1; i<=n; i++) {for(j=2; j<=NF; j+=3) $j=i; print}}' n=3 FS=, OFS=, RS=* ORS= file


Last edited by Scrutinizer; 01-30-2016 at 04:38 AM..
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

awk print columns which are not null

I am working on a file with several columns as below MO_NAME,FAULT_TYPE,CLASS,CODE1,CODE2,CODE3 RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2A,53,58 RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2B,24 RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2A,33 RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2D,57 ... (12 Replies)
Discussion started by: Rizwan Rasul
12 Replies

3. Shell Programming and Scripting

Awk: is it possible to print into multiple columns?

Hi guys, I have hundreds file like this, here I only show two of them: file 1 feco4_s_BB95.log ZE_1=-1717.5206260 feco4_t_BB95.log ZE_1=-1717.5169250 feco5_s_BB95.log ZE_1=-1830.9322060... (11 Replies)
Discussion started by: liuzhencc
11 Replies

4. UNIX for Dummies Questions & Answers

awk question - print columns with names

I found this command and would like to know what it means: gawk 'NR==1{for(i=1;i<=NF;i++)if($i~/FE/)f=i}{for(i=0;i<n;i++)printf"%s%s",i?" ":"",$f;print""}' It seems to mean if the row =1 assign i to that row, and then if FE is in the top row /column then increment and print the row. I am... (2 Replies)
Discussion started by: newbie2010
2 Replies

5. Shell Programming and Scripting

Awk print all columns in delimited file

text file example 1,222222222222,333,444444444444444 111111111,22,33333333,444 desired output 1 222222222222 333 444444444444444 111111111 22 33333333 444I have a delimeted file which I want to print in a table like format. The... (10 Replies)
Discussion started by: Calypso
10 Replies

6. Shell Programming and Scripting

Removing columns from awk '{ print $0 }'

I have a one-line command, lsusb | awk '{ $1=""; $2=""; $3=""; $4=""; $5=""; $6=""; print $0 }' It works, and gives the results I expect, I was just wondering if I am missing some easier way to nullify the first 6 column variables? Something like, lsusb | awk '{ $(1-6)=""; print $0 }' But... (10 Replies)
Discussion started by: AlphaLexman
10 Replies

7. Shell Programming and Scripting

using awk to print some columns of a file

Hi, i have a file with content 00:01:20.613 'integer32' 459254 00:01:34.158 459556 00:01:36.626 'integer32' 459255 and i want to print only output as below 00:01:20.613 459254 00:01:34.158 459556 00:01:36.626 459255 i dont want the word 'integer32' which is the second column. i... (2 Replies)
Discussion started by: dealerso
2 Replies

8. Shell Programming and Scripting

cannot print the columns i want with awk.

hi friends! i have a script where a execute a veritas command, available_media wich retrieves me a list of tapes .lst then i execute cat /tmp/listtapes.lst | grep -v VL |sed '/^$/d'|awk -F, '{print $1, $3, $4, $9} ' > /tmp/media1.lst but it prints all the columns instead of the four... (3 Replies)
Discussion started by: pabloli150
3 Replies

9. Shell Programming and Scripting

AWK script to print all the columns excpet the one specified

I have several columns by the name A B C D E...... and I want to print all the column other than column C and D. Could you please help me with the awk script? Thanks!! (3 Replies)
Discussion started by: kn.naresh
3 Replies

10. UNIX for Dummies Questions & Answers

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: 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... (19 Replies)
Discussion started by: Da_Duck
19 Replies
Login or Register to Ask a Question