Need to maintain in- and output format with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to maintain in- and output format with awk
# 1  
Old 07-07-2014
Need to maintain in- and output format with awk

Hi All,

I have a data file (myfile.txt) as below:

Code:
- A  H        C
- A  HHH    F
- AAA  HH   I

The importan point is that the width between the columns are not fixed and the column seperator is space.

I wish to change the value of 4th column using awk only when $3 = HH. I can achieve this with following command.

Code:
awk '$2=="AAA" && $3=="HH" {$4="IJ" } 1' myfile.txt

However, the output format is changing to below:
Code:
- A  H      C
- A  HHH    F
- AAA HH IJ

How do I maintaint the output format same as input format?

Thanks
Angshuman
# 2  
Old 07-07-2014
Try something like this

Code:
akshay@Aix:/tmp$ cat file
- A  H        C
- A  HHH    F
- AAA  HH   I

akshay@Aix:/tmp$ awk '$2=="AAA" && $3=="HH" {$0 = substr($0,1,index($0,$4)-1) "IJ"} 1' file
- A  H        C
- A  HHH    F
- AAA  HH   IJ

# 3  
Old 07-07-2014
Hi Akshay,

Thank you for your help. It worked. Can you please help to understand following part of the code:
Code:
substr($0,1,index($0,$4)-1)

Thanks
Angsuman
# 4  
Old 07-07-2014
Code:
awk '$2=="AAA" && $3=="HH" {sub($4 "[[:blank:]]*$","IJ")} 1' file

( if $4 does not contain special characters )


--
@Akshay, what if the value of $4 also happens to be "HH" ?
# 5  
Old 07-07-2014
Quote:
Originally Posted by angshuman
Hi Akshay,

Thank you for your help. It worked. Can you please help to understand following part of the code:
Code:
substr($0,1,index($0,$4)-1)

Thanks
Angsuman
Code:
index(in, find)
    This searches the string in for the first occurrence of the string find, and returns the position in characters where that occurrence begins in the string in. For example:
awk 'BEGIN { print index("peanut", "an") }'

prints `3'. If find is not found, index returns 0. (Remember that string indices in awk start at 1.) 

substr(string, start, length)
    This returns a length-character-long substring of string, starting at character number start. The first character of a string is character number one. For example, substr("washington", 5, 3) returns "ing". If length is not present, this function returns the whole suffix of string that begins at character number start. For example, substr("washington", 5) returns "ington". This is also the case if length is greater than the number of characters remaining in the string, counting from character number start.


substr($0,1,index($0,$4)-1)
Here we extract string from line $0 starting from first char to char at first occurrence position of column 4 $4 minus 1

--
@ Scrutinizer : I didn't check much Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Format output with blocks - awk

Format output with blocks - Awk ZONESET_A,DBP02_HBA0,DC01,20:00:00:25:b5:b9:a0:28,10,port-channel20,01-VB2 ZONESET_A,DBP02_HBA0,DC01,50:00:14:42:a0:72:74:00,10,fc4/30,O1-CORE-D ZONESET_A,dc01vb,DC01,20:00:00:25:b5:b0:a0:13,10,fc1/24,01-VB1... (1 Reply)
Discussion started by: greycells
1 Replies

2. Shell Programming and Scripting

Maintain line format using awk

Hello I have a file with the following format: ... text1 num num P # 2014--2-28-22---6 33.76--38.4173---21.9403----0.08-0.00--0.01--0.01--0.46----------0 text1 num num P text 2 num num S text 3 num num P ... (where "-"=space, "spaces" cannot... (4 Replies)
Discussion started by: phaethon
4 Replies

3. Shell Programming and Scripting

Format output using awk

Hello all , need help with this ... Input File DEV % POOL 0CB4 FBA 2211300 81792 4 IE RAID-5(3+1) R5_EFD100_1 - - 1805376 82 IF RAID-1 M2_FC300_1 - ... (4 Replies)
Discussion started by: greycells
4 Replies

4. Shell Programming and Scripting

Using AWK to format output and email

Hello, I'm a bit stumped, for some reason when using AWK 'print' is not printing the entire date/line. awk '{print "Ticket #: " $1} {print "Queue : " $2} {print "Recieved : " $3} {print "AP Date : " $4} {print "Circuit ID : " $5} {print... (4 Replies)
Discussion started by: ArvinSodhi
4 Replies

5. UNIX for Dummies Questions & Answers

after awk-> format output

hi i have a awk command with several querys.... awk 'FS="|""; print $4, $5, $6...etc.... $4 gives me the date 20120304 $5 is timestamp 101023 I want to format these in 2012.03.04 or 2012/03/04 10:10:23 but have no idea, if this is possible with format-parameters in the awk... (2 Replies)
Discussion started by: Jazzmatazz
2 Replies

6. Shell Programming and Scripting

awk to format an output

awk experts, I have in put file with time stamp followed by "," separated data. same patern continues. The output need time stamp in first columns and data total in 2nd columns. Input file T 9:15 d0,1,3,3 d1,2,1,1 d2,3,1,5 e1,1,1,1 T 9:30 d0,1,1,1 d1,2,3,2 d3,1,2,1... (10 Replies)
Discussion started by: arv_cds
10 Replies

7. Shell Programming and Scripting

awk - format output

Input file1 zone: BAU_SERVER1 C0:50:76:01:C6:20:00:12; 50:06:01:69:3B:20:14:8B; 50:06:01:60:3B:20:14:8B zone: BAU_SERVER2 C0:50:76:01:C6:20:00:08; 50:06:01:69:3B:20:14:8B; 50:06:01:60:3B:20:14:8B zone: ... (4 Replies)
Discussion started by: greycells
4 Replies

8. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

9. Shell Programming and Scripting

[need help] output format from awk

hi all, i have a problem with my nawk command output below is the description : nawk $12 == "00008001" { cnt++;cs_cd } END {for(cd in cs_cd) print cd, cs_cd } 2007020814.TDR output : 133 123 desire output: 133,123,.... please advices thank you so much (6 Replies)
Discussion started by: bucci
6 Replies

10. Shell Programming and Scripting

Output in a particular format using AWK

Hi All, I am trying to check if if column 5 is greater than 90. If greater it will print the term in column 6, else if all are within limit, then it will output "Size is within limit". I can't seem to do that with the below code. The output should only be 1 statement of "Size is within the... (4 Replies)
Discussion started by: Raynon
4 Replies
Login or Register to Ask a Question