Add two more fileds to awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add two more fileds to awk command
# 1  
Old 09-16-2013
Add two more fileds to awk command

This is what I was trying but failed to do so need help.

Quote:
cat 1.sql
A1
A2
A3
A4
B1
B2
B3
B4
C1
C2
C3
C4
D1
D2
D3
D4
Code:
cat 1.sql | awk '{printf("%s",NR%4 ? $0",":$0"\n")}'

Output :-
Quote:
A1,A2,A3,A4
B1,B2,B3,B4
C1,C2,C3,C4
D1,D2,D3,D4

I want to add 2 more values for each line (hostname,user name) so the output should have hostname & username (testsrv01,test1)

Output should be like :-
Quote:
A1,A2,A3,A4,testsrv01,test1
B1,B2,B3,B4,testsrv01,test1
C1,C2,C3,C4,testsrv01,test1
D1,D2,D3,D4,testsrv01,test1

Thank you
LazySmilieSmilieSmilie
# 2  
Old 09-16-2013
try:
Code:
awk '{printf("%s",NR%4 ? $0",":$0","s","u"\n")}' s=testsrv01 u=test1 1.sql

# 3  
Old 09-16-2013
Code:
awk '{printf("%s",NR%4 ? $0",":$0",ttestsrv01,test1\n")}' 1.sql

This User Gave Thanks to Aia For This Post:
# 4  
Old 09-16-2013
That works can I generate them dynamically
by giving hostname and whoami commands


Code:
 
awk '{printf("%s",NR%4 ? $0",":$0",`hostname`,`whoami`\n")}' 1.sql

It gives me the same

Quote:
A1,A2,A3,A4,`hostname`,`whoami`
B1,B2,B3,B4,`hostname`,`whoami`
C1,C2,C3,C4,`hostname`,`whoami`
D1,D2,D3,D4,`hostname`,`whoami`
# 5  
Old 09-16-2013
Another awk approach:
Code:
awk -v H="$HOSTNAME" -v U="$USER" 'NR%4{ORS=OFS}!(NR%4){$0=$0 OFS H OFS U;ORS=RS}1' OFS=, 1.sql

Using hostname & whoami commands instead:
Code:
awk -v H="$(hostname)" -v U="$(whoami)" 'NR%4{ORS=OFS}!(NR%4){$0=$0 OFS H OFS U;ORS=RS}1' OFS=, 1.sql

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Command to add Carriage Return and Line Feed

Hello, Can someone please share a Simple AWK command to append Carriage Return & Line Feed to the end of the file, If the Carriage Return & Line Feed does not exist ! Thanks (16 Replies)
Discussion started by: rosebud123
16 Replies

2. Shell Programming and Scripting

Replace the perticular fileds in passwd file

Hi I have 2 different password entries in 2 different files for the same user. file 1 - siva:correct:1000:23:siva:/home/siva:/bin/bash file 2 - siva:incorrect:1000:23:siva:/home/siva:/bin/bash file 1 is having correct passwd entry where as file 2 is wrong. Now, i want to compare... (3 Replies)
Discussion started by: kumar85shiv
3 Replies

3. Shell Programming and Scripting

How to add using awk command for a group?

hi all below is the input file contains a code followed by amount input file 345,5.86 346,20.58 399,10.00 400,12.00 i need the output in the following way outputfile totalsumof345and346,26.44 totalsumof399and400,22.00 (10 Replies)
Discussion started by: hemanthsaikumar
10 Replies

4. Shell Programming and Scripting

How to add printf statement in awk command?

hi all i need to add the prinf statement in awk command for the converted comma separated output.... below is my code : Code Credits :RudiC awk -F, 'NF==2 {next} {ITM=$1 AMT=$2+0 CNT=$3+0 TOTA+=$2 ... (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

5. Shell Programming and Scripting

awk to store in a list the values of certain fileds

Dear Group, I have following input: sa;sb;sc;sd;period;ma;mb;mc;md;me sa1;sb1;sc1;sd1;200001;ma1;mb1;mc1;md1;me1 sa2;sb2;sc2;sd2;200002;ma2;mb2;mc2;md2;me2 sa3;sb3;sc3;sd3;200003;ma3;mb3;mc3;md3;me3 first line contains the headers! I want to create with one pass the following output:... (8 Replies)
Discussion started by: suturjik
8 Replies

6. Shell Programming and Scripting

How to selectively NOT output some fileds?

Dear All I have a text file which has many columns (>10,000). I want to create a new text file which will NOT include following columns: 5,15,105,200. How can I do that in shell (or awk, perl)? Thanks. (6 Replies)
Discussion started by: littlewenwen
6 Replies

7. Shell Programming and Scripting

Re: using AWK to compare fileds

I have following text: NAME=ora.LISTENER.lsnr TYPE=ora.listener.type TARGET=ONLINE , ONLINE , ONLINE , ONLINE STATE=ONLINE on host1, ONLINE on host2, ONLINE on host3, ONLINE on host4 NAME=ora.LISTENER_1525.lsnr TYPE=ora.listener.type TARGET=ONLINE ... (2 Replies)
Discussion started by: rcc50886
2 Replies

8. Shell Programming and Scripting

print the fileds of a file

Hi Friends, Please help me in finding the solution for this : I have a file as below : CuDv: name = "hdisk0" status = 1 chgstatus = 2 ddins = "scsidisk" location = "" parent = "vscsi0" connwhere = "830000000000" PdDvLn... (3 Replies)
Discussion started by: vijaya2006
3 Replies

9. UNIX for Advanced & Expert Users

Mandatory fileds in Bugzilla

Hi, We need to configure some of the fileds in bugzilla like Platform,OS, Version etc are mandatory. Is it possible to set, if yes then how to configure. Thanks & Regards, Bache (0 Replies)
Discussion started by: bache_gowda
0 Replies

10. Shell Programming and Scripting

Merging fileds from 2 files

I have 2 files - fileA and fileB I need to match the first field of the 2 files then output some of the fields from fileA on the same line as certain fields from fileB. There will be instances where fileB does not have a match for first field in fileA Thanks in advance (8 Replies)
Discussion started by: Mudshark
8 Replies
Login or Register to Ask a Question